先将 最下面的的“旺旺大礼包”下载并解压
0.环境变量配置 在环境变量中添加
HADOOP_HOME ------>Hadoop 解压后的目录
HADOOP_USER_NAME ----->集群中hadoop文件创建的所有者 (windows 和linux中的用户名称不同时使用)
将HADOOP_HOME加入PATH中
(注意 JAVA_HOME 目录不要有空格)
然后将集群的主节点的hosts 添加到windows的hosts 中(大礼包中有hosts)
如何修改hosts参考:https://jingyan.baidu.com/article/425e69e6e479a2be15fc16e1.html
1. 将eclipse-hadoop 的插件复制到eclipse->dropins目录下 然后重启eclipse
2. 打开eclipse中的map-Reduce 视图
3 在eclipse windows->preferences -> Hadoop map/reduce 中指向解压后的Hadoop文件
4 配置集群环境
(1)填写MapReduce 的端口 默认是50020
(2)填写hdfs的端口 在core-site.xml 中的 fs.defaultFS 值
5 查看hdfs 中的文件
注意 hdfs中文件改动后需要disconnected &refesh后可以查看
6新建map/reduce 工程
new class --->WordCounts
将集群中五个文件拷贝到src中
7 (实验室的不用做) 增加mapred-site.xml中的代码 消除异常(没有截图记录。。。)
<property>
<name>mapred.remote.os</name>
<value>Linux</value>
<description>Remote MapReduce framework's OS, can be either Linux or Windows</description>
</property>
<property>
<name>mapreduce.app-submission.cross-platform</name>
<value>true</value>
</property>
8 复制 WordCount 源码 到wordCounts
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 WordCounts {
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);
}
//注意1:
// conf.set("mapred.jar", "e://hadoop-jar//wordcount.jar");
Job job = new Job(conf, "word count");
job.setJarByClass(WordCounts.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);
}
}
9 export 成一个jar包 放在一个目录中 这里放在了
e://hadoop-jar//
(可以将这个jar包传到集群中 使用hadoop jar 命令运行)
10 将8中的注意1 的 注释释放开
conf.set("mapred.jar", "e://hadoop-jar//wordcount.jar");
如果不执行此步骤会报异常
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class test.WordCounts$TokenizerMapper not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2197)
at org.apache.hadoop.mapreduce.task.JobContextImpl.getMapperClass(JobContextImpl.java:187)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:747)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164)
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:1746)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)
Caused by: java.lang.ClassNotFoundException: Class test.WordCounts$TokenizerMapper not found
at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:2101)
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195)
... 8 more
11 配置运行参数
第一个参数是输入路径
第二个参数是输出路径(输出路径必须要是不存在 不然会报异常)
12 run on hadoop
13 查看 输出路径的文件
最后附上我的旺旺大礼包:
旺旺大礼包
链接:https://pan.baidu.com/s/1eT1IXNG 密码:8a3l