Eclipse 整合 Hadoop
-
首先下载eclipse
-
然后下载Hadoop包
-
将两者相联系
流程梳理
: 在MapReduce中,首先 存储的海量数据 会将数据 传输给不同的 map,map 会逐行读取文件,并单独完成 自己的 任务。map 完成后的数据 会传到 Barrier:Aggregatuon Shuffle,在 Shuffle这里会重新洗牌一下,也就是重新整理map刚刚整理的数据,将其中一样的数据或者相同的次数整理出来,
代码书写
: 我们会先创建一个 map 类型的包,然后创建三个类 : Mapper,Reducer,Driver。
1.Mapper 书写
:Mapper 的作用 :从一个输入分片中读取数据,至于它是如何具体的读取数据的,你可以简单的理解为他本身就带着这种方法。
1.1 Mapper 代码思路整理
: 首先是你创建的类继承(extends)Mapper以及它的四个属性,在 Mapper 中我们写 map 方法,map中的属性简单的记忆,就是map的(两个属性+mapper的四个属性).Context context。然后就是转化成 String 类型,再继续写逻辑代码就行了。
package com.biem.wc;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
//mapper四个泛型:Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
//分别代表的是:
//KEYIN -- map读取到每一行的一个偏移量
//VALUEIN -- 每一行的数据
//KEYOUT -- 输出的键类型
//VALUEOUT -- 键的值得类型
//在 mapper 中重写 map 的方法两个属性:
//LongWritable key Map函数的输入键
//Text value Map函数的输入值
public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
//接写来mapper中我们要重写一个map方法:里面有一个context参数,是将map后的结果写进去
//context.write
//mapper 中的 map() 则一般承担主要的处理工作
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
//转成string类型
String str = value.toString();
//读取每行数据,然后切分
String[] arr = str.split(" ");
//遍历
for (String s : arr) {
context.write(new Text(s), new LongWritable(1));
}
}
}
2.Reducer 书写
:Reducer 在这里起的作用属于洗牌的作用,前面 map 将数据进行分片统计,统计完的分片数据提交给 Reducer,Reduce 在将这些分片数据集合起来,然后重新整理在map整理好的数据。这相当于一个层级整理的关系,(怎么比较简单的理解呢,就相当于你要招兵买马,你派 map 将军们去各地招揽人才,map将军们招揽的各种人才上报给大将军,大将军再将这些人整理成谋士团,骑兵团…)
2.1 Reducer 代码思路整理
:这里 是你创建的类继承(extends)Reducer的四个属性, 然后在Reducer中写 reduce 方法,reduce 方法中(两个属性+Reducer 的四个属性).Context context,然后写逻辑代码就行了。
package com.biem.wc;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
//KEYIN -- 键的类型
//VALUEIN -- 值
//KEYOUT -- 键的类型
//VALUEOUT -- 值
@Override
protected void reduce(Text key, Iterable<LongWritable> arg1,
Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
//定义求和变量
long sum = 0;
//循环遍历求和
for (LongWritable count : arg1) {
sum += count.get();
}
context.write(key, new LongWritable(sum));
}
}
3.Driver 书写
package com.biem.wc;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCountDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//指定当前job执行的入口类
job.setJarByClass(WordCountDriver.class);
//指定mapper执行的类
job.setMapperClass(WordCountMapper.class);
//指定mapper的输入类型的类
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
//指定reduce执行的类
job.setReducerClass(WordCountReducer.class);
//指定reducer输出的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//指定文件所在路径及输出结果路径
FileInputFormat.setInputPaths(job, new Path("hdfs://ip:9000/查询的文件所在位置"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://ip:9000/result"));
//执行job
job.waitForCompletion(true);
}
}