鼓捣了两天的mapreduce join操作。觉得收获很大所以写了三篇博客。然后的话在网上找的资料都是有瑕疵的 不能运行的代码。 然后我这边 的代码都是经过测试的。不过还是要感谢 网上的大佬分享资料 。
orders.txt:
1001,20170710,p0001,1
1002,20170710,p0001,3
1003,20170710,p0001,3
1004,20170710,p0002,1
products.txt:
p0001,xiaomi,001
p0002,chuizi,001
最终结果:
p0001 xiaomi 1003 20170710 3
p0001 xiaomi 1002 20170710 3
p0001 xiaomi 1001 20170710 1
p0002 chuizi 1004 20170710 1
附上我的代码:
package com.lmz;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.v2.app.webapp.App;
import org.apache.hadoop.util.GenericOptionsParser;
public class Tpou {
public static class joinMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text outKey = new Text();
private Text outValue = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] split = line.split(",");
FileSplit inputSplit = (FileSplit) context.getInputSplit();
String name = inputSplit.getPath().getName();
//System.out.println(name);
//两个文件 在一个 mapper 中处理
//通过文件名判断是那种数据
if (name.contains("products")) {
//取商品ID 作为 输出key 和 商品名称 作为 输出value,即 第0、1 的数据
outKey.set(split[0]);
outValue.set("product#" + split[1]);
context.write(outKey, outValue);
} else {
//取商品ID 作为 输出key 和 购买数量 作为 输出value,即 第2、3 的数据
outKey.set(split[2]);
outValue.set("order#" + split[0] + "\t" + split[1] + "\t" + split[3]);
context.write(outKey, outValue);
}
}
}
public static class joinReducer extends Reducer<Text, Text, Text, Text> {
private Text outValue = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
//用来存放:商品ID、商品名称
List<String> productsList = new ArrayList<String>();
//用来存放:商品ID、购买数量
List<String> ordersList = new ArrayList<String>();
for (Text text : values) {
//System.out.println("key="+key+" zvalue="+text);
String value = text.toString();
if (value.startsWith("product#")) {
productsList.add(value.split("#")[1]); //取出 商品名称
} else if (value.startsWith("order#")) {
ordersList.add(text.toString().split("#")[1].trim());
//System.out.println(ordersList);//取出商品的销量,trim()是删除头尾空白。
}
}
//System.out.println(ordersList.size());
for (int i =0;i<productsList.size();i++){
for (int j = 0;j<ordersList.size();j++){
context.write(key,new Text(productsList.get(i)+""+ordersList.get(j)));
}
}
//System.out.println(ordersList);
//int totalOrders = 0;
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
//conf.set("fs.defaultFS", "file:///");
File file1 = new File("E:\\data1\\out");
//Path path = new Path("E:\\data1\\out");
//FileSystem fileSystem = path.getFileSystem(conf);
if (file1.exists()) {
FileUtils.deleteDirectory(file1);
}
Job job = Job.getInstance(conf);
//设置job的各种属性
//作业名称
job.setJarByClass(Mymap.class); //搜索类
job.setInputFormatClass(TextInputFormat.class); //设置输入格式
job.setMapperClass(joinMapper.class);
job.setReducerClass(joinReducer.class);
//添加输入路径
FileInputFormat.setInputPaths(job, new Path("E:\\data1"));
;
//设置输出路径
FileOutputFormat.setOutputPath(job, new Path("E:\\data1\\out"));
//map输出类型
job.setOutputKeyClass(Text.class); //
job.setOutputValueClass(Text.class); //
job.waitForCompletion(true);
Boolean b = job.waitForCompletion(true);
System.out.println(b ? 0 : 1);
}
}