将user表、group表、order表关;(类似于多表关联查询)

测试准备:

首先同步时间,然后 开启hdfs集群,开启yarn集群;在本地"/home/hadoop/test/"目录创建user表、group表、order表的文件;

user文件:

group文件:

order文件:

测试目标:

得到3张表关联后的结果;

测试代码

一定要把握好输出键值的类型,否则有可能造成有输出目录,但是没有文件内容的问题;

package com.mmzs.bigdata.yarn.mapreduce;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit; public class UserGroupMapper01 extends Mapper<LongWritable, Text, Text, Text> { private Text outKey;
private Text outValue; @Override
protected void setup(Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
outKey = new Text();
outValue = new Text();
} @Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
FileSplit fp = (FileSplit) context.getInputSplit();
String fileName = fp.getPath().getName(); String line = value.toString();
String[] fields = line.split("\\s+"); String keyStr = null;
String valueStr = null;
if ("group".equalsIgnoreCase(fileName)) {
keyStr = fields[0];
valueStr = new StringBuilder(fields[1]).append("-->").append(fileName).toString();
} else {
keyStr = fields[2];
//加“-->”;后以此标识符作为分割符,进行文件区分
valueStr = new StringBuilder(fields[0]).append("\t").append(fields[1]).append("-->").append(fileName).toString();
} outKey.set(keyStr);
outValue.set(valueStr);
context.write(outKey, outValue); } @Override
protected void cleanup(Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
outKey = null;
outValue = null;
} }

UserGroupMapper01

 package com.mmzs.bigdata.yarn.mapreduce;

 import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class UserGroupReducer01 extends Reducer<Text, Text, Text, Text> { private Text outValue; @Override
protected void setup(Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
outValue = new Text();
} @Override
protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
Iterator<Text> its = values.iterator(); String masterRecord = null;
List<String> slaveRecords = new ArrayList<String>(); //拆分出主表记录和从表记录
while (its.hasNext()) {
String[] rowAndFileName = its.next().toString().split("-->");
if (rowAndFileName[1].equalsIgnoreCase("group")) {
masterRecord = rowAndFileName[0];
continue;
}
slaveRecords.add(rowAndFileName[0]);
} for (String slaveRecord : slaveRecords) {
String valueStr = new StringBuilder(masterRecord).append("\t").append(slaveRecord).toString();
outValue.set(valueStr);
context.write(key, outValue);
} } @Override
protected void cleanup(Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
outValue = null;
} }

UserGroupReducer01

 package com.mmzs.bigdata.yarn.mapreduce;

 import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException; import 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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* @author hadoop
*
*/
public class UserGroupDriver01 { private static FileSystem fs;
private static Configuration conf;
static {
String uri = "hdfs://master01:9000/";
conf = new Configuration();
try {
fs = FileSystem.get(new URI(uri), conf, "hadoop");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Job ugJob01 = getJob(args);
if (null == ugJob01) {
return;
}
//提交Job到集群并等待Job运行完成,参数true表示将Job运行时的状态信息返回到客户端
boolean flag = false;
flag = ugJob01.waitForCompletion(true);
System.exit(flag?0:1);
} /**
* 获取Job实例
* @param args
* @return
* @throws IOException
*/
public static Job getJob(String[] args) throws IOException {
if (null==args || args.length<2) return null;
//放置需要处理的数据所在的HDFS路径
Path inputPath = new Path(args[0]);
//放置Job作业执行完成之后其处理结果的输出路径
Path outputPath = new Path(args[1]);
//主机文件路径
Path userPath = new Path("/home/hadoop/test/user");
Path groupPath = new Path("/home/hadoop/test/group"); //如果输入的集群路径存在,则删除
if (fs.exists(inputPath)) {
fs.delete(inputPath, true);//true表示递归删除
}
if (fs.exists(outputPath)) {
fs.delete(outputPath, true);//true表示递归删除
} //创建并且将数据文件拷贝到创建的集群路径
fs.mkdirs(inputPath);
fs.copyFromLocalFile(false, false, new Path[]{userPath, groupPath}, inputPath); //获取Job实例
Job ugJob01 = Job.getInstance(conf, "UserGroupJob01");
//设置运行此jar包入口类
//ugJob01的入口是WordCountDriver类
ugJob01.setJarByClass(UserGroupDriver01.class);
//设置Job调用的Mapper类
ugJob01.setMapperClass(UserGroupMapper01.class);
//设置Job调用的Reducer类(如果一个Job没有Reducer则可以不调用此条语句)
ugJob01.setReducerClass(UserGroupReducer01.class); //设置MapTask的输出键类型
ugJob01.setMapOutputKeyClass(Text.class);
//设置MapTask的输出值类型
ugJob01.setMapOutputValueClass(Text.class); //设置整个Job的输出键类型(如果一个Job没有Reducer则可以不调用此条语句)
ugJob01.setOutputKeyClass(Text.class);
//设置整个Job的输出值类型(如果一个Job没有Reducer则可以不调用此条语句)
ugJob01.setOutputValueClass(Text.class); //设置整个Job需要处理数据的输入路径
FileInputFormat.setInputPaths(ugJob01, inputPath);
//设置整个Job计算结果的输出路径
FileOutputFormat.setOutputPath(ugJob01, outputPath); return ugJob01;
} }

UserGroupDriver01

 package com.mmzs.bigdata.yarn.mapreduce;

 import java.io.IOException;

 import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit; public class UserGroupMapper02 extends Mapper<LongWritable, Text, Text, Text> { private Text outKey;
private Text outValue; @Override
protected void setup(Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
outKey = new Text();
outValue = new Text();
} @Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
FileSplit fp = (FileSplit) context.getInputSplit();
String fileName = fp.getPath().getName(); String line = value.toString();
String[] fields = line.split("\\s+"); String keyStr = fields[2];
String valueStr = null;
valueStr = new StringBuilder(fields[0]).append("\t").append(fields[1]).append("\t").append(fields[3]).append("-->").append(fileName).toString(); outKey.set(keyStr);
outValue.set(valueStr);
context.write(outKey, outValue); } @Override
protected void cleanup(Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
outKey = null;
outValue = null;
} }

UserGroupMapper02

 package com.mmzs.bigdata.yarn.mapreduce;

 import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class UserGroupReducer02 extends Reducer<Text, Text, Text, Text> { private Text outValue; @Override
protected void setup(Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
outValue = new Text();
} @Override
protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String masterRecord = null;
List<String> slaveRecords = new ArrayList<String>(); //拆分出主表记录和从表记录
Iterator<Text> its = values.iterator();
while (its.hasNext()) {
String[] rowAndFileName = its.next().toString().split("-->");
if (!rowAndFileName[1].equalsIgnoreCase("order")) {
masterRecord = rowAndFileName[0];
continue;
}
slaveRecords.add(rowAndFileName[0]);
} for (String slaveRecord : slaveRecords) {
String valueStr = new StringBuilder(masterRecord).append("\t").append(slaveRecord).toString();
outValue.set(valueStr);
context.write(key, outValue);
} } @Override
protected void cleanup(Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
outValue = null;
} }

UserGroupReducer02

 package com.mmzs.bigdata.yarn.mapreduce;

 import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* @author hadoop
*
*/
public class UserGroupDriver02 { private static FileSystem fs;
private static Configuration conf;
static {
String uri = "hdfs://master01:9000/";
conf = new Configuration();
try {
fs = FileSystem.get(new URI(uri), conf, "hadoop");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Job ugJob02 = getJob(new String[] {args[1], args[2]});
if (null == ugJob02) {
return;
}
//提交Job到集群并等待Job运行完成,参数true表示将Job运行时的状态信息返回到客户端
boolean flag = false;
flag = ugJob02.waitForCompletion(true);
System.exit(flag?0:1);
} /**
* 获取Job实例
* @param args
* @return
* @throws IOException
*/
public static Job getJob(String[] args) throws IOException {
if (null==args || args.length<2) return null;
//放置需要处理的数据所在的HDFS路径
Path inputPath = new Path(args[1]);
//放置Job作业执行完成之后其处理结果的输出路径
Path outputPath = new Path(args[2]);
//主机文件路径
Path orderPath = new Path("/home/hadoop/test/order"); //输入的集群路径存在,在第一次已创建
if (!fs.exists(inputPath)) return null;
if (fs.exists(outputPath)) {
fs.delete(outputPath, true);//true表示递归删除
} //将数据文件拷贝到创建的集群路径
fs.copyFromLocalFile(false, false, orderPath, inputPath); //获取Job实例
Job ugJob02 = Job.getInstance(conf, "UserGroupJob02");
//设置运行此jar包入口类
//ugJob02的入口是WordCountDriver类
ugJob02.setJarByClass(UserGroupDriver02.class);
//设置Job调用的Mapper类
ugJob02.setMapperClass(UserGroupMapper02.class);
//设置Job调用的Reducer类(如果一个Job没有Reducer则可以不调用此条语句)
ugJob02.setReducerClass(UserGroupReducer02.class); //设置MapTask的输出键类型
ugJob02.setMapOutputKeyClass(Text.class);
//设置MapTask的输出值类型
ugJob02.setMapOutputValueClass(Text.class); //设置整个Job的输出键类型(如果一个Job没有Reducer则可以不调用此条语句)
ugJob02.setOutputKeyClass(Text.class);
//设置整个Job的输出值类型(如果一个Job没有Reducer则可以不调用此条语句)
ugJob02.setOutputValueClass(Text.class); //设置整个Job需要处理数据的输入路径
FileInputFormat.setInputPaths(ugJob02, inputPath);
//设置整个Job计算结果的输出路径
FileOutputFormat.setOutputPath(ugJob02, outputPath); return ugJob02;
} }

UserGroupDriver02

 package com.mmzs.bigdata.yarn.mapreduce;

 import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job; public class UserGroupDriver { private static FileSystem fs;
private static Configuration conf;
private static final String TEMP= "hdfs://master01:9000/data/usergrouporder/tmp";
static {
String uri = "hdfs://master01:9000/";
conf = new Configuration();
try {
fs = FileSystem.get(new URI(uri), conf, "hadoop");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { String[] params = {args[0], TEMP, args[1]}; //运行第1个Job
Job ugJob01 = UserGroupDriver01.getJob(params);
//提交Job到集群并等待Job运行完成,参数true表示将Job运行时的状态信息返回到客户端
boolean flag01 = ugJob01.waitForCompletion(true);
if (!flag01) {
System.out.println("job2 running failure......");
System.exit(1);
} //运行第2个Job
Job ugJob02 = UserGroupDriver02.getJob(params);
//提交Job到集群并等待Job运行完成,参数true表示将Job运行时的状态信息返回到客户端
boolean flag02 = ugJob02.waitForCompletion(true);
if (flag02) {//等待Job02完成后就删掉中间目录并退出;
// fs.delete(new Path(TEMP), true);
System.out.println("job2 running success......");
System.exit(0);
}
System.out.println("job2 running failure......");
System.exit(1);
} }

UserGroupDriver

为了更好的测试,可以先屏蔽删除中间输出结果的语句;

//总Driver
String[] params = {args[0], TEMP, args[1]};
//运行第1个Job
Job ugJob01 = UserGroupDriver01.getJob(params);
//运行第2个Job
Job ugJob02 = UserGroupDriver02.getJob(params); //分Driver01
//放置需要处理的数据所在的HDFS路径
Path inputPath = new Path(args[0]);//params中的args[0]
//放置Job作业执行完成之后其处理结果的输出路径
Path outputPath = new Path(args[1]);//params中的TEMP //分Driver02
//params中的TEMP和args[2]//放置需要处理的数据所在的HDFS路径
Path inputPath = new Path(args[1]);
//放置Job作业执行完成之后其处理结果的输出路径
Path outputPath = new Path(args[2]);

测试结果:

运行时传入参数是:

如果在eclipse上运行:传参需要加上集群的master的uri即 hdfs://master01:9000

输入路径参数:  /data/usergrouporder/src

输出路径参数:  /data/usergrouporder/dst

YARN集群的mapreduce测试(三)的更多相关文章

  1. YARN集群的mapreduce测试(六)

    两张表链接操作(分布式缓存): ----------------------------------假设:其中一张A表,只有20条数据记录(比如group表)另外一张非常大,上亿的记录数量(比如use ...

  2. YARN集群的mapreduce测试(五)

    将user表计算后的结果分区存储 测试准备: 首先同步时间,然后master先开启hdfs集群,再开启yarn集群:用jps查看: master上: 先有NameNode.SecondaryNameN ...

  3. YARN集群的mapreduce测试(一)

    hadoop集群搭建中配置了mapreduce的别名是yarn [hadoop@master01 hadoop]$ mv mapred-site.xml.template mapred-site.xm ...

  4. YARN集群的mapreduce测试(四)

    将手机用户使用流量的数据进行分组,排序: 测试准备: 首先同步时间,然后master先开启hdfs集群,再开启yarn集群:用jps查看: master上: 先有NameNode.SecondaryN ...

  5. YARN集群的mapreduce测试(二)

    只有mapTask任务没有reduceTask的情况: 测试准备: 首先同步时间,然后 开启hdfs集群,开启yarn集群:在本地"/home/hadoop/test/"目录创建u ...

  6. 大数据入门第八天——MapReduce详解(三)MR的shuffer、combiner与Yarn集群分析

    /mr的combiner /mr的排序 /mr的shuffle /mr与yarn /mr运行模式 /mr实现join /mr全局图 /mr的压缩 今日提纲 一.流量汇总排序的实现 1.需求 对日志数据 ...

  7. 大数据【三】YARN集群部署

    一 概述 YARN是一个资源管理.任务调度的框架,采用master/slave架构,主要包含三大模块:ResourceManager(RM).NodeManager(NM).ApplicationMa ...

  8. Spark on Yarn 集群运行要点

    实验版本:spark-1.6.0-bin-hadoop2.6 本次实验主要是想在已有的Hadoop集群上使用Spark,无需过多配置 1.下载&解压到一台使用spark的机器上即可 2.修改配 ...

  9. 使用Cloudera Manager搭建MapReduce集群及MapReduce HA

    使用Cloudera Manager搭建MapReduce集群及MapReduce HA 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.通过CM部署MapReduce On ...

随机推荐

  1. 《vue.js快跑》总结:为什么选择VUE

    2019-3-31 为什么选择Vue 有这个一个需求,我们需要根据后端数据接口请求返回的数组在页面中按列表展示? 传统上我们使用jQuery的Ajax发送http请求,获取数据.判断列表数据是否存在, ...

  2. struts2参数传递总结

    需求1:登录页面填写表单,提交后进入action,action中能够获取填入的内容.[宏观分类:页面->action] 需求2:登录action从数据库校验完毕后,跳转至主页,主页显示当前登录的 ...

  3. 基于UML的时空建模

    一.基本信息 标题:基于UML的时空建模 时间:2018 出版源:东北大学学报(自然科学版) 领域分类:UML模型:RCC-8空间拓扑:Allen-13时态拓扑:时空数据:建模 二.研究背景 问题定义 ...

  4. Debian 8下手工安装 Eclipse CDT neon.2

    从 http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/neon2 下载 eclipse-cpp-neon-2-li ...

  5. jupyter notebook常用快捷键

    阅读目录 命令模式 (按键 Esc 开启) 编辑模式 ( Enter 键启动) Jupyter Notebook 的快捷键 使用前需要进行安装: pip install jupyter   (前提是你 ...

  6. idea 2017破解的三种方式

    1.该方法最为简便,但是该方法只可以在联网时使用,打开idea主页,找到最后面的Help,打开,找到register-license server, 在输入http://idea.iteblog.co ...

  7. pycharm断点应用

    1.在需要打断点行处,单击鼠标左键打断点 2.调试程序开始,在第一个断点之前停止 3.跳过第一个断点进入到下一个断点

  8. 致我们再也回不去的 Github ...

    本文原文来自公众号 stormzhang,原作者自称“二流学校毕业.非科班出身.从 0 自学编程到现在的「段子张」”. 1.前言   相信大家都知道了,微软已确认要收购 GitHub 了,Github ...

  9. Android常用第三方支付

    移动支付 用户使用移动的终端完成对所购买商品或者服务的支付功能;分为近场支付(蓝牙支付,刷卡,滴卡),和远程支付(网上支付,短信支付) app支付模块 常见的支付厂商-->常见的支付方式 支付宝 ...

  10. win10怎么查看激活到期时间如何看是否永久激活

    win10怎么查看激活到期时间如何看是否永久激活     我们知道Windows系统需要激活后才可以使用全部功能,那么你的Windows10激活了吗?如何查看激活时间呢?是不是永久激活的?带着这些问题 ...