Java 映射减少作业生成空输出文件

Java 映射减少作业生成空输出文件,java,apache,hadoop,mapreduce,bigdata,Java,Apache,Hadoop,Mapreduce,Bigdata,程序正在生成空输出文件。谁能告诉我哪里出了问题。 我们将非常感谢您的帮助。我尝试放置job.setNumReduceTask(0),因为我没有使用reducer,但输出文件仍然为空 public static class PrizeDisMapper extends Mapper<LongWritable, Text, Text, Pair>{ int rating = 0; Text CustID; IntWritable r; Text MovieID; public void

程序正在生成空输出文件。谁能告诉我哪里出了问题。 我们将非常感谢您的帮助。我尝试放置job.setNumReduceTask(0),因为我没有使用reducer,但输出文件仍然为空

public static class PrizeDisMapper extends Mapper<LongWritable, Text, Text, Pair>{
int rating = 0;
Text CustID;
IntWritable r;
Text MovieID;
public void map(LongWritable key, Text line, Context context
                ) throws IOException, InterruptedException {
        String line1 = line.toString();
        String [] fields = line1.split(":");
        if(fields.length > 1)
             {
             String Movieid = fields[0];
             String line2 = fields[1];
             String [] splitline = line2.split(",");
             String Custid = splitline[0];
             int rate = Integer.parseInt(splitline[1]);
             r = new IntWritable(rate);
             CustID = new Text(Custid);
             MovieID = new Text(Movieid);
             Pair P = new Pair();
             context.write(MovieID,P);
             }
             else
             {
             return;
             }
  }
}

 public static class IntSumReducer extends Reducer<Text,Pair,Text,Pair> {
 private IntWritable result = new IntWritable();
 public void reduce(Text key, Iterable<Pair> values,
                   Context context
                   ) throws IOException, InterruptedException {
  for (Pair val : values) {
    context.write(key, val);
  }
  }

  public class Pair implements Writable
  {
  String key;
  int value;
    public void write(DataOutput out) throws IOException {
     out.writeInt(value);
     out.writeChars(key);
  }
  public void readFields(DataInput in) throws IOException {
     key = in.readUTF();
     value = in.readInt();
  }
  public void setVal(String aKey, int aValue)
  {
     key   = aKey;
      value = aValue;
  }
公共静态类PrizedMapper扩展映射器{
内部评级=0;
文本CustID;
不可写r;
文本电影ID;
公共无效映射(可长写键、文本行、上下文
)抛出IOException、InterruptedException{
字符串line1=line.toString();
String[]fields=line1.split(“:”);
如果(fields.length>1)
{
字符串Movieid=字段[0];
字符串行2=字段[1];
String[]splitline=line2.split(“,”);
字符串Custid=splitline[0];
int rate=Integer.parseInt(splitline[1]);
r=新的可写入(速率);
CustID=新文本(CustID);
MovieID=新文本(MovieID);
对P=新对();
context.write(MovieID,P);
}
其他的
{
返回;
}
}
}
公共静态类IntSumReducer扩展了Reducer{
私有IntWritable结果=新的IntWritable();
public void reduce(文本键、Iterable值、,
语境
)抛出IOException、InterruptedException{
for(对值:值){
context.write(key,val);
}
}
公共类对实现了可写
{
字符串键;
int值;
public void write(DataOutput out)引发IOException{
out.writeInt(值);
out.writeChars(键);
}
public void readFields(DataInput in)引发IOException{
key=in.readUTF();
值=in.readInt();
}
public void setVal(字符串aKey,int aValue)
{
key=aKey;
价值=价值;
}
主要类别:

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
  System.err.println("Usage: wordcount <in> <out>");
  System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setInputFormatClass (TextInputFormat.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Pair.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
publicstaticvoidmain(字符串[]args)引发异常{
Configuration conf=新配置();
String[]otherArgs=新的GenericOptionsParser(conf,args);
if(otherArgs.length!=2){
System.err.println(“用法:wordcount”);
系统出口(2);
}
Job Job=新作业(conf,“字数”);
job.setJarByClass(WordCount.class);
setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setInputFormatClass(TextInputFormat.class);
addInputPath(作业,新路径(其他参数[0]);
setOutputPath(作业,新路径(其他参数[1]);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Pair.class);
系统退出(作业等待完成(真)?0:1;

感谢@Pathmanaban Palsamy和@Chris Gerken的建议。我已经按照你的建议修改了代码,但仍然得到了空的输出文件。有人可以建议我在我的主类中配置输入和输出。我需要在输入到mapper&how中指定Pair类吗?

我猜reduce方法应该是dec猪油

public void reduce(Text key, Iterable<Pair> values,
               Context context
               ) throws IOException, InterruptedException
public void reduce(文本键、Iterable值、,
语境
)抛出IOException、InterruptedException

您将获得一个Iterable(一个可以从中获得迭代器的对象),您可以使用它对映射到给定键的所有值进行迭代。

我猜reduce方法应该声明为

public void reduce(Text key, Iterable<Pair> values,
               Context context
               ) throws IOException, InterruptedException
public void reduce(文本键、Iterable值、,
语境
)抛出IOException、InterruptedException

您将获得一个Iterable(一个可以从中获得迭代器的对象),您可以使用该对象对映射到给定键的所有值进行迭代。

由于不需要减缩器,我怀疑这行代码如下所示

Pair p=新的Pair();
context.write(MovieID,P);

空对将是问题所在。 另外,请检查您的驾驶员等级,您已经给出了正确的keyclass和valueclass,如

job.setMapOutputKeyClass(Text.class);

job.setMapOutputValueClass(Pair.class);

由于不需要减速机,我怀疑下面这行

Pair p=新的Pair();
context.write(MovieID,P);

空对将是问题所在。 另外,请检查您的驾驶员等级,您已经给出了正确的keyclass和valueclass,如

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

右诊断(+1)。看一看,右诊断(+1)。看一看,右诊断(+1)有两个方法。