gpt4 book ai didi

hadoop - 为什么自定义分区程序在mapreduce程序中无法正常工作

转载 作者:行者123 更新时间:2023-12-02 21:40:58 28 4
gpt4 key购买 nike

我已经编写了一个带有自定义分区程序的示例mapreduce程序。
我有一个包含数字和文本的文件。数字可以是偶数和奇数的混合。我想对数字进行分区,并将偶数放在一个化简器中,将奇数移到另一个化简器。我将Reduce分区器的数量设置为2,但是数据仅在一个Reducer中显示,并且分区器无法正常工作。请找到内联程序,如果在任何地方有错误,请纠正我。

package com.mapreduce;

import java.io.IOException;

import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class FindEvenOddNumber {

/**
* @param args
* @throws IOException
*/
public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
Text emitKey = new Text();
IntWritable emitValue = new IntWritable();

public void map(LongWritable key, Text value, Context context) throws InterruptedException, IOException {

String line = value.toString();
String[] data = line.split("\\t");
if (data.length == 2) {
emitValue.set(Integer.valueOf(data[0]));
emitKey.set(data[1]);
context.write(emitKey,emitValue);
}
}
}


public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

public void reduce(Text key, Iterable<IntWritable> values, Context context)throws IOException, InterruptedException {
for(IntWritable value : values)
{
context.write(key, value);
}
}
}

public static class ReducerPartitioner extends Partitioner<Text, IntWritable>{

@Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {

int number = value.get();

if( numReduceTasks == 0)
{
return 0;
}

if ( number % 2 == 0 ){
System.out.println("You entered an even number.");
return 1 % numReduceTasks;
}
/*if(number >2){

return 1 % numReduceTasks;
}*/

else
{
return 2 % numReduceTasks;
}

}

}


/**
* @param args
*/
public static void main(String args[]) throws IOException,InterruptedException, ClassNotFoundException {

Configuration conf = new Configuration();
String userArgs[] = new GenericOptionsParser(conf, args).getRemainingArgs();
if (userArgs.length < 2) {
System.out.println("Usage: hadoop jar jarfilename mainclass input output");
System.exit(1);
}

Job job = new Job(conf, "Partitioning Even Odd Numbers");
job.setJarByClass(FindEvenOddNumber.class);

job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setPartitionerClass(ReducerPartitioner.class);
job.setNumReduceTasks(2);

job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);


FileInputFormat.addInputPath(job, new Path(userArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(userArgs[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);

}
}

这是我的数据:
0   zero    
1 one
2 two
3 three
4 four
5 five
and so on

最佳答案

那对我有用:

public static class ReducerPartitioner extends Partitioner<Text, IntWritable>{

@Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
// assert that numReduceTasks is at least 2...
return value.get() % 2 == 0 ? 0 : 1;
}
}

关于hadoop - 为什么自定义分区程序在mapreduce程序中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28751729/

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