需要的工具如下:
VMware14.0(hadoop集群)
Hadoop-2.6.0
Eclipse
所需插件:
hadoop-eclipse-plugin-2.6.0.jar
winutils.exe
hadoop.exp
hadoop.dll
hadoop-2.6.0在windows10下安装
1.hadoop-2.6.0解压在任意目录下,然后把winutils.exe,hadoop.exe.hadoop.dll放在hadoop的bin目录下
2.设置环境变量
添加HADOOP_HOME=D:\hadoop-2.6.0;
添加Path=%HADOOP_HOME%\bin;
3.将hadoop.dll复制到C盘→windows→system32目录下
4.将插件hadoop-eclipse-plugin-2.6.0.jar放在eclipse的plugins目录下
Eclipse上的相关配置
打开eclipse后会看到DFS Locations,表示hadoop上的相关配置完成
1.打开window→preferences→hadoop Map/Reduce,然后添加hadoop的安装目录
2.打开window→show views→others找到MapReduce Tools打开
3.新建一个hadoop Location,填写如下内容
4.执行./start-dfs.sh,启动hdfs,然后输入jps看是否启动成功,
在eclipse里能看到如下目录表示连接成功
5.上传文件到hdfs上,先进入opt/hadoop-2.6.0/bin下执行命令
./hadoop fs -put /opt/hadoop-2.6.0/x.txt /user/root/input
或者使用
./hdfs dfs -put /opt/hadoop-2.6.0/x.txt /user/root/input
执行命令查看文件是否上传成功
./hadoop fs -ls /input
6.Wordcount代码如下:
package zxy;
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);
}
}
找到运行配置,新建启动设置
所有都设置好以后点击运行方式,或者直接在启动设置页面点击运行
运行以后得到如下结果
到此整个hadoop连接eclipse以及wordcount代码的演示就结束了。
安装hadoop所需的插件地址:
//download.csdn.net/download/weixin_44988131/12033737