1. 程式人生 > >Hadoop學習筆記:(一)WordCount執行

Hadoop學習筆記:(一)WordCount執行

前言:本文是在hadoop已經配置好的情況下

WordCount是hadoop下的HelloWorld程式,是初學者必須要會的。下面是用eclipse進行開發

一、工程與MapReduce程式碼

新建工程,建立WordCount class
下面的程式碼是舊版mapreduce

package mapreduce;

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache
.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.FileInputFormat; import org.apache.hadoop.mapred.FileOutputFormat; import org.apache.hadoop.mapred.JobClient; import org.apache.hadoop.mapred.JobConf; import org.apache
.hadoop.mapred.MapReduceBase; import org.apache.hadoop.mapred.Mapper; import org.apache.hadoop.mapred.OutputCollector; import org.apache.hadoop.mapred.Reducer; import org.apache.hadoop.mapred.Reporter; import org.apache.hadoop.mapred.TextInputFormat; import org.apache.hadoop.mapred.TextOutputFormat;
public class WordCount { public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while(tokenizer.hasMoreTokens()){ word.set(tokenizer.nextToken()); output.collect(word, one); } } } public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { int sum = 0; while(values.hasNext()){ sum += values.next().get(); } output.collect(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { JobConf conf = new JobConf(WordCount.class); conf.setJobName("wordcount"); conf .setOutputKeyClass(Text.class); conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(Map.class); conf.setReducerClass(Reduce.class); conf.setInputFormat(TextInputFormat.class); conf.setOutputFormat(TextOutputFormat.class); FileInputFormat.setInputPaths(conf, new Path(args[0])); FileOutputFormat.setOutputPath(conf, new Path(args[1])); JobClient.runJob(conf); } }

上述程式碼的執行成功離不開很多jar包,這些jar包主要在安裝的hadoop資料夾裡面。具體位置參考:

HADOOP_HOME/share/hadoop/

這裡的HADOOP_HOME是你安裝hadoop的路徑。

這個目錄下包含下面資料夾:

common
httpfs
hdfs
mapreduce
tool
yarn

其中我們的程式需要從common, mapreduce, yarn資料夾裡新增存在的所有jar包以及lib下的所有jar包(這可能有重複,只要相同的覆蓋即可)

新增好jar包後,就可以執行程式了。這裡我們要為程式配置兩個輸入。

本地測試時,直接在eclipse點選run configuration ,在arguments下的program argument下新增要統計的檔案地址以及輸出檔案路徑。

1.本地模式:測試檔案路徑 輸出檔案路徑(資料都在本地)
2.HDFS檔案:hdfs://localhost/測試檔案 hdfs://localhost/輸出目錄(資料在HDFS中)

配置好了,就可以點選run運行了。

另外:

也可以用命令列

打包:用eclipse將工程打包.
執行:hadoop jar wordcount.jar input output

這裡的input最好寫成HDFS下的檔案路徑.(本地的不知道怎麼表示,試了下老報錯),好像hadoop它會預設載入hdfs的路徑.

叢集模式:

先將檔案複製到HDFS上:hadoop dfs -copyFromLocal 本地檔案 hdfs路徑
然後命令列執行:hadoop jar wordcout.jar WordCout input output

執行wordcount.jar中的WordCount類,input作為輸入,output作為輸出.