Reduce端join操作问题
Map端join操作
3.1 概述
适用于关联表中有小表的情形.
使用分布式缓存,可以将小表分发到所有的map节点,这样,map节点就可以在本地对自己所读到的大表数据进行join并输出最终结果,可以大大提高join操作的并发度,加快处理速度.
3.2 实现步骤
先在mapper类中预先定义好小表,进行join
引入实际场景中的解决方案:一次加载数据库或者用
Step 1:定义Mapper
package map_join;
import org.apache.hadoop.fs.FSDataInputStream;
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.Mapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
public class MapJoinMapper extends Mapper<LongWritable, Text,Text,Text> {
private HashMap<String, String> map = new HashMap<>();
//第一件事情:将分布式缓存的小表数据读取到本地map集合(只需要做一次)
@Override
protected void setup(Context context) throws IOException, InterruptedException {
//1.获取分布式缓存文件列表
URI[] cacheFiles = context.getCacheFiles();
//2.获取指定的缓存文件系统(Filesystem)
FileSystem fileSystem = FileSystem.get(cacheFiles[0], context.getConfiguration());
//3.获取文件的输入流
FSDataInputStream inputstream = fileSystem.open(new Path(cacheFiles[0]));
//4.读取文件的内容,并将数据存入Map集合
//4.1将字节输入流转为字符缓存流 ,FSDataInputStream---->BufferedReader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream));
//4.2读取小表文件内容,以行为单位,并将读取的数据存入map集合
String line =null;
while((line = bufferedReader.readLine())!=null){
String[] split = line.split(",");
map.put(split[0],line);
}
//5.关闭流
bufferedReader.close();
fileSystem.close();
}
//第二件事情:对大表处理的业务逻辑,而且要实现大表和小表的join操作
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//1:从行文本数据中获取商品id:p0001,p0002,p0003 得到了K2
String[] split = value.toString().split(",");
String productid =split[2];
//2:在map集合中,将商品ID作为键,获取值(商品的行文本数据),将value和值拼接得到v2
String productLine = map.get(productid);
String ValueLine=productLine+"\t"+value.toString(); //v2
//3:将k2和v2写入上下文
context.write(new Text(productid),new Text(ValueLine));
}
}
Step 2:定义主类
package map_join;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.net.URI;
public class JobMain extends Configured implements Tool {
@Override
public int run(String[] strings) throws Exception {
//1.获取job对象
Job job = Job.getInstance(super.getConf(), "map_join_job");
//2.设置Job对象(将小表写入缓存中)
//将小表放在分布式缓存中
job.addCacheFile(new URI("hdfs://node01:8020/cache_file/product.txt"));
//第一步:设置输入类和输入路径
job.setInputFormatClass(TextInputFormat.class);
TextInputFormat.addInputPath(job,new Path("file:///D:\\input\\map_join_input"));
//第二步:设置map类和数据类型
job.setMapperClass(MapJoinMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//第三,四,五,六步
//第七步:设置Reducer 类和数据对象
//第八步 :设置输出类和输出路径
job.setOutputFormatClass(TextOutputFormat.class);
TextOutputFormat.setOutputPath(job,new Path("file:///D:\\out\\map_join"));
//3.等待job任务结束
boolean b = job.waitForCompletion(true);
return b ?0:1;
}
public static void main(String[] args) throws Exception {
Configuration configuration =new Configuration();
//启动job任务
int run = ToolRunner.run(configuration, new JobMain(), args);
System.exit(run);
}
}