mapreduce 中单表关联的问题

lty369963 lty369963 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写个一个单表关联的程序,但是没有输出,只输出表头,不知道为什么(每行数据是以tab分割的),请大侠指教
public class STjoin {
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 childnName = new String();
String parentName = new String();
//String relationtype = new String();
String line = value.toString();
String[] strs= line.split(" ");

context.write(new Text(strs[1]),new Text("1"+"-"+strs[0]));//输出左表
context.write(new Text(strs[0]),new Text("2"+"-"+strs[1]));//输出右表
}
}

public static class Reduce extends Reducer<Text,Text,Text,Text>{
public void reduce(Text key,Iterable<Text> values,Context context)throws IOException,InterruptedException{
if (time == 0){//输出表头
context.write(new Text("grandchild"),new Text("grandparent"));
time++;
}
int grandchildNum = 0;
String grandchild[] = new String[20];
int grandparentNum = 0;
String grandparent[] = new String[20];

Iterator iter = values.iterator();
while (iter.hasNext()){
String record = iter.next().toString();
String[] st = record.split("-");
if(st[0].equals("1")){
grandchild[grandchildNum] =st[1];
grandchildNum ++;

} else if(st[0].equals("2")){
grandparent [grandparentNum ]=st[1];
grandparentNum ++;
}
}


//grandchild和grandparent数组求笛卡尔积
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();
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length !=2){
System.err.println("Usage:...");
System.exit(2);
}
Job job = new Job(conf,"single table join");
job.setJarByClass(STjoin.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(otherArgs[0]));
FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : -1);
}
}
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
lty369963
Hadoop生态社区 20,842 成员 4,695 内容