mapreduce
分三部分
- mapper
- reducer
- driver
仿写 wordCount
/**
* <h3>study-all</h3>
*
* <p></p>
*
* @Author zcz
* @Date 2020-03-31 20:48
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private Text text = new Text();
private IntWritable count = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split(" ");
for (String word : words) {
text.set(word);
context.write(text, count);
}
}
}
/**
* <h3>study-all</h3>
*
* <p></p>
*
* @Author zcz
* @Date 2020-03-31 21:19
*/
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable value = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable i : values) {
sum = sum+ i.get();
}
value.set(sum);
context.write(key, value);
}
}
/**
* <h3>study-all</h3>
*
* <p></p>
*
* @Author zcz
* @Date 2020-03-31 21:33
*/
public class WordCountDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建 job
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//指定 job 启动的 main class
job.setJarByClass(WordCountDriver.class);
//指定 job 运行的 mapper 和 reducer
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
//指定 mapper 的输出
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//指定reducer 的输出
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//指定文件的输入和输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//job 开始执行
boolean result = job.waitForCompletion(true);
System.out.println(result?0:1);
}
}
- 项目配置 maven 打包 jar
<!-- pom 文件增加打包插件-->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--指定 main 类 全限定名-->
<mainClass>com.zcz.study.hadoop.mapreduce.WordCountDriver</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
mvn clean package
hadoop jar customerWorkCount.jar com.zcz.study.hadoop.mapreduce.WordCountDriver /user/zcz/input/ /user/zcz/output/
序列化
- hadoop 中 mapper 与 reducer 输出的对象都需要是 hadoop 序列化的对象 实现Writable
- mapper 与 reducer 输出的 key 需要是可比较的 实现 Comparable
- Mapeduce 中对象可实现WritableComparable接口 同时支持序列化和比较
- mapper过程会对 key 进行排序, 所以 value 可以不需要比较
/**
* <h3>study-all</h3>
*
* <p></p>
*
* @Author zcz
* @Date 2020-04-01 20:53
*/
public class Flow implements WritableComparable<Flow> {
private Long upFlow;
private Long downFlow;
private Long sumFlow;
public Long getUpFlow() {
return upFlow;
}
public void setUpFlow(Long upFlow) {
this.upFlow = upFlow;
}
public Long getDownFlow() {
return downFlow;
}
public void setDownFlow(Long downFlow) {
this.downFlow = downFlow;
}
public Long getSumFlow() {
return sumFlow;
}
public void setSumFlow(Long sumFlow) {
this.sumFlow = sumFlow;
}
public Flow(Long upFlow, Long downFlow) {
this.upFlow = upFlow;
this.downFlow = downFlow;
this.sumFlow = this.upFlow + this.downFlow;
}
public Flow() {
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(upFlow);
out.writeLong(downFlow);
out.writeLong(sumFlow);
}
@Override
public void readFields(DataInput in) throws IOException {
this.upFlow = in.readLong();
this.downFlow = in.readLong();
this.sumFlow = in.readLong();
}
@Override
public String toString() {
return upFlow + "\t" +
downFlow + "\t" +
sumFlow ;
}
@Override
public int compareTo(Flow o) {
return this.sumFlow> o.getSumFlow()?1:-1;
}
}
/**
* <h3>study-all</h3>
*
* <p></p>
*
* @Author zcz
* @Date 2020-04-01 20:59
*/
public class FlowMapper extends Mapper<LongWritable, Text, Text, Flow> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] strs = value.toString().split("\t");
Flow flow = new Flow(Long.valueOf(strs[3]), Long.valueOf(strs[4]));
context.write(new Text(strs[1]), flow);
}
}
/**
* <h3>study-all</h3>
*
* <p></p>
*
* @Author zcz
* @Date 2020-04-01 21:09
*/
public class FlowReducer extends Reducer<Text, Flow,Text, Flow> {
@Override
protected void reduce(Text key, Iterable<Flow> values, Context context) throws IOException, InterruptedException {
Long up = 0L;
Long down = 0L;
Long sum = 0L;
for (Flow flow : values) {
up = up + flow.getUpFlow();
down = down + flow.getDownFlow();
sum = sum + flow.getSumFlow();
}
context.write(key, new Flow(up, down));
}
}
/**
* <h3>study-all</h3>
*
* <p></p>
*
* @Author zcz
* @Date 2020-04-01 21:15
*/
public class SerializeDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(SerializeDriver.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Flow.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Flow.class);
job.setMapperClass(FlowMapper.class);
job.setReducerClass(FlowReducer.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean result = job.waitForCompletion(true);
System.out.println(result? 0 : 1 );
}
}
- java 类型对应 mapred 序列化类型
Java类型 | Hadoop Writable类型 |
---|---|
boolean | BooleanWritable |
byte | ByteWritable |
int | IntWritable |
float | FloatWritable |
long | LongWritable |
double | DoubleWritable |
String | Text |
map | MapWritable |
array | ArrayWritable |