1、使用Hadoop的版本為穩定版0.20.203.0rc1
hadoop-0.20.203.0rc1.tar.gz
當然外掛程式也要選用hadoop-0.20.203.0/contrib/eclipse-plugin中的
hadoop-eclipse-plugin-0.20.203.0.jar
eclipse 可以使用
eclipse-jee-indigo-SR1-linux-gtk.tar.gz
2、在eclipse 中配置Hadoop開發環境時,連接埠號碼的設定
Map/Reduce Master中
port 填9001, 與Hadoop設定檔mapred-site.xml中mapred.job.tracker的連接埠一致
[hadoop@hdp0 conf]$ more mapred-site.xml<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!-- Put site-specific property overrides in this file. --><configuration><property><name>mapred.job.tracker</name> <value>hdp0:9001</value></property></configuration>
DFS Master
port 填9000,與Hadoop設定檔core-site.xml中fs.default.name的連接埠號碼一致。
[hadoop@hdp0 conf]$ more core-site.xml<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!-- Put site-specific property overrides in this file. --><configuration><property><name>fs.default.name</name><value>hdfs://hdp0:9000</value></property></configuration>
參考下面這個描述比較詳細的文章:
在eclipse中配置hadoop外掛程式1.安裝外掛程式
準備程式:
eclipse-3.3.2(這個版本的外掛程式只能用這個版本的eclipse)
hadoop-0.20.2-eclipse-plugin.jar (在hadoop-0.20.2/contrib/eclipse-plugin目錄下)
將hadoop-0.20.2-eclipse-plugin.jar 複製到eclipse/plugins目錄下,重啟eclipse。
2.開啟MapReduce視圖
Window -> Open Perspective -> Other 選擇Map/Reduce,表徵圖是個藍色的象。
3.添加一個MapReduce環境
在eclipse下端,控制台旁邊會多一個Tab,叫“Map/Reduce Locations”,在下面空白的地方點右鍵,選擇“New Hadoop location...”,:
在彈出的對話方塊中填寫如下內容:
Location name(取個名字)
Map/Reduce Master(Job Tracker的IP和連接埠,根據mapred-site.xml中配置的mapred.job.tracker來填寫)
DFS Master(Name Node的IP和連接埠,根據core-site.xml中配置的fs.default.name來填寫)
4.使用eclipse對HDFS內容進行修改
經過上一步驟,左側“Project Explorer”中應該會出現配置好的HDFS,點擊右鍵,可以進行建立檔案夾、刪除檔案夾、上傳檔案、下載檔案、刪除檔案等操作。
注意:每一次操作完在eclipse中不能馬上顯示變化,必須得重新整理一下。
5.建立MapReduce工程5.1配置Hadoop路徑
Window -> Preferences 選擇 “Hadoop Map/Reduce”,點擊“Browse...”選擇Hadoop檔案夾的路徑。
這個步驟與運行環境無關,只是在建立工程的時候能將hadoop根目錄和lib目錄下的所有jar包自動匯入。
5.2建立工程
File -> New -> Project 選擇“Map/Reduce Project”,然後輸入項目名稱,建立項目。外掛程式會自動把hadoop根目錄和lib目錄下的所有jar包匯入。
5.3建立Mapper或者Reducer
File -> New -> Mapper 建立Mapper,自動繼承mapred包裡面的MapReduceBase並實現Mapper介面。
注意:這個外掛程式自動繼承的是mapred包裡舊版的類和介面,新版的Mapper得自己寫。
Reducer同理。
6.在eclipse中運行WordCount程式6.1匯入WordCount
1 import java.io.IOException; 2 import java.util.StringTokenizer; 3 4 import org.apache.hadoop.conf.Configuration; 5 import org.apache.hadoop.fs.Path; 6 import org.apache.hadoop.io.IntWritable; 7 import org.apache.hadoop.io.LongWritable; 8 import org.apache.hadoop.io.Text; 9 import org.apache.hadoop.mapreduce.Job;10 import org.apache.hadoop.mapreduce.Mapper;11 import org.apache.hadoop.mapreduce.Reducer;12 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;13 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;14 15 public class WordCount {16 public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable>{17 18 private final static IntWritable one = new IntWritable(1);19 private Text word = new Text();20 21 public void map(LongWritable key, Text value, Context context)22 throws IOException, InterruptedException {23 StringTokenizer itr = new StringTokenizer(value.toString());24 while (itr.hasMoreTokens()) {25 word.set(itr.nextToken());26 context.write(word, one);27 }28 }29 }30 31 public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {32 private IntWritable result = new IntWritable();33 34 public void reduce(Text key, Iterable<IntWritable> values, Context context)35 throws IOException, InterruptedException {36 int sum = 0;37 for (IntWritable val : values) {38 sum += val.get();39 }40 result.set(sum);41 context.write(key, result);42 }43 }44 45 public static void main(String[] args) throws Exception {46 Configuration conf = new Configuration();47 if (args.length != 2) {48 System.err.println("Usage: wordcount ");49 System.exit(2);50 }51 52 Job job = new Job(conf, "word count");53 job.setJarByClass(WordCount.class);54 job.setMapperClass(TokenizerMapper.class);55 job.setReducerClass(IntSumReducer.class);56 job.setMapOutputKeyClass(Text.class);57 job.setMapOutputValueClass(IntWritable.class);58 job.setOutputKeyClass(Text.class);59 job.setOutputValueClass(IntWritable.class);60 61 FileInputFormat.addInputPath(job, new Path(args[0]));62 FileOutputFormat.setOutputPath(job, new Path(args[1]));63 64 System.exit(job.waitForCompletion(true) ? 0 : 1);65 66 }67 68 }
6.2配置運行參數
Run As -> Open Run Dialog... 選擇WordCount程式,在Arguments中配置運行參數:/mapreduce/wordcount/input /mapreduce/wordcount/output/1
分別表示HDFS下的輸入目錄和輸出目錄,其中輸入目錄中有幾個文字檔,輸出目錄必須不存在。
6.3運行
Run As -> Run on Hadoop 選擇之前配置好的MapReduce運行環境,點擊“Finish”運行。
控制台會輸出相關的運行資訊。
6.4查看運行結果
在輸出目錄/mapreduce/wordcount/output/1中,可以看見WordCount程式的輸出檔案。除此之外,還可以看見一個logs檔案夾,裡面會有啟動並執行日誌。
參考二:
Hadoop學習全程記錄——在Eclipse中運行第一個MapReduce程式
HadoopEclipseMapreduceJavaUbuntu
接上一篇文章:Hadoop學習全程記錄——hadoop 入門
這是Hadoop學習全程記錄第2篇,在這篇裡我將介紹一下如何在Eclipse下寫第一個MapReduce程式。
新說明一下我的開發環境:
作業系統:在windows下使用wubi安裝了ubuntu 10.10
hadoop版本:hadoop-0.20.2.tar.gz
Eclipse版本:eclipse-jee-helios-SR1-linux-gtk.tar.gz
為了學習方便這個例子在“偽分布式模式”Hadoop安裝方式下開發。
第一步,我們先啟動Hadoop守護進程。
如果你讀過我第1篇文章Hadoop學習全程記錄——hadoop 入門應該比較清楚在“偽分布式模式”下啟動Hadoop守護進程的方法,在這裡就不多說了。
第二步,在Eclipse下安裝hadoop-plugin。
1.複製 hadoop安裝目錄/contrib/eclipse-plugin/hadoop-0.20.2-eclipse-plugin.jar 到 eclipse安裝目錄/plugins/ 下。
2.重啟eclipse,配置hadoop installation directory。
如果安裝外掛程式成功,開啟Window-->Preferens,你會發現Hadoop Map/Reduce選項,在這個選項裡你需要配置Hadoop installation directory。配置完成後退出。
3.配置Map/Reduce Locations。
在Window-->Show View中開啟Map/Reduce Locations。
在Map/Reduce Locations中建立一個Hadoop Location。在這個View中,右鍵-->New Hadoop Location。在彈出的對話方塊中你需要配置Location name,如myubuntu,還有Map/Reduce Master和DFS Master。這裡面的Host、Port分別為你在mapred-site.xml、core-site.xml中配置的地址及連接埠。如:
Map/Reduce MasterJava代碼
- localhost
- 9001
localhost9001
DFS MasterJava代碼
- localhost
- 9000
localhost9000
配置完後退出。點擊DFS Locations-->myubuntu如果能顯示檔案夾(2)說明配置正確,如果顯示"拒絕串連",請檢查你的配置。
第三步,建立項目。
File-->New-->Other-->Map/Reduce Project
項目名可以隨便取,如hadoop-test。
複製 hadoop安裝目錄/src/example/org/apache/hadoop/example/WordCount.java到剛才建立的項目下面。
第四步,上傳類比資料檔案夾。
為了運行程式,我們需要一個輸入的檔案夾,和輸出的檔案夾。輸出檔案夾,在程式運行完成後會自動產生。我們需要給程式一個輸入檔案夾。
1.在目前的目錄(如hadoop安裝目錄)下建立檔案夾input,並在檔案夾下建立兩個檔案file01、file02,這兩個檔案內容分別如下:
file01Java代碼
- Hello World Bye World
Hello World Bye World
file02Java代碼
- Hello Hadoop Goodbye Hadoop
Hello Hadoop Goodbye Hadoop
2.將檔案夾input上傳到Distributed File System中。
在已經啟動Hadoop守護進程終端中cd 到hadoop安裝目錄,運行下面命令:Java代碼
- bin/hadoop fs -put input input01
bin/hadoop fs -put input input01
這個命令將input檔案夾上傳到了hadoop檔案系統了,在該系統下就多了一個input01檔案夾,你可以使用下面命令查看:Java代碼
- bin/hadoop fs -ls
bin/hadoop fs -ls
第五步,運行項目。
1.在建立的項目hadoop-test,點擊WordCount.java,右鍵-->Run As-->Run Configurations
2.在彈出的Run Configurations對話方塊中,點Java Application,右鍵-->New,這時會建立一個application名為WordCount
3.配置運行參數,點Arguments,在Program arguments中輸入“你要傳給程式的輸入檔案夾和你要求程式將計算結果儲存的檔案夾”,如:Java代碼
- hdfs://localhost:9000/user/panhuizhi/input01 hdfs://localhost:9000/user/panhuizhi/output01
hdfs://localhost:9000/user/panhuizhi/input01 hdfs://localhost:9000/user/panhuizhi/output01
這裡面的input01就是你剛傳上去檔案夾。檔案夾地址你可以根據自己具體情況填寫。
4.點擊Run,運行程式。
點擊Run,運行程式,過段時間將運行完成,等運行結束後,可以在終端中用命令:Java代碼
- bin/hadoop fs -ls
bin/hadoop fs -ls
查看是否組建檔案夾output01。
用下面命令查看產生的檔案內容:Java代碼
- bin/hadoop fs -cat output01/*
bin/hadoop fs -cat output01/*
如果顯示如下,恭喜你一切順利,你已經成功在eclipse下運行第一個MapReduce程式了。Java代碼
- Bye 1
- Goodbye 1
- Hadoop 2
- Hello 2
- World 2
Bye1Goodbye1Hadoop2Hello2World2