mapreduce-实现单表关联
//map类
package hadoop3;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class danbiaomap extends Mapper <LongWritable,Text,Text,Text>{
String childname=new String();
String parientname=new String();
String flag=new String();
protected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException
{
String [] str=value.toString().split("\t");
if (str[0].compareTo("child")!=0)
{ //left table
flag="1";
childname=str[0];
parientname=str[1];
context.write(new Text(parientname), new Text(flag+"+"+childname+"+"+parientname));
//right table
flag="2";
context.write(new Text(childname), new Text(flag+"+"+childname+"+"+parientname));
}
}
}
//reduce 类
package hadoop3;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class danbiaoreduce extends Reducer<Text,Text,Text,Text>{
private int num=0;
protected void reduce(Text key,Iterable<Text> value, Context context) throws IOException, InterruptedException
{
if (num==0)
{
context.write(new Text("grandchild"),new Text( "grandparient"));
num++;
}
Iterator <Text> itr=value.iterator();
int grandchildnum=0;
String [] grandchild=new String[100];
int grandparientnum=0;
String [] grandparient=new String[100];
while (itr.hasNext())
{
String [] record=itr.next().toString().split("\\+");
if (record[0].compareTo("1")==0)
{
grandchild[grandchildnum]=record[1];
grandchildnum++;
}
else if (record[0].compareTo("2")==0)
{
grandparient[grandparientnum]=record[2];
grandparientnum++;
}
else
{}
}
if(grandchildnum !=0 && grandparientnum !=0)
{
for (int i=0;i<grandparientnum;i++)
{
for (int j=0;j<grandchildnum;j++)
{
context.write(new Text(grandchild[i]), new Text(grandparient[j]));
}
}
}
}
}
//主类
package hadoop3;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
//import com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider.Text;
public class danbiao extends Configured implements Tool{
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ToolRunner.run(new danbiao(), args);
}
@Override
public int run(String[] arg0) throws Exception {
// TODO Auto-generated method stub
Configuration conf=getConf();
Job job=new Job();
job.setJarByClass(getClass());
FileSystem fs=FileSystem.get(conf);
fs.delete(new Path("/outfile1104"),true);
FileInputFormat.addInputPath(job,new Path("/luo/danbiao.txt"));
FileOutputFormat.setOutputPath(job, new Path("/outfile1104"));
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(danbiaomap.class);
job.setReducerClass(danbiaoreduce.class);
job.waitForCompletion(true);
return 0;
}
}
mapreduce-实现单表关联的更多相关文章
- MapReduce应用案例--单表关联
1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. ...
- Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException
简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...
- Hadoop 单表关联
前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础.单表关联这个实例要求从给出的数据中寻找到所关心的数据,它是对原始数据所包含信息的挖掘.下面进入这个实例. 1.实例描述 实例中给出chi ...
- MapRedece(单表关联)
源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...
- MR案例:单表关联查询
"单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘. 需求:实例中给出 child-parent(孩子—父母)表,要求输出 grandchild ...
- Hadoop工程师面试题(1)--MapReduce实现单表汇总统计
数据源格式描述: 输入t1.txt源数据,数据文件分隔符"*&*",字段说明如下: 字段序号 字段英文名称 字段中文名称 字段类型 字段长度 1 TIME_ID 时间(到时 ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parentTom LucyTom JackJone LucyJone JackLucy MaryLucy Ben ...
- MapReduce单表关联学习~
首先考虑表的自连接,其次是列的设置,最后是结果的整理. 文件内容: import org.apache.hadoop.conf.Configuration; import org.apache.had ...
- 利用hadoop来解决“单表关联”的问题
已知 child parent a b a c d b d c b e b f c g c h x g x h m x m n o x o n 则 c 2+c+g 2+c+h 1+a+c 1+d+c ...
随机推荐
- OC自动释放池autoreleasepool介绍
自动释放池的机制是:它使得应用在创建新对象时,系统能够有效地管理应用所使用的内存. @autoreleasepool { statements } 在创建新对象时,并且系统未启动ARC特性,那么在使用 ...
- python cookboo 文件与IO 函数
写出文本数据 g = open('test.txt', 'rt', newline='',encoding = 'utf-8', errors='replace') t是windows平台特有的所谓t ...
- Nginx负载均衡案例
nginx负载均衡配置,windows版本和linux版本的nginx除了启动方式其他基本无差异. 1.Niginx安装 参考:https://www.cnblogs.com/zwcry/p/9454 ...
- java word导入导出工具类
package com.shareworx.yjwy.utils; import java.io.InputStream; import java.util.HashMap; import java. ...
- Array.asList:数组转list
String s[]={"aa","bb","cc"}; List<String> sList=Arrays.asList(s) ...
- Android编译系统简要介绍【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/18466779 在Android源码环境中,我们开发好一个模块后,再写一个Androi ...
- 剑指Offer——字符串的排序
Question 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描 ...
- 智能穿戴设备移动APP端与外设数据传输协议
S1 Communication Layer specifications 1. Purpose of This Document ...
- 到底EJB是什么
到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:" ...
- Log4j2的日志配置文件,log4j2.xml文件的配置(实现控制台输出,各级别分别文件输出,自动压缩等)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W ...