分区程序无法正常工作

时间:2015-11-25 09:48:32

标签: hadoop mapreduce partitioner

我正在尝试编写一个MapReduce场景,其中我以JSON的形式创建了一些User ClickStream数据。之后我编写了Mapper类来从文件中获取所需的数据,我的映射器代码是: -

private final static String URL = "u";

private final static String Country_Code = "c";

private final static String Known_User = "nk";

private final static String Session_Start_time = "hc";

private final static String User_Id = "user";

private final static String Event_Id = "event";

public void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {
    String aJSONRecord = value.toString();
    try {
        JSONObject aJSONObject = new JSONObject(aJSONRecord);
        StringBuilder aOutputString = new StringBuilder();
        aOutputString.append(aJSONObject.get(User_Id).toString()+",");
        aOutputString.append(aJSONObject.get(Event_Id).toString()+",");
        aOutputString.append(aJSONObject.get(URL).toString()+",");
        aOutputString.append(aJSONObject.get(Known_User)+",");
        aOutputString.append(aJSONObject.get(Session_Start_time)+",");
        aOutputString.append(aJSONObject.get(Country_Code)+",");
        context.write(new Text(aOutputString.toString()), key);
        System.out.println(aOutputString.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

我的减速机代码是: -

public void reduce(Text key, Iterable<LongWritable> values,
        Context context) throws IOException, InterruptedException {
        String aString =  key.toString();
        context.write(new Text(aString.trim()), new Text(""));  

}

我的分区代码是: -

public int getPartition(Text key, LongWritable value, int numPartitions) {
    String aRecord = key.toString();
    if(aRecord.contains(Country_code_Us)){
        return 0;
    }else{
        return 1;
    }
}

这是我的驱动程序代码

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "Click Stream Analyzer");
    job.setNumReduceTasks(2);
    job.setJarByClass(ClickStreamDriver.class);
    job.setMapperClass(ClickStreamMapper.class);
    job.setReducerClass(ClickStreamReducer.class);
    job.setPartitionerClass(ClickStreamPartitioner.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(LongWritable.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);

}

我在这里尝试根据国家/地区代码对数据进行分区。但它不起作用,它将每个记录发送到一个reducer文件中,我认为其他文件是为美国创建的文件减少。

当我看到映射器的输出时,还会显示在每条记录末尾添加一些额外空间。

如果我在这里犯了错误,请建议。

3 个答案:

答案 0 :(得分:0)

分区的问题是由于减少器的数量。如果为1,则所有数据都将发送给它,与您从分区程序返回的数据无关。因此,将mapred.reduce.tasks设置为2将解决此问题。或者您可以简单地写一下:

job.setNumReduceTasks(2);

为了有你想要的2个减速器。

答案 1 :(得分:0)

除非您有非常具体的要求,否则您可以将减压器设置为以下作业参数。

mapred.reduce.tasks (in 1.x) & mapreduce.job.reduces(2.x)

job.setNumReduceTasks(2)根据mark91回答。

但是使用以下API将工作留给Hadoop fraemork。框架将根据文件&amp ;;决定减少器的数量。块大小。

job.setPartitionerClass(HashPartitioner.class);

答案 2 :(得分:0)

我使用过NullWritable并且它有效。现在我可以看到记录在不同的文件中被分区。由于我使用longwritable作为空值而不是null可写,因此在每行的最后一行添加了空格,因此美国被列为“US”,分区无法划分订单。

相关问题