【轉】Windows10下Eclipse搭建Hadoop3開發環境
阿新 • • 發佈:2019-01-10
Windows10下Eclipse搭建Hadoop3開發環境
前言
由於筆記本配置限制,虛擬機器CentOs-7關閉了圖形介面,作者在Windows端編寫mapreduce程式然後在linux上執行。
工具
windows 10
CentOs-7(已安裝,見上一博文)
eclipse-jee-oxygen-2-win32-x86_64.zip(採用其他精簡版的eclipse可能會出問題)
jdk1.8(請保證jdk位數跟計算機位數一致)
在Windows上解壓Hadoop3
我們程式設計要用到hadoop的庫,需要將
hadoop-3.0.0.tar.gz
hadoop-3.0.0\bin
資料夾;接著將bin
裡面的hadoop.dll
複製到C:\Windows\System32
中。(hadoop.dll
檔案儘量用最新版的)到系統->高階系統設定->環境變數下面的系統變數處選擇“新建”
然後設定一下PATH
開放Hadoop的許可權
- 為了能在Ecplise上對Linux的HDFS檔案操作,需要設定一下許可權。
- 進入Linux修改裡面的
hdfs-site.xml
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
- 1
- 2
- 3
- 4
- 請保證已經在hadoop上已經建立了使用者以及新建了
input
資料夾,如果之前沒做這一步請執行以下命令(開啟Hadoop集群后)
hadoop dfs -mkdir -p /user/hadoop
hadoop dfs -mkdir input
- 1
- 2
- (開啟Hadoop集群后)執行
hadoop fs -chmod 777 /user/hadoop
在Eclipse上安裝Hadoop外掛
自行去下載
hadoop-eclipse-plugin-2.6.0.jar
,放到Eclipse的plugins
目錄下,重啟Eclipse。開啟eclipse,在
window->Preferences->Hadoop Map/Reduce
下設定Hadoop的解壓路徑
點選
window->show view->other->map/reduce locations
OPEN。- 也可以在右上角的這裡切換到map/reduce專案(以後可以在這裡切回去Resource介面)
- 右鍵new一個
- 配置如下,Host那裡最好直接填IP地址,如果像我這樣填Linux主機名請先在Windows的Hosts檔案設定好IP對映
然後點選右邊設定一下
hadoop.tmp.dir
的地址,跟core-site.xml
的要一致
還有這個我們之前設定為1
- Finish後可能會報
NullPointer
錯誤,貌似沒影響先不理它。
執行WordCount例子
- 先去linux啟動下Hadoop叢集
start-all.sh
- 此時Eclipse應該能看到以下內容,沒有就試試右鍵重新整理下
- 注意要先建立一些檔案到
input
資料夾內,建立方法可以在linux上用命令列上傳上去,也可以先在windows新建好一些形如input1.txt
檔案,裡面隨便填一些句子hello word
之類的,然後在Eclipse直接上傳上去。
- 新建專案,
File->new->Project->Map/Reduce project
,包名最好留空!否則最後執行老會遇到”找不到class”錯誤。 - 新建一個class命名為
WordCount
加入下面程式碼
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 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);
}
}
}
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<span class="hljs-preprocessor">.iterator</span>()<span class="hljs-comment">; i$.hasNext(); sum += val.get()) {
val = (IntWritable)i$.next();
}
this.result.set(sum);
context.write(key, this.result);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 右鍵class選擇
run as->Run configuations
設定如下,當然裡面的IP填你Linux主機的IP,然後RUN
即可。
- 輸出結果如下,下次執行的話需要先將
output
資料夾刪除掉。
打包JAR在linux執行
- 如果上述步驟搞不好,無法在eclipse直接執行程式碼,也可以
export
出一個jar包,通過SFTP發到linux上執行
hadoop jar WordCount.jar WordCount input output
- 1
這裡中間的WordCount貌似是填Main函式的所在Class名,但是網上說填包名,我報錯無數次(找不到class)之後,在新建project時不用包名才成功執行。