Hadoop HelloWorld Examples - 单表连接

  应该是那本"Hadoop 实战"的第4个demo了,单表连接。给出一对对的children和parents的名字,然后输出所有的grandchildren和grandparents对。

  输入数据(第一列child,第二列 parent)

Tom Lucy
Tom Jack
Jone Lucy
Jone Jack
Lucy Mary
Lucy Ben
Jack Alice
Jack Jesse
Terry Alice
Terry Jesse
Philip Terry
Philip Alma
Mark Terry
Mark Alma


  输出数据(第一列grandchild,第二列grandparents)

Tom    Jesse
Tom    Alice
Jone    Jesse
Jone    Alice
Jone    Ben
Jone    Mary
Tom    Ben
Tom    Mary
Philip    Alice
Philip    Jesse
Mark    Alice
Mark    Jesse

  不知到是"Hadoop 实战" 还是那个虾皮工作室写的解法,把这个问题说成是单表联接。个人觉得其实没有这么深奥,说白了非常的简单,map输入:如上所说,key是child's name, value是parent's name; map的输出:key是名字,value是“标识+名字”,其中标识'0'后面接他的child's name,'1'接他的parent's name。比如第一个记录Tom Lucy,那么key是"Tom", value是"1Lucy",还有key是lucy,value是"0Tom" 。 reducer的输入:对于每一个人名(key),它的一系列values是他的children或者parents name(通过标识来识别是children还是parents),这样子得到key的children集合和parents集合(说白了就是知道了key的所有children和parents的名字)。然后求这两个集合的乘积(或者称为笛卡尔积)即可。。

  具体代码如下:

import java.util.*;
import java.io.*;

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.mapred.TextInputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
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.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;


public class oneTableConnect {
	
	public static class tableMapper extends Mapper<Text, Text, Text, Text>
	{
		@Override
		public void map(Text key, Text value, Context context) throws IOException, InterruptedException
		{
			context.write(key, new Text( "1" + value.toString()));
			context.write(value, new Text("0" + key.toString()));
		}
	}
	
	
	public static class tableReducer extends Reducer<Text, Text, Text, Text>
	{
		@Override
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException
		{
			String[] gChildren = new String[10];
			String[] gParents = new String[10];
			int cNum = 0;
			int pNum = 0;
			for(Text val : values)
			{
				if(val.toString().charAt(0)  == '0')// the key's child.
				{
					gChildren[cNum++] = val.toString().substring(1);
				}
				else//the key's parent.
				{
					gParents[pNum++] = val.toString().substring(1);
				}
			}
			
			for(int i=0; i<cNum; i++)
				for(int j=0;j<pNum;j++)
				{
					context.write(new Text(gChildren[i]), new Text(gParents[j]));
				}
		}	
	}
	
	public static void main(String[] args) throws Exception
	{
		Configuration conf = new Configuration();
		conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator", " ");
		
		Job job = new Job(conf, "tableConnect");
		job.setJarByClass(oneTableConnect.class);
		
		job.setMapperClass(tableMapper.class);
		job.setReducerClass(tableReducer.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		
		job.setInputFormatClass(KeyValueTextInputFormat.class);
		job.setOutputFormatClass(TextOutputFormat.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		System.exit( job.waitForCompletion(true) ? 0 : 1);
	}
}


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值