MapReduce中多表合并案例
[infobox title="需求分析"]
订单数据order.txt
商品数据pd.txt
将商品信息表中数据根据商品pid合并到订单数据表中。
[/infobox]
[infobox title="code01(有数据倾斜风险)"]
通过将关联条件作为map输出的key,将两表满足join条件的数据并携带数据所来源的文件信息,发往同一个reduce task,在reduce中进行数据的串联。
这种方式中,合并的操作是在reduce阶段完成,reduce端的处理压力太大,map节点的运算负载则很低,资源利用率不高,且在reduce阶段极易产生数据倾斜
ruaDriver
TableBean
TableMapper
TableReducer
[/infobox]
[infobox title="map端表合并(Distributedcache)"]
适用于关联表中有小表的情形;
可以将小表分发到所有的map节点,这样,map节点就可以在本地对自己所读到的大表数据进行合并并输出最终结果,可以大大提高合并操作的并发度,加快处理速度。
[successbox title="ruaDriver"]
package com.kami.demo04;
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;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
*/
public class ruaDriver {
public static void main(String[] args) throws Exception {
args = new String[]{"data\\d03\\order.txt","output\\d04"};
//获取job信息
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
//设置加载jar包路径
job.setJarByClass(ruaDriver.class);
//关联map
job.setMapperClass(DistributedCacheMapper.class);
//设置最终输出数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//设置输入输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//加载缓存数据
// job.addCacheFile(new URI("file:///e:/inputcache/pd.txt"));
// DistributedCache.addCacheFile(new URI("file:///C:/tool/dev/JAVA/2020.06/day0616_work01/data/d03/pd.txt"),configuration);
// DistributedCache.addLocalFiles(configuration,"data/d03/pd.txt");
//map端join的逻辑不需要reduce阶段,设置reducetask数量为0
job.setNumReduceTasks(0);
//提交
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
[/successbox]
[successbox title="DistributedCacheMapper"]
package com.kami.demo04;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
* 读取缓存文件数据
*/
public class DistributedCacheMapper extends Mapper {
Map pdMap = new HashMap<>();
@Override
protected void setup(Mapper.Context context) throws IOException, InterruptedException {
// context.getCacheFiles()
//获取缓存的文件
// URI[] cacheFiles = DistributedCache.getCacheFiles(context.getConfiguration());
System.out.println(context.getCacheFiles().length);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("data\\d03\\pd.txt"), "UTF-8"));
String line;
while (StringUtils.isNotEmpty(line = reader.readLine())) {
//切割
String[] fields = line.split("\t");
//缓存数据到集合
pdMap.put(fields[0], fields[1]);
}
//关流
reader.close();
}
Text k = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//获取一行
String line = value.toString();
//截取
String[] fields = line.split("\t");
//获取产品id
String pId = fields[1];
//获取商品名称
String pdName = pdMap.get(pId);
//拼接
k.set(line + "\t" + pdName);
//写出
context.write(k, NullWritable.get());
}
}
[/successbox]
[/infobox]
from:https://www.cnblogs.com/frankdeng/p/9256248.html
Comments NOTHING