分区程序似乎不适用于单个节点?

时间:2016-03-09 10:16:32

标签: hadoop mapreduce hdfs reduce bigdata

我已经编写了map reduce代码和自定义分区。自定义分区对某些条件的键进行排序。我在驱动程序类中设置了setNumReduceTasks = 6。 但是我在我的单机上测试这段代码。我只得到一个reducer输出文件,而不是6个reducer文件。 分区程序不能在单机上运行吗?是否需要多节点集群来查看自定义分区程序的效果? 任何有关这方面的见解将不胜感激。

3 个答案:

答案 0 :(得分:1)

当您将reducer的no设置为大于1时,分区程序始终有效,即使它是单个节点集群。

我在单节点集群上测试了以下代码,它按预期工作:

public final class SortMapReduce extends Configured implements Tool {

public static void main(final String[] args) throws Exception {
    int res = ToolRunner.run(new Configuration(), new SortMapReduce(), args);
    System.exit(res);
}

public int run(final String[] args) throws Exception {

    Path inputPath = new Path(args[0]);
    Path outputPath = new Path(args[1]);

    Configuration conf = super.getConf();

    Job job = Job.getInstance(conf);

    job.setJarByClass(SortMapReduce.class);
    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);

    job.setInputFormatClass(KeyValueTextInputFormat.class);

    job.setMapOutputKeyClass(Person.class);
    job.setMapOutputValueClass(Text.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    job.setPartitionerClass(PersonNamePartitioner.class);

    job.setNumReduceTasks(5);

    FileInputFormat.setInputPaths(job, inputPath);
    FileOutputFormat.setOutputPath(job, outputPath);

    if (job.waitForCompletion(true)) {
        return 0;
    }
    return 1;
}

public static class Map extends Mapper<Text, Text, Person, Text> {

    private Person outputKey = new Person();

    @Override
    protected void map(Text pointID, Text firstName, Context context) throws IOException, InterruptedException {
        outputKey.set(pointID.toString(), firstName.toString());
        context.write(outputKey, firstName);
    }
}

public static class Reduce extends Reducer<Person, Text, Text, Text> {

    Text pointID = new Text();

    @Override
    public void reduce(Person key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        pointID.set(key.getpointID());
        for (Text firstName : values) {
            context.write(pointID, firstName);
        }
    }
}

}

分区者类:

public class PersonNamePartitioner extends Partitioner<Person, Text> {

@Override
public int getPartition(Person key, Text value, int numPartitions) {

    return Math.abs(key.getpointID().hashCode() * 127) % numPartitions;
}

}

运行命令:

  

hadoop jar /home/hdfs/SecondarySort.jar org.test.SortMapReduce /demo/data/Customer/acct.txt / demo / data / Customer / output2

谢谢,

答案 1 :(得分:0)

我在一台机器上有一个双节点集群。这是exactly what I did。从那里你可以看到我这样做(执行时):

  

指定减速器的数量,例如两个

-D mapred.reduce.tasks=2

答案 2 :(得分:0)

仔细查看自定义分区程序。它可能会为传入其中的所有键返回相同的分区值。

在这种情况下,它是一个低效的分区程序,它将所有键发送到同一个reducer。因此,即使您将减速器的数量设置为6,只有一个减速器将具有所有键值,而剩余的5个减速器将无法处理任何内容。

因此,您将获得处理所有记录的唯一reducer的输出。

分区程序不能单独运行 机吗 分区程序也适用于单机虚拟群集。

是否需要多节点集群来查看自定义的效果 分割器吗 否。