Hadoop 踩坑记(三)java
Eclipse配置hadoop开发环境node
windows 10git
java 1.8github
namenode(hadoop1-ali) 阿里云(CentOS 7.3) 120.26.173.104apache
hadoop 版本 2.8.5
windows
须要安装 Enterprise 版本,因为网络缘由,建议使用离线安装包网络
https://www.eclipse.org/downl...app
固然在此以前先要安装 java 在此不作赘述eclipse
把下载好的 Hadoop 解压到本地目录。添加系统环境变量:新建变量名 HADOOP_HOME
,值为Hadoop 的解压路径,如 D:\Program Files\hadoop-2.7.5
ide
添加到 path
中:%HADOOP_HOME%\bin
把 hadoop-eclipse-plugin-2.8.5.jar
包复制到 Eclipse 目录下的 plugins
目录中。重启Eclipse。
打开 Window->Prefences
能够看到左侧多出了 Hadoop Map/Reduce
项
点击多出的 Hadoop Map/Reduce
项,在右侧添加 Hadoop 解压路径 如 D:\Program Files\hadoop-2.7.5
解压 hadoop-common-2.8.5-bin-master
包(本人是直接从 github 上找的,要寻找对应版本的文件),把解压获得的 bin
目录下的 hadoop.dll、hadoop.exp、hadoop.lib、winutils.exe
这四个文件复制到 hadoop-2.8.5
的 bin
目录下。
再把 hadoop.dll
和 winutils.exe
复制到 C:\Windows\System32
目录下
Eclipse 中依次点击:Window->Open Perspective->Map/Reduce
,项目左侧中出现 DFS Locations
结构。
若是没有,直接新建一个 map/reduce
项目便可
Eclipse 中依次点击:Window->Show View ->Other->MapReduce Tools->Map/Reduce Locations
点击肯定(open)
控制台多出了 Map/Reduce Locations
视图。
右键 Map/Reduce Locations
视图的空白处,选择新建,定义 Hadoop 集群的连接。
其中 location name 和 user name 随意
Map/Reduce(V2) Master
配置要与 hadoop 配置中 mapred-site.xml
内容保持一致
DFS Master
配置要与 core-site.xml
内容保持一致
点击 Finish 后 DFS Locations
下就会出现相关的链接信息
在配置正确的状况下,点开就能看到 hdfs 文件系统中的文件内容,即链接成功
项目 src
下建立 Package(本文名为 wit),Package 下建立 WordCount.java
类
粘贴以下 java 代码
package wit; 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(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } @SuppressWarnings("deprecation") 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(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
右键项目,依次 Run as ->Run Configurations...->Java Application
选 Java Application
后点击左上角的 New launch application
,配置 Main 标签参数。
填写Name(任起),Search...往下拉,找到 WordCount,肯定。
配置 Arguements
(此部分图来自https://blog.csdn.net/u010185...)
右键 WordCount 类,选择 Build Path -> configure build path
添加 jar 包
点击 Add External JARs
将 hadoop 解压文件夹中相关的 jar 包所有添加进来
配置完成,便可点击 Run 来运行 WordCount 类
运行结果会生成在前面配置的 hdfs 文件系统的指定目录中,能够直接在 Eclipse 中查看
原文来自 陈十一的博客