一、环境准备
(1)本地搭建hadoop环境
将hadoop相关文件放到目录下如下
配置环境变量
在终端输入hadoop version 如下
(2)搭建maven
conf下setting文件使用国内镜像
nexus-aliyun central Nexus aliyun http://maven.aliyun.com/nexus/content/groups/public
环境变量
在终端下输入mvn -v
二、IDEA编写
(1)新建一个Maven 工程
界面如下:
(2)修改配置文件位置
(3)编辑pom.xml 文件 代码如下
4.0.0 org.example MapReduce1.0 3.1.3 org.apache.hadoop hadoop-client${hadoop.version} org.apache.hadoop hadoop-common${hadoop.version} org.apache.hadoop hadoop-hdfs${hadoop.version}
然后点击maven 进行下载 如下
(3)在data下新建一个wc.txt文件 内容如下
(4) 新建MRMapper,java
代码如下:
package MR.wc; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class MRMapper extends Mapper{ Text k = new Text(); IntWritable v = new IntWritable(1); @Override protected void setup(Context context) throws IOException, InterruptedException { System.out.println("Mapper.setup----------"); } @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 1 获取一行 String line = value.toString(); // 2 切割 String[] words = line.split(" "); // 3 输出 for (String word : words) { k.set(word); context.write(k, v); } } @Override protected void cleanup(Context context) throws IOException, InterruptedException { System.out.println("Mapper.cleanup"); } }
(5)新建MRReducer.java
package MR.wc; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class MRReducer extends Reducer{ int sum; IntWritable v = new IntWritable(); @Override protected void setup(Context context) throws IOException, InterruptedException { System.out.println("Mapper.setup__________"); } @Override protected void reduce(Text key, Iterable values,Context context) throws IOException, InterruptedException { // 1 累加求和 sum = 0; for (IntWritable count : values) { sum += count.get(); } // 2 输出 v.set(sum); context.write(key, v); } @Override protected void cleanup(Context context) throws IOException, InterruptedException { System.out.println("Mapper.cleanup__________"); } }



