需求:将商品表与订单表关联
order.txt
201801 01 1
201802 02 2
201803 03 3
201804 01 4
201805 02 5
201806 03 6
......
pd.txt
01 小米
02 华为
03 格力
......
当数据量比较小的时候,可以使用MapJoin,数据量较大是,使用ReduceJoin
1:定义TableBean类
package ReduceJoin;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@AllArgsConstructor
@NoArgsConstructor
public class TableBean implements Writable {
//订单id、产品id、产品数量、产品名、表标记
private String orderId;
private String pId;
private int amount;
private String pName;
private String flag;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getPid() {
return pId;
}
public void setPid(String pId) {
this.pId = pId;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getPname() {
return pName;
}
public void setPname(String pName) {
this.pName = pName;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@Override //序列化
public void write(DataOutput out) throws IOException {
out.writeUTF(orderId);
out.writeUTF(pId);
out.writeInt(amount);
out.writeUTF(pName);
out.writeUTF(flag);
}
@Override //反序列化
public void readFields(DataInput in) throws IOException {
this.orderId = in.readUTF();
this.pId = in.readUTF();
this.amount = in.readInt();
this.pName = in.readUTF();
this.flag = in.readUTF();
}
@Override
public String toString() {
return orderId + "\t" + pName + "\t" + amount;
}
}
2:定义TableMapper类
package ReduceJoin;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import java.io.IOException;
/**
* LongWritable,Text 偏移量,数据文本
* Text,Bean pid,Bean
*/
public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean> {
Text k = new Text();
TableBean bean = new TableBean();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//通过context找到InputSplit的子类FileSplit 注:转类型
FileSplit split = (FileSplit) context.getInputSplit();
//找到文件名,用于判断两个文件
String name = split.getPath().getName();
String line = value.toString();
// 0 按订单表处理 orderid、pid、amount
if (name.startsWith("order")) {
String[] fields = line.split("\t");
bean.setOrderId(fields[0]);
bean.setPid(fields[1]);
bean.setAmount(Integer.parseInt(fields[2]));
bean.setPname("");
bean.setFlag("0");
k.set(fields[1]);
}else {
// 1 按产品表处理
String[] fields = line.split("\t");
bean.setPid(fields[0]);
bean.setPname(fields[1]);
bean.setAmount(0);
bean.setOrderId("");
bean.setFlag("1");
k.set(fields[0]);
}
context.write(k,bean);
}
}
3:定义TableReducer类
package ReduceJoin;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TableReducer extends Reducer<Text, TableBean,TableBean, NullWritable> {
@Override
protected void reduce(Text key, Iterable<TableBean> values, Context context) throws IOException, InterruptedException {
List<TableBean> orderBeans = new ArrayList<>();
TableBean pdBean = new TableBean();
for(TableBean bean : values){
// 0 是订单表
if("0".equals(bean.getFlag())){
TableBean orderBean = new TableBean();
try {
//bean copy to orderBean
BeanUtils.copyProperties(orderBean,bean);
} catch (Exception e) {
e.printStackTrace();
}
orderBeans.add(orderBean);
}else {
// 1 是产品表
try {
BeanUtils.copyProperties(pdBean,bean);
} catch (Exception e) {
e.printStackTrace();
}
}
}
for(TableBean bean : orderBeans) {
bean.setPname(pdBean.getPname());
context.write(bean,NullWritable.get().get());
}
}
}
4:定义TableDriver类
package ReduceJoin;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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;
import java.io.IOException;
public class TableDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
args = new String[]{"E:\\bigdata_code\\reducejoin","E:\\bigdata_code\\reducejoin\\out"};
//获取配置信息,job对象实例
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//指定Jar包所在的路径
job.setJarByClass(TableDriver.class);
//指定Map类和Map输出数据的KV类型
job.setMapperClass(TableMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(TableBean.class);
//指定reduce类和reduce输出数据类型
job.setReducerClass(TableReducer.class);
job.setOutputKeyClass(TableBean.class);
job.setOutputValueClass(NullWritable.class);
//指定输入输出类型
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//提交
job.waitForCompletion(true);
}
}
输出结果:
201804 小米 4
201801 小米 1
201805 华为 5
201802 华为 2
201806 格力 6
201803 格力 3
......