gpt4 book ai didi

apache - hadoop mapreduce 常见 friend reducer 溢出

转载 作者:行者123 更新时间:2023-12-02 21:00:37 26 4
gpt4 key购买 nike

我正在尝试运行以下代码,以找到两个人之间的共同 friend 。输入如下

A : B C D

B : A C D E

C : A B D E

D : A B C E

E : B C D

我无法在输出文件中获得任何输出,也不异常(exception)。

请在下面找到我的代码,
public class Friend {
public static class Mapperfriend extends Mapper<Object, Text, Text, Text>{


private Text vendor = new Text();


@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer tokenizer = new StringTokenizer(value.toString(), "\n");
String line = null;
String[] lineArray = null;
String[] friendArray = null;
String[] tempArray = null;



while(tokenizer.hasMoreTokens()){
line = tokenizer.nextToken();
lineArray = line.split(":");
friendArray = lineArray[1].split(" ");
tempArray = new String[2];
for(int i = 0; i < friendArray.length; i++){
tempArray[0] = friendArray[i];
tempArray[1] = lineArray[0];
Arrays.sort(tempArray);
context.write(new Text(tempArray[0] + " " + tempArray[1]), new Text(lineArray[1]));
}

}


}}

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

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


Text[] texts = new Text[2];
int index = 0;


for(Text val: values)
{
texts[index++] = new Text(val);
}
String[] list1 = texts[0].toString().split(" ");
String[] list2 = texts[1].toString().split(" ");
List<String> list = new LinkedList<String>();
for(String friend1 : list1){
for(String friend2 : list2){
if(friend1.equals(friend2)){
list.add(friend1);
}
}
}
StringBuffer sb = new StringBuffer();
for(int i = 0; i < list.size(); i++){
sb.append(list.get(i));
if(i != list.size() - 1)
sb.append(" ");
}
context.write(key, new Text(sb.toString()));
}
}




/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here

Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Friends");
job.setJarByClass(Friend.class);
job.setMapperClass(Mapperfriend.class);
job.setReducerClass(ReducerFriend.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);
}



}

最佳答案

映射器类从第一个发出整个键。而不是拿起阵列。当删除它工作正常。

关于apache - hadoop mapreduce 常见 friend reducer 溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43326002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com