須要用到的工具java
jdk(個人是1.8)linux
hadoop-eclipse-plugin-2.6.5.jar(這裏我提供已編譯好的包 下載地址,如果其餘版本可自行搜索或用ant和hadoop源代碼自行編譯)apache
eclipse(個人版本是eclipse-standard-luna-R-linux-gtk-x86_64.tar.gz)bash
hadoop-2.7.0.tar.gzapp
hadoop2.7僞分佈式搭建地址eclipse
1. 下載解壓eclipse,解壓分佈式
sudo tar -zxvf eclipse-standard-luna-R-linux-gtk-x86_64.tar.gz
點擊eclipse中的eclipse.ini文件打開eclipse工具
2. eclipse打開報錯oop
解決,進入eclipse的jre目錄,將javabin目錄連接到eclipse中測試
cd ./eclipse/jre sudo ln -s /usr/local/java-8-openjdk-amd64/bin/ bin
3. 打開eclipse > window > perference搜索mapreduce,添加hadoop目錄
4. 顯示hadoop鏈接配置界面Windows > show view > other
5. 在Map.Reduce Locations中 右鍵 > new application
6. 打開mapreduce的hdfs,在左側就有hdfs目錄了
8. 新建mapreduce project
9. 新建包和類輸入測試代碼
package com.hadoop.test; 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(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length < 2) { System.err.println("Usage: wordcount <in> [<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); for (int i = 0; i < otherArgs.length - 1; ++i) { FileInputFormat.addInputPath(job, new Path(otherArgs[i])); } FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
10. 建立單詞文件,並上傳到hdfs上
11. 右鍵》run As 》run on configuration,輸出