Mapreduce编程二 表关联


import java.awt.image.AreaAveragingScaleFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Vector;


import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.nntp.NewsgroupInfo;
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.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.input.SplitLineReader;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet.COL;


import com.cloudera.io.netty.handler.codec.http.HttpHeaders.Values;
import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.xml.bind.v2.schemagen.xmlschema.List;


import jdk.internal.org.objectweb.asm.tree.analysis.Value;


public class findparent {


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


private Text data = new Text();
private IntWritable result = new IntWritable();


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


String[] val = value.toString().split("\\s+");
String child = val[0];
String parent = val[1];
context.write(new Text(child), new Text("a#" + parent));
context.write(new Text(parent), new Text("b#" + child));
}
}


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


public void reduce(Text key, Iterable<Text> value, Context context) throws IOException, InterruptedException {
Vector<String> vecA = new Vector<String>();
Vector<String> vecB = new Vector<String>();
System.out.println("new line is " + value.toString());
for (Text values : value) {
String val = values.toString();
if (val.startsWith("a#")) {
vecA.add(val.substring(2));
} else if (val.startsWith("b#")) {
vecB.add(val.substring(2));
}
}


int sizeA = vecA.size();
int sizeB = vecB.size();


System.out.println("key is " + key + "sizeA is " + sizeA + " sizeB is " + sizeB);


int i, j;
for (i = 0; i < sizeA; i++) {
for (j = 0; j < sizeB; j++) {
context.write(new Text(""), new Text(vecB.get(i) + "#" + vecA.get(j)));
}
}
}
}


public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "mapjoin");
job.setJarByClass(findparent.class);
job.setMapperClass(TokenizerMapper.class);
// job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(1);
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
发出的红包

打赏作者

tom_fans

谢谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值