apache - hadoop mapreduce 常见 friend reducer 溢出

标签 apache hadoop

我正在尝试运行以下代码,以找到两个人之间的共同 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/

相关文章:

hadoop - "Storm"数据处理能力

angularjs - Angular 将所有 $http 请求默认为 GET 请求

php - .htaccess错误无法按预期工作

apache - 在 htaccess 重写规则中验证 utf-8

hadoop - 从 Spark 作业创建 FS 的快照

hadoop - 在 Hive 中永久添加/定义 Jar

linux - Git pull : Change Authentication

svn - 如何使用提交后 Hook 将提交的文件从 SVN 复制到 Web 目录?

json - pig 。 JsonLoader。加载没有架构的嵌套对象

hadoop - 用Impala写入S3 Parquet