user数据:
u001,senge,18,angelababy
u002,laozhao,48,ruhua
u003,xiaoxu,16,chunge
u004,laoyang,28,zengge
u005,nana,14,huangbo
order订单数据:
order001,u001
order002,u001
order003,u005
order004,u002
order005,u003
order006,u004
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class One {
static {
System.setProperty("hadoop.home.dir","D:\\soft\\hadoop\\hadoop-2.9.2");
}
public static class MyMapper extends Mapper<LongWritable, Text,Text,Text>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String FileName = ((FileSplit) context.getInputSplit()).getPath().getName();
String line = value.toString();
String[] lineArr = line.split(",");
if ("order.txt".equals(FileName)){
context.write(new Text(lineArr[1]),new Text("1,"+lineArr[0]));
}else {
context.write(new Text(lineArr[0]),new Text("2," +lineArr[1]+","+lineArr[2]+","+lineArr[3]));
}
}
}
public static class MyReducer extends Reducer<Text,Text,Text,Text>{
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
List<String> orderDetailList = new ArrayList<>();
List<String> itemInfoList = new ArrayList<>();
for (Text value:values){
String valueStr = value.toString();
String[] valueArr = valueStr.split(",");
if ("1".equals(valueArr[0])){
orderDetailList.add(valueArr[1]);
}else {
itemInfoList.add(valueStr.substring(2));
}
}
for (String iteminfo:itemInfoList){
for (String orderDatail:orderDetailList){
context.write(new Text(orderDatail+","+key+","+iteminfo),new Text(""));
}
}
}
}
public static void main(String[] args) throws Exception {
//初始化一个作业
Configuration conf = new Configuration();
//给作业取个名字
Job job = Job.getInstance(conf, "One");
//输入文件路径
FileInputFormat.addInputPaths(job,args[0]);
//map并行计算
job.setMapperClass(MyMapper.class);
//shuffle流程
//reduce计算
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//输出文件路径
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//判断文件是否存在
FileSystem fs = FileSystem.get(conf);
if (fs.exists(new Path(args[1]))){
fs.delete(new Path(args[1]),true);
}
//判断是否成功
boolean b = job.waitForCompletion(true);
System.out.println(b ? 1 : 0);
}
}
结果:
order002,u001,senge,18,angelababy
order001,u001,senge,18,angelababy
order004,u002,laozhao,48,ruhua
order005,u003,xiaoxu,16,chunge
order006,u004,laoyang,28,zengge
order003,u005,nana,14,huangbo