工具及軟件
1. myeclipse 10.8
2. hadoop2.6.0 插件 點我下載
1. 將插件放入myeclipse的目錄MyEclipse\MyEclipse 10\dropins
2. 如有必要,刪除configuration目錄下的org.eclipse.update文件夾
3. 第一次啓動eclpse後,會讓我們設定一個工作目錄,即以後建的項目都在這個工作目錄下。
進入後,在菜單window->Rreferences下打開設置:
點擊browse選擇hadoop目錄,然後點OK
4. 打開myeclipse在window>>show view >>會出現MapReduce Tools>>Map/Reduce Locations 打開
配置hadoop運行環境
在eclipse下配置hadoop location的時候。hadoop端口號應該與conf文件夾下的core-site.xml以及mapred-site.xml保持一致,前者對應dfs
master,後者對應map-red master
在配置完後,在Project Explorer中就可以瀏覽到DFS中的文件
編寫第一個MR程序(統計單詞數量)
在eclipse菜單下 new Project 可以看到,裏面增加了Map/Reduce選項:
新建一個Map/Reduce
新建一個類 WordCount.java
代碼如下
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 {
System.out.println("key=" + key.toString());
System.out.println("Value=" + value.toString());
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();
System.out.println("url:" + conf.get("fs.default.name"));
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
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(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
接下來 直接在 myeclipse中運行。 Run on Hadoop
如果報錯 如
(null) entry in command string: null chmod 0700
需要把 hadoop\bin 下的hadoop.dll拷貝到 c:\windows\system32目錄中
以上的WordCount運行還需要設置運行時參數, 輸入文件夾和輸出文件夾
Run Configuration
首先創建 wcin 目錄 並且上傳多個 .txt 的 碎文件 到 wcin目錄下
F:\study\hadoop\hadoop-2.6.5\sbin>hadoop fs -mkdir hdfs://localhost:9002/wcin
F:\study\hadoop\hadoop-2.6.5\sbin>hadoop fs -put c:\a.txt hdfs://localhost:9002/
wcin/wc.txt
wcout目錄不用創建了 ,運行輸出後自動創建
看圖結果
進入 wcout
下載後 打開輸出文件 ,統計出了單子數量
eclipse中查看文件系統