MapReduce 在使用字典表時“#”后面的映射在map中,結果顯示找不到檔案路徑但在map中填寫字典路徑則沒問題。下面是代碼
package Testday07_01_lagou;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.filecache.DistributedCache;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class lagoudriver {
public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
//System.setProperty("HADOOP_USER_NAME", "root");
String inputPath = "file:/F:/Text/lagou/lagou.txt";
String outPath = "file:/F:/Text/lagou/lagouOut5";
String dicpath="file:/F:/Text/lagou/lagoudic.txt";
//Configuration conf=new Configuration();
Job job = Job.getInstance();
//job.setNumReduceTasks(0);
job.setJarByClass(lagoudriver.class);
//job.setJar("C:\\Users\\Fantastic_Rebo\\Desktop\\wordcount_zxf.jar");
//job.setJobName("wordcountzxf");
job.setMapperClass(lagoumapper.class);
job.setReducerClass(lagoureduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
Path pathdic=new Path(dicpath);
String dicpath2=pathdic.toUri().toString()+"#"+"dic";
DistributedCache.addCacheFile(new URI(dicpath2), job.getConfiguration());
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outPath));
boolean waitForCompletion = job.waitForCompletion(true);
System.out.println(waitForCompletion);
}
}
```
package Testday07_01_lagou;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Set;
import javax.swing.plaf.synth.SynthSpinnerUI;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class lagoumapper extends Mapper<LongWritable, Text, Text, IntWritable> {
HashMap<String, String> hm=new HashMap<>();
BufferedReader br=null;
@Override
protected void setup(Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
br=new BufferedReader(new InputStreamReader(new FileInputStream("dic")));
try {
while(br.ready()) {
String readLine = br.readLine();
String[] line = readLine.split(",");
if(hm!=null) {
hm.put(line[0], readLine);
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if(br!=null)
br.close();
}
}
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String[] lines = value.toString().split(",");
String code=null;
String addr=null;
if(lines.length==6) {
code=lines[2];
addr=lines[lines.length-2];
}else {
code=lines[3];
addr=lines[lines.length-2];
}
//融資輪數
String lun=code.substring(0, 1);
context.write(new Text("rongzi,"+lun), new IntWritable(1));
//城區的分布情況
if(addr.contains("北京")) {
Set<String> set = hm.keySet();
System.out.println(set);
for (String chengqu : set) {
if(addr.contains(chengqu)) {
String chinessenglishchengqu = hm.get(chengqu);
context.write(new Text("chengqu,"+chinessenglishchengqu), new IntWritable(1));
}
}
}
}
}
```
以下是顯示的錯誤
java.lang.Exception: java.io.FileNotFoundException: dic (系統找不到指定的檔案。)
at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.io.FileNotFoundException: dic (系統找不到指定的檔案。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at Testday07_01_lagou.lagoumapper.setup(lagoumapper.java:26)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:142)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:243)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
```
```
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/49097.html
標籤:華為云計算
