您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)hadoop-002中Eclipse如何運(yùn)行WordCount,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
1、如過提示 eclipse 無法編譯 文件 ,提示對某文件無權(quán)限。
chmod -R 777 workspace
2、在eclipse中跑Hadoop測試用例時(shí),出現(xiàn)這樣的錯(cuò)誤
Exception in thread "main" org.apache.hadoop.mapred.InvalidInputException: Input path does not exist:
原因是系統(tǒng)沒有找到hadoop的配置文件,
對于2.5.2就是core-site.xml
其中指定了
fs.defaultFS的配置
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
解決方法:
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");
//conf.set("fs.defaultFS", "hdfs://localhost:9000");
//conf.addResource(new Path("/opt/hadoop/etc/hadoop/core-site.xml"));
任選注釋掉代碼其中的一行執(zhí)行即可。
完整代碼如下:
package com.zwh; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; 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.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 WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://localhost:9000"); Job job = new Job(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path("/user/root/input/")); FileOutputFormat.setOutputPath(job,new Path("/user/root/output/wc")); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
示意圖
關(guān)于“hadoop-002中Eclipse如何運(yùn)行WordCount”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。