首页 > 解决方案 > 如何使用 IntWritable 的值作为条件对数据进行分区?

问题描述

我想使用 IntWritable 的值作为分区数据的条件。但似乎 Partitioner() 无法获得价值。

public static class GroupMapper extends Mapper<Object, Text, Text, IntWritable>
{               
    public void map(Object index, Text value, Context context) throws IOException, InterruptedException 
    {
        String[] words=value.toString().split(" ");
        for(int i=0;i<words.length-1;i++) 
        {
            context.write(new Text(words[i]), new IntWritable(Integer.parseInt(words[words.length-1])));            
        }   
    }

}

public static class RatingPartitioner extends Partitioner<Text, IntWritable>
{
    public int getPartition(Text key, IntWritable value, int numReduceTasks)
    {  
        int group=value.get();
        if(numReduceTasks==0) {return 0;}
        else if(group==1) {return 0;}
        else if(group==2) {return 1;}
        else if(group==3) {return 2;}
        else {return 3;}
    }
 }

当我运行代码时。Java 显示 NullPointerException。如何修复代码?

标签: javahadoop-partitioning

解决方案


3 件事是可能的,但由于我没有启动这些功能的代码,所以我会说它们都是 XD 要么:

在第一个函数中:

the parameter 'value' is null and it causes a NullPointerException when 
trying to do : value.toString

the parameter 'context' is null and it causes a NullPointerException 
when trying to do : context.write

在第二个功能中:

the parameter 'value' is null and it causes a NullPointerException when 
trying to do : value.get()

推荐阅读