吸取前人教訓(xùn),寫下此稿
筆者所用到的軟件版本:
hadoop 2.7.3
hadoop-eclipse-plugin-2.7.3.jar
eclipse-java-2020-06-R-win32-x86_64
先從虛擬機下載hadoop 需要解壓好和文件配置好的版本,方便后文配置偽分布式文件)
?筆者linux的hadoop目錄為:/usr/hadoop
下載到windows的某個目錄,自行選擇 筆者下載到的windows目錄為D:\desktop\fsdownload\hadoop-2.7.3
下載eclipse和hadoop連接的插件?hadoop-eclipse-plugin-2.7.3.jar
(筆者hadoop版本為2.7.3,你使用的hadoop版本和插件版本需要對應(yīng))
鏈接:https://pan.baidu.com/s/1m5BCOa1vcNghG0TUklB7CA?
提取碼:e9mo
(此鏈接為hadoop-eclipse-plugin-2.7.3.jar)
將下載好的插件hadoop-eclipse-plugin-2.7.3.jar 復(fù)制到eclipse的plugins目錄下
?
將winutils.exe文件復(fù)制到hadoop目錄下的bin路徑下
wintil.exe鏈接
鏈接:https://pan.baidu.com/s/1Ahzo8IyPhoxzvPSuT1nN7Q?
提取碼:bhip
配置hadoop環(huán)境變量
進入 設(shè)置--系統(tǒng)--關(guān)于--高級系統(tǒng)設(shè)置--環(huán)境變量
編輯path變量,增加內(nèi)容為:%HADOOP_HOME%\bin
增加一個變量HADOOP_HOME
變量值為 hadoop的路徑
?開啟Hadoop集群(此處省略。。自行開啟)
開啟集群和復(fù)制插件之后 開啟eclipse,可以在eclipse左側(cè)工作區(qū)看到DFS Locations
?在windows菜單下選中perference
perence下選中hadoop map/reduce
設(shè)置hadoop的安裝路徑 前文有提到 筆者的安裝路徑為?D:\desktop\fsdownload\hadoop-2.7.3
?
?建立與hadoop的連接 在eclipse下方工作區(qū)對Map/Reduce Locations 進行設(shè)置
?右擊 new hadoop location 進入設(shè)置
相關(guān)設(shè)置如圖
筆者的core-site.xml(筆者路徑為%HADOOP_HOME%/etc/hadoop/core-site.xml)文件中端口號為9000
?設(shè)置完成之后可以在DFS Locations看到hdfs上的文件
新建項目hadoop1(項目名隨意)
進入eclipse 點擊左上角file--new--projects
?輸入項目名稱
這樣就創(chuàng)建好了一個mapreduce項目
?執(zhí)行MapReduce 項目前,需要將hadoop中有修改過的配置文件(如偽分布式需要core-site.xml 和 hdfs-site.xml),以及l(fā)og4j.properties復(fù)制到項目下的 src文件夾中
(請忽略已經(jīng)建好的包)
?還需要將windows下的hosts文件修改 讓ip和主機名映射
hosts文件路徑為C:\Windows\System32\drivers\etc\hosts
而修改hosts文件需要管理員權(quán)限
可以先右擊hosts文件 點擊屬性
?將屬性中的“只讀”取消 確定并應(yīng)用
然后通過管理員身份運行命令提示窗口
進入hosts文件目錄上一級
輸入notepad hosts 即可修改hosts文件
?
?在最底下 輸入相對應(yīng)的映射關(guān)系即可
筆者為192.168.222.171 master746
在hadoop1項目中創(chuàng)建包和類
需要填寫兩個地方:在Package?處填寫?org.apache.hadoop.examples;在Name?處填寫wordcount。(包名、類名可以隨意起)
此處筆者參考?http://t.csdn.cn/4WKHD
寫入代碼
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);
}
}
}
}
運行時右擊空白處 點擊 run as 然后點擊 run configutations?
?前面寫需要進行mapreduce計算的文件 必須原本就存在
后面寫輸出文件 必須原本不存在 不然均會報錯
?看到類似這樣的運行結(jié)果就是成功啦
成功運行后 查看輸出文件(若未及時出現(xiàn) 可以右擊文件refresh)
?
?做到這 eclipse和hadoop就算連接成啦
完結(jié)撒花~
跟著筆者的步驟可以避免筆者踩過的大部分坑
下面是我沒有注明在文中的一些錯誤,歡迎大家一起來討論
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Wi
報錯如下
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
at org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Native Method)
at org.apache.hadoop.io.nativeio.NativeIO$Windows.access(NativeIO.java:609)
at org.apache.hadoop.fs.FileUtil.canRead(FileUtil.java:977)
at org.apache.hadoop.util.DiskChecker.checkAccessByFileMethods(DiskChecker.java:187)
at org.apache.hadoop.util.DiskChecker.checkDirAccess(DiskChecker.java:174)
at org.apache.hadoop.util.DiskChecker.checkDir(DiskChecker.java:108)
at org.apache.hadoop.fs.LocalDirAllocator$AllocatorPerContext.confChanged(LocalDirAllocator.java:285)
at org.apache.hadoop.fs.LocalDirAllocator$AllocatorPerContext.getLocalPathForWrite(LocalDirAllocator.java:344)
at org.apache.hadoop.fs.LocalDirAllocator.getLocalPathForWrite(LocalDirAllocator.java:150)
at org.apache.hadoop.fs.LocalDirAllocator.getLocalPathForWrite(LocalDirAllocator.java:131)
at org.apache.hadoop.fs.LocalDirAllocator.getLocalPathForWrite(LocalDirAllocator.java:115)
at org.apache.hadoop.mapred.LocalDistributedCacheManager.setup(LocalDistributedCacheManager.java:125)
at org.apache.hadoop.mapred.LocalJobRunner$Job.<init>(LocalJobRunner.java:163)
at org.apache.hadoop.mapred.LocalJobRunner.submitJob(LocalJobRunner.java:731)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:240)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1290)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1287)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1287)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1308)
at com.qs.WordcountDriver.main(WordcountDriver.java:44)
解決辦法:
在創(chuàng)建的項目中 重新創(chuàng)建一個包,名為:org.apache.hadoop.io.nativeio
在包中創(chuàng)建一個類,名為NativeIO
內(nèi)容為:???參考鏈接:http://t.csdn.cn/YxMOW
創(chuàng)建好在eclipse如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-443252.html
?再運行,就可以解決了。文章來源地址http://www.zghlxwxcb.cn/news/detail-443252.html
到了這里,關(guān)于eclipse和hadoop連接攻略(詳細) Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Wi的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!