您好,登錄后才能下訂單哦!
本篇內容介紹了“Hadoop中的HelloWorld程序怎么實現”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
在linux平臺上執(zhí)行wordcount,有官方示例,相應的jar包放在hadoop-2.0.0-cdh5.5.0\share\hadoop\mapreduce1下的hadoop-examples-2.0.0-mr1-cdh5.5.0.jar(注:本人用的是CDH4.5.0版本),我們首先需要準備好數據:
echo "Hello World Hello Hadoop" > 1.txt echo "Hello Hadoop Bye " >2.txt
然后把數據put到HDFS里:
hadoop fs -mkdir /input hadoop fs -put /root/1.txt /input hadoop fs -put /root/2.txt /input
再然后進入到jar所在的目錄里“
cd hadoop-2.0.0-cdh5.5.0\share\hadoop\mapreduce1
執(zhí)行命令:
hadoop jar hadoop-mapreduce-examples-2.0.0-cdh5.5.0.jar WordCount /input /output
其中,/output是執(zhí)行結果輸出目錄。
到此,HelloWorld就順利執(zhí)行了,你可以用hadoop fs -cat /output/part 命令來查看結果.
接下來,我們在看看在window上的eclipse如何執(zhí)行。
首先貼出代碼:
public class WordCount { // mapper public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { private 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 token = new StringTokenizer(line); while (token.hasMoreElements()) { word.set(token.nextToken()); context.write(word, one); } }; } // reduce public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable value : values) { sum += value.get(); } context.write(key, new IntWritable(sum)); }; } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); System.setProperty("HADOOP_USER_NAME", "root");//這句話很重要,要不然會告你沒有權限執(zhí)行 Job job = new Job(conf); String[] ioArgs = new String[] { "hdfs://192.168.1.101:7001/input", "hdfs://192.168.1.101:7001/output" }; String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs(); job.setJarByClass(WordCount.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
然后在eclipse上點執(zhí)行即可,在執(zhí)行時可能發(fā)現jvm內存不夠,添加-Xmx1024M參數執(zhí)行即可。
“Hadoop中的HelloWorld程序怎么實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。