MapReduce(五)
MapReduce的(五)
1.MapReduce的多表关联查询。
根据文本数据格式。查询多个文本中的内容关联。查询。
2.MapReduce的多任务窜执行的使用
多任务的串联执行问题,主要是要建立controlledjob,然后建组管理起来。留意多线程因效率而导致执行结束时间不一致的问题。
-------------------------------------------------- -------------------------------------------------- ----------------------------
MapReduce的的的多表关联查询
数据:
ctoryname地址
北京红星1
深圳迅雷3
广州本田2
北京瑞星1
广州发展银行2
腾讯3
北京银行5
addressID地址名称
1北京
2广州
3深圳
4西安
代码:
包com.huhu.day05;
import java.io.IOException;
导入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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import com.huhu.day04.ProgenyCount;
/ **
* *厂名厂址为北京红星1
*
*地址addressID地址名称1北京
*
*从工厂选择factory.factoryname,address.addressname,地址在哪里
* factory.addressed = address.addressID
*
*流程1.读取这2个文件?1个mapreduce 2.mapper 2个:map--同时可以处理2个文件代码3.map输出kv k:id
* v:t1:北京红星1 k:id v:t2:1北京4.降低价值{t1:北京红
*星1,t2:1北京}
*
* @作者huhu_k
*
* /
公共类扩展ToolRunner implements Tool {
私人配置conf;
公共静态类MyMapper扩展Mapper <LongWritable,文本,文本,文本> {
@覆盖
protected void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException {
String [] line = value.toString()。split(“\ t”);
如果(line [0] .matches(“\\ d”)){
// k:1 v:t1:1:北京
context.write(new Text(line [0]),new Text(“t1”+ line [0] +“:”+ line [1]));
} else {
// k:1 v:t2:beijingredstar:1
context.write(new Text(line [1]),new Text(“t2”+ line [0] +“:”+ line [1])) ;
}
}
}
公共静态类MyReduce扩展减速器{
@覆盖
保护无效设置(上下文上下文)抛出IOException,InterruptedException {
context.write(new Text(“factoryname \ t \ t”),新文本(“地址名称”));
}
@覆盖
protected void reduce(Text key,Iterable <Text> values,Context context)
抛出IOException,InterruptedException {
String fsc =“”;
String addr =“”;
for(Text s:values){
String line = s.toString();
if(line.contains(“t1”)){
addr = line.split(“:”)[1];
} else if(line.contains(“t2”)){
fsc = line.split(“:”)[0];
}
}
if(!fsc.equals(“”)&&!addr.equals(“”)){
context.write(new Text(fsc),new Text(addr));
}
}
@覆盖
保护无效清理(上下文上下文)抛出IOException,InterruptedException {
}
}
公共静态无效的主要(字符串[]参数)抛出异常{
多重连接t = new MutipleJoin();
配置conf = t.getConf ();
String [] other = new GenericOptionsParser(conf,args).getRemainingArgs();
if(other.length!= 2){
System.err.println(“number is fail”);
}
int run = ToolRunner.run(conf,t,args);
System.exit(运行);
}
@覆盖
public Configuration getConf(){
if(conf!= null){
返回conf;
}
返回新的配置();
}
@覆盖
public void setConf(Configuration arg0){
}
@覆盖
公共诠释运行(字符串[]其他)抛出异常{
配置con = getConf();
Job job = Job.getInstance(con);
job.setJarByClass(ProgenyCount.class);
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
//默认分区
// job.setPartitionerClass(HashPartitioner.class);
job.setReducerClass(MyReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job,new Path(“hdfs:// ry-hadoop1:8020 / in / day05”));
Path path = new Path(“hdfs:// ry-hadoop1:8020 / out / day05.txt”);
FileSystem fs = FileSystem.get(getConf());
if(fs.exists(path)){
fs.delete(path,true);
}
FileOutputFormat.setOutputPath(job,path);
返回job.waitForCompletion(true)?0:1;
}
}
运行结果:
将有规律的数据进行关联查询。
二。MapReduce的的多任务窜改的使用
包com.huhu.day05;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
导入org.apache.hadoop.mapreduce.Mapper;
公共类WordCount_Mapper扩展映射器<LongWritable,Text,Text,IntWritable> {
private final IntWritable one = new IntWritable(1);
@覆盖
保护无效映射(LongWritable键,文本值,映射器<LongWritable,文本,文本,IntWritable> .Context上下文)
抛出IOException,InterruptedException {
String [] line = value.toString()。split(“”);
for(String s:line){
context.write(new Text(s),one);
}
}
}
WordCount_Reduce
包com.huhu.day05;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
公共类WordCount_Reducer扩展Reducer <Text,IntWritable,Text,IntWritable> {
@覆盖
protected void reduce(Text key,Iterable <IntWritable> values,Context context)
抛出IOException,InterruptedException {
int sum = 0;
for(IntWritable i:values){
sum + = i.get();
}
context.write(key,new IntWritable(sum));
}
}
Top10_Mapper
包com.huhu.day05;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
导入org.apache.hadoop.mapreduce.Mapper;
公共类Top10_Mapper扩展了Mapper <LongWritable,Text,Text,IntWritable> {
@覆盖
protected void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException {
String [] line = value.toString()。split(“\ t”);
context.write(new Text(line [0]),new IntWritable(Integer.parseInt(line [1])));
}
}
Top10_Reducer
包com.huhu.day05;
import java.io.IOException;
import java.util.TreeSet;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import com.huhu.day05.pojo.WordCount;
公共类Top10_Reducer扩展Reducer <Text,IntWritable,WordCount,NullWritable> {
private TreeSet <WordCount> set = new TreeSet <>();
@覆盖
protected void reduce(Text key,Iterable <IntWritable> values,Context context)
抛出IOException,InterruptedException {
for(IntWritable v:values){
System.err.println(v.toString()+“----- -----------”);
set.add(new WordCount(key.toString(),Integer.parseInt(v.toString())));
}
if(10 <set.size()){
set.remove(set.last());
}
}
@覆盖
保护无效清理(上下文上下文)抛出IOException,InterruptedException {
for(WordCount w:set){
context.write(w,NullWritable.get());
}
}
}
WordCountTop_Cuan
包com.huhu.day05;
导入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.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.jobcontrol.JobControl;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import com.huhu.day05.pojo.WordCount;
公共类WordCountTop_Cuan扩展ToolRunner实现工具{
私人配置con;
@覆盖
public配置getConf(){
如果(con!= null)
返回con;
返回新的配置();
}
@覆盖
public void setConf(Configuration arg0){
}
@覆盖
公共诠释运行(字符串[] arg0)抛出异常{
配置con = getConf();}
Job WordJob = Job.getInstance(con,“WordCount Job”);
WordJob.setJarByClass(WordCountTop_Cuan.class);
WordJob.setMapperClass(WordCount_Mapper.class);
WordJob.setMapOutputKeyClass(Text.class);
WordJob.setMapOutputValueClass(IntWritable.class);
WordJob.setReducerClass(WordCount_Reducer.class);
WordJob.setOutputKeyClass(WordCount.class);
WordJob.setOutputValueClass(NullWritable.class);
FileInputFormat.addInputPath(WordJob,new Path(“hdfs:// ry-hadoop1:8020 / in / ihaveadream.txt”));
Path path = new Path(“hdfs:// ry-hadoop1:8020 / out / Word_job.txt”);
FileSystem fs = FileSystem.get(getConf());
if(fs.exists(path)){
fs.delete(path,true);
}
FileOutputFormat.setOutputPath(WordJob,path);
Job TopJob = Job.getInstance(con,“Top10 Job”);
TopJob.setJarByClass(WordCountTop_Cuan.class);
TopJob.setMapperClass(Top10_Mapper.class);
TopJob.setMapOutputKeyClass(Text.class);
TopJob.setMapOutputValueClass(IntWritable.class);
TopJob.setReducerClass(Top10_Reducer.class);
TopJob.setOutputKeyClass(WordCount.class);
TopJob.setOutputValueClass(NullWritable.class);
FileInputFormat.addInputPath(TOPJOB,路径);
Path paths = new Path(“hdfs:// ry-hadoop1:8020 / out / Top_Job.txt”);
if(fs.exists(paths)){
fs.delete(paths,true);
}
FileOutputFormat.setOutputPath(TopJob,paths);
//重点
ControlledJob controlledWC = new ControlledJob(WordJob.getConfiguration());
ControlledJob controlledTP = new ControlledJob(TopJob.getConfiguration());
// JobTop依赖JobWC
controlledTP.addDependingJob(controlledWC);
//定义控制器
JobControl jobControl =新的JobControl(“WordCount和Top”);
jobControl.addJob(controlledWC);
jobControl.addJob(controlledTP);
线程线程=新线程(JobControl作业控制);
thread.start();
而{(jobControl.allFinished()!)
了了Thread.sleep(1000);
}
jobControl.stop();
返回0;
}
公共静态无效的主要(字符串[]参数)抛出异常{
WordCountTop_Cuan wc = new WordCountTop_Cuan();
配置conf = wc.getConf();
String [] other = new GenericOptionsParser(conf,args).getRemainingArgs();
int run = ToolRunner.run(conf,wc,other);
System.exit(运行);
}
}
运行结果:
我是在本地运行的,如果在Hadoop的的上运行输入命令
hadoop jar xxx.jar /in/xx.txt /out/Word_Job.txt /out/Top_Job.txt
此时Top_Job依赖于Word_Job
因为Top_Job的输入路径是Word_Job的输出路径。当线程只启动一个工作,Top_job等待Word_Job运行完,Top_Job开始运行。
MapReduce(五)的更多相关文章
- mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次
(总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html combiner是把同一个机器上的多个map的结果先聚合一次现重新实现一个: 思路 ...
- MapReduce(五) mapreduce的shuffle机制 与 Yarn
一.shuffle机制 1.概述 (1)MapReduce 中, map 阶段处理的数据如何传递给 reduce 阶段,是 MapReduce 框架中最关键的一个流程,这个流程就叫 Shuffle:( ...
- MongoDB各种查询操作详解
这篇文章主要介绍了MongoDB各种查询操作详解,包括比较查询.关联查询.数组查询等,需要的朋友可以参考下 一.find操作 MongoDB中使用find来进行查询,通过指定find的第一个参数可 ...
- mongoDB操作2
一.find操作 MongoDB中使用find来进行查询,通过指定find的第一个参数可以实现全部和部分查询. 1.查询全部 空的查询文档{}会匹配集合的全部内容.如果不指定查询文档,默认就是{}. ...
- 《Hadoop权威》学习笔记五:MapReduce应用程序
一.API的配置---Configuration类 API的配置:Hadoop提供了专门的API对资源进行配置,Configuration类的实例(在org.apache.hadoop.conf包)包 ...
- Hadoop(十五)MapReduce程序实例
一.统计好友对数(去重) 1.1.数据准备 joe, jon joe , kia joe, bob joe ,ali kia, joe kia ,jim kia, dee dee ,kia dee, ...
- Hadoop MapReduce编程 API入门系列之Crime数据分析(二十五)(未完)
不多说,直接上代码. 一共12列,我们只需提取有用的列:第二列(犯罪类型).第四列(一周的哪一天).第五列(具体时间)和第七列(犯罪场所). 思路分析 基于项目的需求,我们通过以下几步完成: 1.首先 ...
- 大数据入门第九天——MapReduce详解(五)mapJoin、GroupingComparator与更多MR实例
一.数据倾斜分析——mapJoin 1.背景 接上一个day的Join算法,我们的解决join的方式是:在reduce端通过pid进行串接,这样的话: --order ,,P0001, ,,P0001 ...
- MapReduce Design Patterns(chapter 3 (part 1))(五)
Chapter 3. Filtering Patterns 本章的模式有一个共同点:不会改变原来的记录.这种模式是找到一个数据的子集,或者更小,例如取前十条,或者很大,例如结果去重.这种过滤器模式跟前 ...
随机推荐
- std::function
参考资料 • cplusplus.com:http://www.cplusplus.com/reference/functional/function/ • cppreference.com:http ...
- 编译器将"+"转换成了StringBuilder类
MapReduce map100% Reduce 66% 卡死 如果你碰到map100%,reduce 66% 然后程序就貌似停止在这里了,可能是由于在Reduce类里使用了String造成的 根据一 ...
- laravel 比较好的资料地址 看云
https://www.kancloud.cn/curder/laravel/408497
- wysiwyg 富文本编辑器(附带图片上传功能)
Fist: 需要的文件 font 文件夹下面的也是需要的哟 Then: 引入文件 <link href="bootstrap/css/bootstrap.css" rel=& ...
- phpcms网页替换验证码功能 及 搜索功能
在使用phpcms替换网页的时候,除了正常的替换栏目.内容页等,其他的什么验证码啦,提交表单了,搜索功能了,这些在替换的时候可能会对一些默认文件有一些小小 的改变 下面就是自己在失败中成功的过程,最后 ...
- div “下沉”
最近在做一个计算器,按键整体布局如下: Div2,div3 display属性设置为inline-block.三个div “容器”没添加任何元素时,布局是符合预想的.添加上按键后,布局变成下面这样了: ...
- 20190408Linux权限管理week1_day5
权限概述 Linux系统一般将文件可存/取访问的身份分为3个类别:owner(拥有者).group(和所有者同组的用户).others(其他人,除了所有者,除了同组的用户以及除了超级管理员),且3种身 ...
- StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)
一.截取 StringUtils中常用的截取字符串的方法如下: substring(String str,int start) substring(String str,int start, in ...
- opencv学习之路(24)、轮廓查找与绘制(三)——凸包
一.简介 二.绘制点集的凸包 #include<opencv2/opencv.hpp> using namespace cv; void main() { //---绘制点集的凸包 Mat ...
- 原生js实现九宫格,全解析
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...