//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-实现单表关联的更多相关文章

  1. MapReduce应用案例--单表关联

    1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. ...

  2. Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException

    简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...

  3. Hadoop 单表关联

    前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础.单表关联这个实例要求从给出的数据中寻找到所关心的数据,它是对原始数据所包含信息的挖掘.下面进入这个实例. 1.实例描述 实例中给出chi ...

  4. MapRedece(单表关联)

    源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...

  5. MR案例:单表关联查询

    "单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘. 需求:实例中给出 child-parent(孩子—父母)表,要求输出 grandchild ...

  6. Hadoop工程师面试题(1)--MapReduce实现单表汇总统计

    数据源格式描述: 输入t1.txt源数据,数据文件分隔符"*&*",字段说明如下: 字段序号 字段英文名称 字段中文名称 字段类型 字段长度 1 TIME_ID 时间(到时 ...

  7. MapReduce编程系列 — 5:单表关联

    1.项目名称: 2.项目数据: chile    parentTom    LucyTom    JackJone    LucyJone    JackLucy    MaryLucy    Ben ...

  8. MapReduce单表关联学习~

    首先考虑表的自连接,其次是列的设置,最后是结果的整理. 文件内容: import org.apache.hadoop.conf.Configuration; import org.apache.had ...

  9. 利用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 ...

随机推荐

  1. matlab fread

    Matlab中fread函数用法    “fread”以二进制形式,从文件读出数据. 语法1:[a,count]=fread(fid,size,precision) 语法2:[a,count]=fre ...

  2. 【HackerRank】Gem Stones

    Gem Stones John has discovered various rocks. Each rock is composed of various elements, and each el ...

  3. tcp/ip 中的分组和分片

    osi 大家应该都知道osi七层模型吧,物理层 链路层 网络层 传输层 会话层 表示层 应用层ip 属于网络层,tcp 属于传输层,你可以把每一层想像成粽子的粽叶,包裹了七层的粽子最外面的就是物理层, ...

  4. 跨平台移动开发_PhoneGap API 事件类型

    PhoneGap API Events backbuttondevicereadymenubuttonpauseresumeonlineofflinebatterycriticalbatterylow ...

  5. JMeter学习(八)JDBC Request

    [step_1]:“测试计划”--(右键)à添加à线程组: [step_2]:选择step_1中添加的线程组—(右键)à添加à配置元件àJDBC Connection Configuration,添加 ...

  6. 吴恩达深度学习笔记(五) —— 优化算法:Mini-Batch GD、Momentum、RMSprop、Adam、学习率衰减

    主要内容: 一.Mini-Batch Gradient descent 二.Momentum 四.RMSprop 五.Adam 六.优化算法性能比较 七.学习率衰减 一.Mini-Batch Grad ...

  7. processing学习整理---Image

      1.Load and Display(加载与显示) Images can be loaded and displayed to the screen at their actual size or ...

  8. HYSBZ 1500 维修数列(伸展树模板)

    题意: 题解:典型伸展树的题,比较全面. 我理解的伸展树: 1 伸展操作:就是旋转,因为我们只需保证二叉树中序遍历的结果不变,所以我们可以旋转来保持树的平衡,且旋转有左旋与右旋.通过这种方式保证不会让 ...

  9. Linux嵌入式 -- 内核 - 内核定时器

    1.  度量时间差 时钟中断由系统的定时硬件以周期性的时间间隔产生,这个间隔(即频率)由内核根据HZ来确定,HZ是一个与体系结构无关的常数,可配置(50-1200),在X86平台,默认值为1000(每 ...

  10. 51nod 1060 最复杂的数 反素数

    1060 最复杂的数 基准时间限制:1 秒 空间限制:131072 KB 把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数. 例如:12的约数为:1 2 3 4 6 ...