寫在前面:
測試環境:Deepin 15.10.1、Hadoop-2.8.5、Eclipse Photon Release (4.8.0)
Step0:使用Eclipse操作HDFS
安裝hadoop2x-eclipse-plugin插件,能夠使得在Eclipse中查看HDFS中的目錄和文件。
安裝 hadoop-eclipse-plugin,可在下Github 上下載的 hadoop2x-eclipse-plugin.下載後,將 release 中的 hadoop-eclipse-kepler-plugin-2.6.0.jar 複製到 Eclipse 安裝目錄的 plugins 文件夾中,運行 eclipse -clean
重啓 Eclipse。(添加插件後只需要運行一次該命令,以後按照正常方式啓動就行了)。
安裝完成後進行有關配置,在此強調需要先啓動Hadoop,需要先啓動Hadoop,需要先啓動Hadoop(重要的事情說三遍)。重啓完成Eclipse後,可在project Explorer中看到上圖中的DFS Locations。
接着配置本地hdfs目錄位置。安裝插件後在Window->Preferences中會多出Hadoop Map/Reduce選項,點擊配置本機上的hadoop目錄。
Step1:創建MapReduce項目
MapReduce項目其實就是普通的Java項目,由於我們先前安裝了插件,在此處我們可以使用插件進行快捷項目創建,在File->New->Map/Reduce Project。(其實這裏就是幫助我們導入了有關的jar包,無需再手動導入)
創建項目完成後,切換Map/Reduce開發視圖,Window -> Perspective -> Open Perspective -> Other,在下方同Console相同的面板區即可查看到Map/Reduce Locations 開發面板。
選中Map/Reduce Locations面板中 ,在空白區右擊選擇New Hadoop Location,建立與 Hadoop 集羣的連接。
在彈出來的 General 選項面板中,General 的設置要與 Hadoop 的配置一致。一般兩個 Host 值是一樣的,如果是僞分佈式,填寫 localhost 即可,此處本地配置的是Hadoop僞分佈式配置,設置 fs.defaultFS 爲 hdfs://localhost:9001,則 DFS Master 的 Port 要改爲 9001。Map/Reduce(V2) Master 的 Port 用默認的即可,Location Name 隨意填寫。
配置完成後即可在DFS Locations中查看到HDFS中的文件,可以自由的進行文件的上傳和下載等操作。
Step3:編寫MapReduce程序
右擊WordCount項目,創建一個新的Class,這裏我們使用官方的WordCount文件。代碼如下:
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.Iterator;
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 WordCount() {
}
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 = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCount.TokenizerMapper.class);
job.setCombinerClass(WordCount.IntSumReducer.class);
job.setReducerClass(WordCount.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);
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public IntSumReducer() {
}
public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0;
IntWritable val;
for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
val = (IntWritable)i$.next();
}
this.result.set(sum);
context.write(key, this.result);
}
}
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private static final IntWritable one = new IntWritable(1);
private Text word = new Text();
public TokenizerMapper() {
}
public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while(itr.hasMoreTokens()) {
this.word.set(itr.nextToken());
context.write(this.word, one);
}
}
}
}
執行WordCount.java之前,還需要將本地Hadoop配置文件和log4j配置文件複製到項目目錄(未添加log4j配置文件可以正常執行), WordCount的輸入和輸出文件都在HDFS中,所以需要配置Eclipse中的執行文件時的參數,正常執行後控制檯無輸出(圖中輸出WARN由於未配置log4j),在DFS Locantions中可以查看到執行結果。(test.txt文件內容只有一句話:It's a test text.txt)
除了設置運行參數以外,還可以通過修改代碼實現參數的設置。修改如下代碼即可。
// String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
String[] otherArgs=new String[]{"input","output"}; /* 直接設置輸入參數 */