public class SingleJoin {
static {
System.setProperty("hadoop.home.dir", "D:\\software\\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 {
if (key.get() == 0) return;
String line = value.toString();
String[] lineArr = line.split("\\s");
context.write(new Text(lineArr[0]), new Text("1:" + lineArr[1]));
context.write(new Text(lineArr[1]), new Text("2:" + lineArr[0]));
}
}
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> parentList = new ArrayList<>();
List<String> childList = new ArrayList<>();
//1.根据item_id,遍历对应的订单明细信息和商品信息,拆分到相应的集合中
for (Text value : values) {
String valueStr = value.toString();
String[] valueArr = valueStr.split(":");
if("1".equals(valueArr[0])) {
parentList.add(valueArr[1]);
} else {
childList.add(valueArr[1]);
}
}
//2.将订单明细列表和商品列表进行嵌套遍历
for (String child : childList) {
for (String parent : parentList) {
context.write(key, new Text(child + ":" + parent));
}
}
}
}
/**
* 驱动方法
* @param args
*/
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//0.初始化一个job
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "single_join");
job.setJarByClass(SingleJoin.class);
//1.输入文件
FileInputFormat.addInputPaths(job, args[0]);
//2.map并行计算
job.setMapperClass(MyMapper.class);
//3.shuffle流程(内部实现)
//4.reduce计算
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//5.输出文件
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);
}
//6.提交作业(总入口)
boolean result = job.waitForCompletion(true);
System.out.println(result ? 1 : 0);
}
}
child parent
Tom Lucy
Tom Jack
Jone Lucy
Jone Jack
Lucy Marry
Lucy Ben
Jack Alice
Jack Jesse
Terry Alice
Terry Jesse
Philip Terry
Philip Alma
Mark Terry
Mark Alma