我创建了一个 hbase-hive 表。我在其中通过配置单元插入数据。
CREATE TABLE hivetest(cookie string, timespent string, pageviews string, visit string, logdate string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = "m:timespent, m:pageviews, m:visit, m:logdate")
TBLPROPERTIES ("hbase.table.name" = "hbasetest");
数据已正确插入。我可以使用 hbase shell 访问相同的数据。但是,我创建了一个 hbase 客户端,它在访问相同数据时返回空指向异常。
现在的问题是,如果我创建一个虚拟 hbase 表并通过 hbase shell 不通过 HIVE 放入一些数据,然后尝试从 hbase 客户端获取此数据。它不会给出错误并返回数据。 另外,如果我尝试通过 hbase 客户端将数据放入 hbasetest 表的某行中,然后尝试获取同一行,也会发生同样的情况。现在这一次它给了我数据。
那么,我的问题是通过hive和hbase shell写入数据有什么区别?
Hbase 客户端:
Configuration conf = HBaseConfiguration.create();
Map variables = System.getenv();
conf.set("hbase.zookeeper.quorum","192.168.0.92");
HTable table;
byte family[];
byte qualifier[];
table = new HTable(conf, "hivetest");
family = Bytes.toBytes("m");
qualifier = Bytes.toBytes("logdate");
Get get = new Get(Bytes.toBytes("cookie_value"));
Result r = table.get(get);
byte valueObj[] = r.getValue(family, qualifier);
byte keyObj[] = r.getRow();
String key = new String(keyObj);
String value = new String(valueObj); /* line where exception occur */
System.out.println((new StringBuilder(String.valueOf(key))).append(" = ").append(value).toString());
异常(exception):
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(Unknown Source)
at com.hbase.test.HbaseExample.main(HbaseExample.java:51)
最佳答案
您使用的是 hive 版本 0.9.0 吗? 如果是这样,则该版本有错误。 HBaseStorageHandler 的 SerDe 不会忽略逗号和列族之间的空格。这使得存储的列族名称与您期望的不同。
HbaseExample 类中的第 51 行是什么?
byte valueObj[] = r.getValue(family, qualifier);
尝试在 SERDEPROPERTIES 映射中去掉逗号符号和列族之间的空格。
关于java - 从通过 hive 插入的 hbase 读取数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18983286/