我们正在尝试设计一个简单的程序,其目标是从文件中读取专利数据,并检查其他国家是否引用了该专利,这来自教科书'Hadoop in Action'
by { {1}},我们正在尝试了解'chuck Lam'
。
我们设置的hadoop发布版本为advanced map/reduce programming
,我们正在使用Local Node
在Windows environment
中执行该程序。
这是我们下载文件的网址cygwin
:http://www.nber.org/patents/
和apat63_99.txt
。
我们使用cite75_99.txt
作为分布式缓存文件,'apat63_99.txt'
位于'cite75_99.txt'
文件夹中,我们从命令行参数传递。
问题是程序没有生成输出,我们看到的输出文件中没有数据。
我们尝试使用mapper阶段以及reducer阶段输出,两者都是空白的。
以下是我们为此任务开发的代码:
input
我们使用的工具package com.sample.patent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class country_cite {
private static Hashtable<String, String> joinData
= new Hashtable<String, String>();
public static class Country_Citation_Class extends
Mapper<Text, Text, Text, Text> {
Path[] cacheFiles;
public void configure(JobConf conf) {
try {
cacheFiles = DistributedCache.getLocalCacheArchives(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void map(Text key, Text value, Context context)
throws IOException, InterruptedException {
if (cacheFiles != null && cacheFiles.length > 0) {
String line;
String[] tokens;
BufferedReader joinReader = new BufferedReader(new FileReader(
cacheFiles[0].toString()));
try {
while ((line = joinReader.readLine()) != null) {
tokens = line.split(",");
joinData.put(tokens[0], tokens[4]);
}
} finally {
joinReader.close();
}
}
if (joinData.get(key) != null)
context.write(key, new Text(joinData.get(key)));
}
}
public static class MyReduceClass extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
String patent_country = joinData.get(key);
if (patent_country != null) {
for (Text val : values) {
String cited_country = joinData.get(val);
if (cited_country != null
&& !cited_country.equals(patent_country)) {
context.write(key, new Text(cited_country));
}
}
}
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
DistributedCache.addCacheFile(new Path(args[0]).toUri(),
conf);
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 3) {
System.err.println("Usage: country_cite <in> <out>");
System.exit(2);
}
Job job = new Job(conf,"country_cite");
job.setJarByClass(country_cite.class);
job.setMapperClass(Country_Citation_Class.class);
job.setInputFormatClass(org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat.class);
// job.setReducerClass(MyReduceClass.class);
job.setNumReduceTasks(0);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[1]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[2]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
和Eclipse
为Hadoop's version
。
这些是运行作业的命令行参数:
1.2.1
这是程序执行时生成的跟踪:
/cygdrive/c/cygwin64/usr/local/hadoop
$ bin/hadoop jar PatentCitation.jar country_cite apat63_99.txt input output
请告知我们哪里出错了,万一我错过了重要信息,请告诉我。
谢谢和问候
答案 0 :(得分:1)
我认为该错误符合if (joinData.get(key) != null)
行。 joinData
使用String
作为密钥,并将Text
作为参数传递给get
,以便get
每次都返回null
。尝试将此行替换为if (joinData.get(key.toString()) != null)
。
另一个错误是每个Mapper
和每个Reducer
都在自己的jvm中运行,因此Reducer
和Mapper
无法通过静态对象和{{1}进行通信每个joinData
都是空的。