MapReduce应用案例--单表关联

摘要:
importjava.util.ArrayList;importorg.apach.hadoop.io.Text;importorg.apach.hadoop.mapreduce.Mapper;查找孙子孙女表*/publicclassSingleTableRelation{publicstaticintime=0;

1. 实例描述

  单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘。

  实例中给出child-parent 表, 求出grandchild-grandparent表。

  输入数据 file01:

child        parent
Tom          Lucy
Tom          Jack
Jone         Lucy
Jone         Jack
Lucy         Marry
Lucy         Ben
Jack         Alice
Jack         Jesse
Terry        Alice
Terry        Jesse
Philip       Terry
Philip       Alma
Mark         Terry
Mark         Alma

  希望输出为:

 

grandchild    grandparent
Tom    Alice
Tom    Jesse
Jone    Alice
Jone    Jesse
Tom    Marry
Tom    Ben
Jone    Marry
Jone    Ben
Philip    Alice
Philip    Jesse
Mark    Alice
Mark    Jesse

2. 设计思路

  1. 在map阶段,将原数据进行分割,将parent作为map输出的key值,child作为map输出的value值,这样形成左表。

  2. 同时在map阶段过程中,将child作为map输出的key值,parent作为map输出的value值,这样形成右表。

  3. 连接左表的paren列和右表的child列。

3. 具体实现

  

package tablerelation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
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;

/**
 * 
 * @author Amei 单表链接,求grandchild grandparent表
 */

public class SingleTableRelation {
    public static int time = 0;

    /**
     * 
     * @author Amei 左表的paren 和 右表的 child 做链接
     */
    public static class Map extends Mapper<LongWritable, Text, Text, Text> {
        protected void map(LongWritable key, Text value, Context context)
                throws java.io.IOException, InterruptedException {
       // 左右表的标识
int relation; StringTokenizer tokenizer = new StringTokenizer(value.toString()); String child = tokenizer.nextToken(); String parent = tokenizer.nextToken(); if (child.compareTo("child") != 0) { // 左表 relation = 1; context.write(new Text(parent), new Text(relation + "+" + child)); // 右表 relation = 2; context.write(new Text(child), new Text(relation + "+" + parent)); } }; } public static class Reduce extends Reducer<Text, Text, Text, Text> { protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context output) throws java.io.IOException, InterruptedException { int grandchildnum = 0; int grandparentnum = 0; List<String> grandchilds = new ArrayList<>(); List<String> grandparents = new ArrayList<>(); /** 输出表头 */ if (time == 0) { output.write(new Text("grandchild"), new Text("grandparent")); time++; } for (Text val : values) { String record = val.toString(); char relation = record.charAt(0); // 取出此时key所对应的child if (relation == '1') { String child = record.substring(2); grandchilds.add(child); grandchildnum++; } // 取出此时key所对应的parent else { String parent = record.substring(2); grandparents.add(parent); grandparentnum++; } } if (grandchildnum != 0 && grandparentnum != 0) { for (int i = 0; i < grandchildnum; i++) for (int j = 0; j < grandparentnum; j++) output.write(new Text(grandchilds.get(i)), new Text( grandparents.get(j))); } } } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = new Job(conf,"single tale relation"); job.setJarByClass(SingleTableRelation.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path("/user/hadoop_admin/singletalein")); FileOutputFormat.setOutputPath(job, new Path("/user/hadoop_admin/singletableout")); System.exit((job.waitForCompletion(true) ? 0 : 1)); } }

免责声明:文章转载自《MapReduce应用案例--单表关联》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇zabbix监控memcached服务右键菜单插件——contextmenu下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

前端入门flutter-06 ListView基础列表组件、水平列表组件、图标组件

  在日常开发中,经常看到的列表界面就是这集的内容: 垂直列表 垂直图文列表 横向列表 动态列表   ListView组件常用的参数: scrollDirection: Axis.horizontal 横向列表 Axis.vertical 垂直列表(默认垂直列表) padding : EdgeInsetsGeometry, 内边距   re...

纯css设置各行变色

/*从第二行(偶数行)开始变色*/table tr:th-child(2n){ background-color:red;} /*从第一行(奇数行)开始变色*/table tr:nth-child(2n-1){ background-color:red;}...

格式Table.TransformColumns(Power Query 之 M 语言)

数据源:         任意表,其中包含文本列 目标:         对文本列格式进行设置  操作过程:    选取文本列》【转换】》【格式】》选取        M公式:     = Table.TransformColumns( 表, {{"列名1", 转换函数1, 数据类型1},…,{"列名n", 转换函数n, 数据类型n}}, 剩余列转换函数,...

Flutter Animation AnimatedBuilder

Flutter AnimatedBuilder 创建动画的widget Key key, @required Listenable animation, @required this.builder, this.child, animation:Animationcontroller //动画 child 动画作用的view builder:每次con...

QTablewidget通过代理实现限制输入

QTablewidget代理 之前做过一个QTablewidget想要限制某些单元格只能输入IP,刚开始采用在单元格中添加QLineEdit控件的方法,效果差强人意。后来发现通过QItemDelegate可以很方便实现需求。 QItemDelegate  1 #pragma once 2 #include <QWidget> 3 #inc...

android--LinearLayout的child中layout_weight的作用与使用

linearLayout中包含有weight的child时,linearLayout会measure两次: 第一次 测量 child 的 原始值: 第二次 测量 child 的 比重值: 然后将2次测量的值相加,得到child 的具体的宽 或 高。 //----------------------------------------------------...