轉帖|使用教程|編輯:龔雪|2014-12-15 10:16:02.000|閱讀 3598 次
概述:Windows下Eclipse連接Hadoop的方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
慧都年終巨獻,Aspose系列產品黃金版+白金版享<8折>優惠! 截止日期2014/12/31
hadoop在虛擬機上(遠程連接也是一樣只需要知道master的ip和core-site.xml配置即可。
Vmware上搭建了hadoop分布式平臺:
192.168.11.134 master
192.168.11.135 slave1
192.168.11.136 slave2
core-site.xml 配置文件:
<property> <name>fs.defaultFS</name> <value>hdfs://master:9000</value> <description>The name of the default file system.</description> </property> <property> <name>hadoop.tmp.dir</name> <!-- 注意創建相關的目錄結構 --> <value>/usr/setup/hadoop/temp</value> <description>A base for other temporary directories.</description> </property> |
1 下載插件
hadoop-eclipse-plugin-2.5.1.jar
github上下載源碼后需要自己編譯。這里使用已經編譯好的插件即可
2 配置插件
把插件放到..\eclipse\plugins目錄下,重啟eclipse,配置Hadoop installation directory ,如果插件安裝成功,打開Windows—Preferences后,在窗口左側會有Hadoop Map/Reduce選項,點擊此選項,在窗口右側設置Hadoop安裝路徑。(windows下只需把hadoop-2.5.1.tar.gz解壓到指定目錄)
3 配置Map/Reduce Locations
打開Windows—Open Perspective—Other,選擇Map/Reduce,點擊OK,控制臺會出現:
右鍵 new Hadoop location 配置hadoop:輸入Location Name,任意名稱即可.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成與core-site.xml的設置一致即可。
點擊"Finish"按鈕,關閉窗口。
點擊左側的DFSLocations—>master (上一步配置的location name),如能看到user,表示安裝成功
4 wordcount實例
File—>Project,選擇Map/Reduce Project,輸入項目名稱WordCount等。在WordCount項目里新建class,名稱為WordCount,代碼如下:
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(); 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("hdfs://192.168.11.134:9000/in/test*.txt"));//路徑1 FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.11.134:9000/output"));//輸出路徑 System.exit(job.waitForCompletion(true) ? 0 : 1); } } |
上面的路徑1 和路徑2 由于在代碼中已經定義,這不需要在配置文件中定義,若上面路徑1和路徑2 代碼為:
FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); |
這需要配置運行路徑:類 右鍵 Run As—>Run Configurations
紅色部分為配置的hdfs上文件路徑,
點擊run 或或者:Run on Hadoop,運行結果會顯示在DFS Locations。若運行中有更新,右鍵DFS Locations,點disconnect更新
運行結果:
5 問題及解決辦法
1、 在Hadoop的bin目錄下放winutils.exe,
2、在環境變量中配置 HADOOP_HOME,
3、hadoop.dll拷貝到C:\Windows\System32下面即可
上面的文件已經下載 ,在文件hadoop-common-2.2.0-bin-master.zip中。
安裝過程中由于已經在hdfs上上傳了文件,當重啟在 hdfs namenode –format時,后,會提示無法上傳文件,此時需要刪除hdfs已經存在的副本:
在master上刪除dfs上name目錄下的current目錄: rm –rf current/
在slave上刪除dfs上的整個data目錄 :rm –rf data/
將文件log4j.properties放到src下和java文件同目錄.
參考博客:
<property> <name>dfs.permissions</name> <value>false</value> </property> |
原帖://www.cnblogs.com/baixl/p/4154429.html
狂歡繼續!【年終大促 巔峰盛"慧" 】促銷火熱進行中 iPhone 6 Plus、 iPhone 6、iPad Air滿就送,還不趕快買買買!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網