hadoop实战之单表关联STjoin源码

实例中给出child-parent表,要求输出grandchild-grandparent表:

file:

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

样例输出为:

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

一个晚上,看了一下书上的源码,直接抛开书自己写,和书中的略有不同(改进),自己用split函数写的切分部分:

package com.test.stjoin;


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


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.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class STjoin {
	private static int time = 0;


	public static class TokenizerMapper extends Mapper<Object, Text, Text, Text> {
		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			String childname = new String();
			String parentname = new String();
			String[] str = value.toString().trim().split(" ");
			if (time == 0) {
				time++;
			} else {
				childname = str[0];
				parentname = str[1];
				context.write(new Text(parentname), new Text("1" + "+" + childname));
				context.write(new Text(childname), new Text("2" + "+" + parentname));
			}
		}
	}


	public static class TokenizerReducer extends Reducer<Text, Text, Text, Text> {
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
			// 类似C++中的泛型模板
			ArrayList<String> grandchild = new ArrayList<String>();
			ArrayList<String> grandparent = new ArrayList<String>();
			// String[] grandchild=new String[10];
			// String[] grandparent=new String[10];
			// int grandchildnum=0;
			// int grandparentnum=0;
			// 处理第一行
			if (time == 0) {
				context.write(new Text("grandchild"), new Text("grandparent"));
				time++;
			}
			// foreach方法迭代每一行
			for (Text val : values) {
				String[] str = val.toString().split("\\+");
				String relationtype = new String(str[0]);
				String realtion = new String(str[1]);
				if (relationtype.equals("1"))
					grandchild.add(realtion);
				else
					grandparent.add(realtion);
				// context.write(key,new Text(""));
			}
			// 笛卡尔积的方法求关系
			if (grandchild.size() != 0 && grandparent.size() != 0) {
				for (int i = 0; i < grandchild.size(); i++)
					for (int j = 0; j < grandparent.size(); j++)
						context.write(new Text(grandchild.get(i)), new Text(grandparent.get(j)));
			}
		}
	}


	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, "STjoin");
		// Job job=new Job(conf,"word count");
		job.setJarByClass(STjoin.class);
		job.setMapperClass(TokenizerMapper.class);
		// job.setCombinerClass(TokenizerReducer.class);
		job.setReducerClass(TokenizerReducer.class);
		// job.setPartitionerClass(Partition.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);


	}


}

运行过程:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值