Mapreduce《案例之内连接》

Mapreduce《案例之内连接》

 

数据源:

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

 

输出结果为:

grandChildgrandParent

TomAlice

TomJesse

JoneAlice

JoneJesse

TomMary

TomBen

JoneMary

JoneBen

PhilipAlice

PhilipJesse

MarkAlice

MarkJesse

 

===================================JAVA  CODE ======================

package gq;

 

 

import java.io.IOException;

import java.util.Iterator;

import java.util.StringTokenizer;

 

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

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;

 

/**

 * 

 * Class Description:自连接,爷找孙子测试类

 *

 * Author:gaoqi  

 *

 * Date:2015年6月5日 下午2:03:08  

 *

 */

public class OwnerJoin {

 

public static int TIME = 0;

 

public static class Map extends Mapper<Object, Text, Text, Text>{

public void map(Object key,Text value,Context context) throws IOException, InterruptedException{

String line = value.toString();

String childname = "";

String parentname="";

String flag = "";

 

StringTokenizer stk = new StringTokenizer(line);

String[] _values = new String[2];

int i= 0;

while(stk.hasMoreElements()){

_values[i]=stk.nextToken();

i++;

}

if(_values[0].compareTo("child") !=0){

childname = _values[0];

parentname = _values[1];

 

flag = "1";//left table

context.write(new Text(parentname), new Text(flag+"-"+childname+"-"+parentname));

 

flag="2";//right table

context.write(new Text(childname), new Text(flag+"-"+childname+"-"+parentname));

}

 

}

 

}

public static class Reduce extends Reducer<Text, Text, Text, Text>{

 

public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{

 

 

String[] pArray = new String[10];

String[] cArray = new String[10];

int pnum = 0;

int cnum = 0;

 

Iterator<Text> vals = values.iterator();

while(vals.hasNext()){

String recod = vals.next().toString();

String[] ss = recod.split("-");

String flag = ss[0];

String childname = ss[1];

String parentname = ss[2];

System.out.println(flag+"-"+childname+"--"+parentname);

if(flag.equals("1")){

cArray[cnum] = childname;

cnum++;

}elseif(flag.equals("2")){

pArray[pnum] = parentname;

pnum++;

}

}

if(TIME ==0){

context.write(new Text("grandChild"), new Text("grandParent"));

  TIME++;

}

if(pnum != 0 && cnum !=0 ){

for(int j=0;j<cnum;j++){

for(int k=0;k<pnum;k++){

context.write(new Text(cArray[j]), new Text(pArray[k]));

}

 

}

}

 

}

}

 

 

public static void main(String[] args) throws Exception {

 

Configuration conf = new Configuration();

 

Job job = new Job(conf,"OwnerJoin");

job.setJarByClass(OwnerJoin.class);

 

job.setMapperClass(Map.class);

//job.setCombinerClass(Reduce.class);

job.setReducerClass(Reduce.class);

 

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

 

FileInputFormat.addInputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/inputOwnerJoin"));

FileOutputFormat.setOutputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/outputOwnerJoin"));

System.exit(job.waitForCompletion(true)?0:1);

 

}

 

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值