Hadoop之表的关联
来源:互联网 发布:网络写手如何赚钱 编辑:程序博客网 时间:2024/04/30 09:39
实例来自于《Hadoop实战》5.4和5.5节,也见于《Hadoop集群(第9期)_MapReduce初级案例》。
第一个实例是单表关联,给出child-parent表,要求输出grandchild-grandparent表。
在关系数据库里这是一个连接操作,用MapReduce来处理我觉得效率变低了,但可以处理海量的数据。
我对其进行了些许改进。简要描述如下。
map输入的每行数据,拆分为两条数据,比如A B,变成A <B和B >A两条,前者表示A是B的child,后者表示B是A的parent;
reduce依次处理每个人的关系,它将此人的parent存入一个数组中,将此人的child存入一个数组中,然后这对两个数组进行笛卡尔积运算即可。
输入数据之间必须用一个空格来分隔。
import java.io.IOException;import java.util.StringTokenizer;import java.util.Iterator;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;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;public class joint{ public static int once = 0; public static class MyMapper extends Mapper<Object, Text, Text, Text> { public void map(Object key, Text value, Context context) throws IOException, InterruptedException { int i = 0; String line = value.toString(); while(line.charAt(i) != ' ') ++i; String[] values = {line.substring(0,i),line.substring(i+1)}; if(values[0].compareTo("child") != 0) { context.write(new Text(values[0]), new Text("<"+values[1])); context.write(new Text(values[1]), new Text(">"+values[0])); } } } public static class MyReducer extends Reducer<Text,Text,Text,Text> { public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { if(once == 0) { context.write(new Text("grandchild"),new Text("grandparent")); ++once; } int grandchildnum = 0; int grandparentnum = 0; String grandchild[] = new String[10]; String grandparent[] = new String[10]; Iterator ite = values.iterator(); while(ite.hasNext()) { String record = ite.next().toString(); if(record.charAt(0) == '<') { grandparent[grandparentnum++] = record.substring(1); } else if(record.charAt(0) == '>') { grandchild[grandchildnum++] = record.substring(1); } } if(grandchildnum != 0 && grandparentnum != 0) { for(int m = 0; m<grandchildnum; ++m) for(int n = 0; n<grandparentnum; ++n) context.write(new Text(grandchild[m]),new Text(grandparent[n])); } } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf, "number sort"); job.setJarByClass(joint.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); }}
第二个实例差不多。就不说了。
0 0
- Hadoop之表的关联
- 【Hadoop基础教程】6、Hadoop之单表关联查询
- hadoop单表关联
- hadoop多表关联
- hadoop单表关联
- hadoop多表关联
- hadoop 多表关联
- hadoop--单表关联
- Hadoop案例之单表关联输出祖孙关系
- Hadoop入门案例(八)之 表 关联
- Hadoop 单表关联 多表关联
- hadoop之科普篇【hadoop和其他关联项目的关系】
- hadoop mapreduce多表关联
- Hadoop实现数据库表关联
- hadoop实例---多表关联
- hadoop学习--单表关联
- hadoop学习--多表关联
- hadoop学习--单表关联
- openMPM源码分析(四)
- Deep Learning(深度学习)学习笔记整理系列之(八)
- stack栈
- 关于文件读取的第一次尝试
- try{}catch{} 快捷键
- Hadoop之表的关联
- Android反编译-反编译工具和方法
- C/C++变量在内存中的分布
- eclipse 好用的几种插件介绍
- gcc 多文件编译 vim指定行剪切
- PHP访问MySql数据库
- 链表算法总结
- 10个超级有用、必须收藏的PHP代码样例
- POJ1364 HDU1531 King【SPFA】【差分约束】