[英]MapReduce HBase NullPointerException
I am beginner at bigdata. 我是bigdata的初学者。 First I wanna try how mapreduce work with hbase. 首先,我想尝试mapreduce如何与hbase一起工作。 The scenario is summing of a field uas in my hbase use map reduce based on date which is as primary key. 该方案是基于日期作为主键在我的hbase使用映射reduce中对字段uas进行求和。 Here is my table : 这是我的桌子:
Hbase::Table - test ROW COLUMN+CELL 10102010#1 column=cf:nama, timestamp=1418267197429, value=jonru 10102010#1 column=cf:quiz, timestamp=1418267197429, value=\\x00\\x00\\x00d 10102010#1 column=cf:uas, timestamp=1418267197429, value=\\x00\\x00\\x00d 10102010#1 column=cf:uts, timestamp=1418267197429, value=\\x00\\x00\\x00d 10102010#2 column=cf:nama, timestamp=1418267180874, value=jonru 10102010#2 column=cf:quiz, timestamp=1418267180874, value=\\x00\\x00\\x00d 10102010#2 column=cf:uas, timestamp=1418267180874, value=\\x00\\x00\\x00d 10102010#2 column=cf:uts, timestamp=1418267180874, value=\\x00\\x00\\x00d 10102012#1 column=cf:nama, timestamp=1418267156542, value=jonru 10102012#1 column=cf:quiz, timestamp=1418267156542, value=\\x00\\x00\\x00\\x0A 10102012#1 column=cf:uas, timestamp=1418267156542, value=\\x00\\x00\\x00\\x0A 10102012#1 column=cf:uts, timestamp=1418267156542, value=\\x00\\x00\\x00\\x0A 10102012#2 column=cf:nama, timestamp=1418267166524, value=jonru 10102012#2 column=cf:quiz, timestamp=1418267166524, value Hbase :: Table-测试ROW COLUMN + CELL 10102010#1列= cf:nama,时间戳= 1418267197429,值= jonru 10102010#1列= cf:测验,时间戳= 1418267197429,值= \\ x00 \\ x00 \\ x00d 10102010#1列= cf:uas,时间戳= 1418267197429,值= \\ x00 \\ x00 \\ x00d 10102010#1列= cf:uts,时间戳= 1418267197429,值= \\ x00 \\ x00 \\ x00d 10102010#2列= cf:nama,时间戳= 1418267180874,值= jonru 10102010#2列= cf:测验,时间戳= 1418267180874,值= \\ x00 \\ x00 \\ x00d 10102010#2列= cf:uas,时间戳= 1418267180874,值= \\ x00 \\ x00 \\ x00d 10102010#2列= cf:uts,时间戳= 1418267180874,值= \\ x00 \\ x00 \\ x00d 10102012#1列= cf:nama,时间戳= 1418267156542,值= jonru 10102012#1列= cf:测验,时间戳= 1418267156542,值= \\ x00 \\ x00 \\ x00 \\ x0A 10102012#1列= cf:UAS,时间戳= 1418267156542,值= \\ x00 \\ x00 \\ x00 \\ x0A 10102012#1列= cf:uts,时间戳= 1418267156542,值= \\ x00 \\ x00 \\ x00 \\ x0A 10102012#2列= cf:nama,时间戳= 1418267166524,值= jonru 10102012#2列= cf:测验,时间戳= 1418267166524,值 =\\x00\\x00\\x00\\x0A 10102012#2 column=cf:uas, timestamp=1418267166524, value=\\x00\\x00\\x00\\x0A 10102012#2 column=cf:uts, timestamp=1418267166524, value=\\x00\\x00\\x00\\x0A = \\ x00 \\ x00 \\ x00 \\ x0A 10102012#2列= cf:UAS,时间戳= 1418267166524,值= \\ x00 \\ x00 \\ x00 \\ x0A 10102012#2列= cf:uts,时间戳= 1418267166524,值= \\ x00 \\ X00 \\ X00 \\ X0A
My codes are like these : 我的代码如下:
public class TestMapReduce {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration config = HBaseConfiguration.create();
Job job = new Job(config, "Test");
job.setJarByClass(TestMapReduce.TestMapper.class);
Scan scan = new Scan();
scan.setCaching(500);
scan.setCacheBlocks(false);
TableMapReduceUtil.initTableMapperJob(
"test",
scan,
TestMapReduce.TestMapper.class,
Text.class,
IntWritable.class,
job);
TableMapReduceUtil.initTableReducerJob(
"test",
TestReducer.class,
job);
job.waitForCompletion(true);
}
public static class TestMapper extends TableMapper<Text, IntWritable> {
@Override
protected void map(ImmutableBytesWritable rowKey, Result columns, Mapper.Context context) throws IOException, InterruptedException {
System.out.println("mulai mapping");
try {
//get row key
String inKey = new String(rowKey.get());
//get new key having date only
String onKey = new String(inKey.split("#")[0]);
//get value s_sent column
byte[] bUas = columns.getValue(Bytes.toBytes("cf"), Bytes.toBytes("uas"));
String sUas = new String(bUas);
Integer uas = new Integer(sUas);
//emit date and sent values
context.write(new Text(onKey), new IntWritable(uas));
} catch (RuntimeException ex) {
ex.printStackTrace();
}
}
}
public class TestReducer extends TableReducer {
public void reduce(Text key, Iterable values, Reducer.Context context) throws IOException, InterruptedException {
try {
int sum = 0;
for (Object test : values) {
System.out.println(test.toString());
sum += Integer.parseInt(test.toString());
}
Put inHbase = new Put(key.getBytes());
inHbase.add(Bytes.toBytes("cf"), Bytes.toBytes("sum"), Bytes.toBytes(sum));
context.write(null, inHbase);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I got errors like these : 我遇到了这样的错误:
Exception in thread "main" java.lang.NullPointerException
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)
at org.apache.hadoop.util.Shell.runCommand(Shell.java:451)
at org.apache.hadoop.util.Shell.run(Shell.java:424)
at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:656)
at org.apache.hadoop.util.Shell.execCommand(Shell.java:745)
at org.apache.hadoop.util.Shell.execCommand(Shell.java:728)
at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:633)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:421)
at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:281)
at org.apache.hadoop.mapreduce.JobSubmissionFiles.getStagingDir(JobSubmissionFiles.java:125)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:348)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1295)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1292)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1554)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1292)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1313)
at TestMapReduce.main(TestMapReduce.java:97)
Java Result: 1
Help me please :) 请帮帮我 :)
Let's look at this part of your code: 让我们看一下代码的这一部分:
byte[] bUas = columns.getValue(Bytes.toBytes("cf"), Bytes.toBytes("uas"));
String sUas = new String(bUas);
For the current key you are trying to get a value of column uas from column family cf . 对于当前键,您尝试从列族cf获取列uas的值。 This is a non-relational DB, so it is easily possible that this key doesn't have a value for this column. 这是一个非关系型数据库,因此很可能此键对此列没有值。 In that case, getValue method will return null. 在这种情况下, getValue方法将返回null。 String constructor that accepts byte[] as an input can't handle null values, so it will throw a NullPointerException. 接受byte []作为输入的字符串构造函数无法处理null值,因此它将引发NullPointerException。 A quick fix will look like this: 快速修复如下所示:
byte[] bUas = columns.getValue(Bytes.toBytes("cf"), Bytes.toBytes("uas"));
String sUas = bUas == null ? "" : new String(bUas);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.