java - 如何使用 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。如何修复代码?
解决方案
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()
推荐阅读
- android - 如何限制文本输入长度,而不是长度本身?
- java - 来自 GUAVA 库的条带锁的全局锁定
- amazon-web-services - 无法删除 Athena 表
- r - Conda 构建 R 包在 MacOS Mojave 上的 C 编译器问题上失败
- python - 将 Python 列表重塑为 Keras CNN 的 numpy 数组
- java - 将 openjdk 更新到 11 会使 intelij 失去对类的所有引用
- jquery - 如何创建自定义 Django 小部件,将 jQuery Grid Manager 拖放编辑功能添加到 Django TextArea
- c# - Visual Studio 15.9.3 会破坏 Web 表单?
- vue.js - vue2-google-maps 标记图标大小和使用 google 对象调整大小不起作用
- angular - 如何从角度6中的数组中删除重复对象