请看下面的代码
Map.java
public class Map extends Mapper<longwritable, intwritable="" text,=""> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
</longwritable,>
减少.java
public class Reduce extends Reducer<text, intwritable,="" intwritable="" text,=""> {
@Override
protected void reduce(
Text key,
java.lang.Iterable<intwritable> values,
org.apache.hadoop.mapreduce.Reducer<text, intwritable,="" intwritable="" text,="">.Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
context.write(key, new IntWritable(sum));
}
}
</text,></intwritable></text,>
字数.java
public class WordCount {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "wordcount");
job.setJarByClass(WordCount.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}
整个代码都是从这个 Map Reduce
教程(http://cloud.dzone.com/articles/how-run-elastic-mapreduce-job)
. 当我将这些类复制到eclipse中时,它显示了很多错误,比如cannotbe Resolved By Type
. 这是合理的,因为此代码用作示例的类在默认jdk中找不到,而且教程也没有给出下载任何库的任何说明。我忽略了它,以为它与 Elastic Map Reduce
在服务器端。
当我把这个上传到amazonelasticmapreduce,创建了一个作业流并运行了这个程序,它给了我以下错误。
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Configuration cannot be resolved to a type
Configuration cannot be resolved to a type
Job cannot be resolved to a type
Job cannot be resolved to a type
Text cannot be resolved to a type
IntWritable cannot be resolved to a type
TextInputFormat cannot be resolved to a type
TextOutputFormat cannot be resolved to a type
FileInputFormat cannot be resolved
Path cannot be resolved to a type
FileOutputFormat cannot be resolved
Path cannot be resolved to a type
at WordCount.main(WordCount.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:187)
我怎样才能使这个代码工作?我需要下载图书馆吗?如何运行此代码并查看结果?这是我在amazon和elastic map reduce的第一次体验,也是第一次体验大数据。
请帮忙。
2条答案
按热度按时间6rvt4ljy1#
在eclipse中将所有hadoopjar添加到项目中,如果您的代码没有错误,那么您可以将其导出为jar并在hadoop中运行jar。
要将jar添加到“构建路径”,请选择“配置构建路径”和“添加外部jar”(选择所有hadoop jar并添加它们)
gk7wooem2#
所以,你的意思是,你没有在你的项目中添加任何hadoopjar,你忽略了编译错误,希望这个可以在安装了hadoop客户机的服务器端运行?
如果这是真的,那是不可能的。
必须将hadoop-client.xx.jar添加到项目中,任何版本都可以。