【发布时间】:2012-10-16 06:58:57
【问题描述】:
我有一个 hive/hbase 集成表,定义如下。
create table user_c(user_id int, c_name string, c_kind string, c_industry string,
c_jobtitle string, c_workyear int, c_title string, c_company string)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:c_name,cf1:c_kind,cf1:c_industry,cf1:c_jobtitle,cf1:c_workyear,cf1:c_title,cf1:c_company")
TBLPROPERTIES ("hbase.table.name" = "user_c");
在我的 java 代码中,我创建了一个 Put 并用从 db 读取的值填充它。代码如下所示:
final Put to = new Put(getByte(from, keyColumn));
for (final IColumn column : table.getColumns()) {
if (column.equals(keyColumn)) continue;
to.add(Bytes.toBytes(column.getColumnFamily()), Bytes.toBytes(column.getDestName()), getByte(from, column));
}
return to;
getByte 是一种将值转换为byte[] 的方法。
好像
byte[] getByte(final Map<String, Object> map, IColumn column) {
final Object val = map.get(column.getName());
if (val instanceof Integer) {
return Bytes.toBytes((Integer) val);
}
...
}
然后放到hbase中。
我可以从 hbase shell 扫描记录。
hbase(main):001:0> scan 'user_c'
ROW COLUMN+CELL
\x00\x0A\x07\x0D column=cf1:c_workyear, timestamp=1350298280554, value=\x00\x00\x07\xD8
\x00\x0A\x07\x0D column=cf1:c_industry, timestamp=1350298280554, value=120
...
Row key 是一个Integer 类型,当被getByte 方法处理时,它应该被自动拆箱为原始int 类型。不仅行键,其他数字类型列(cf1:c_workyear)也显示为\x00\x0A\x07\x0D,一个字节数组。
同时String 类型列(cf1:c_industry) 仅显示它的值。
这样好吗?
当我从 hive 查询记录时,它只给我一个 NULL 而不是数字类型列的值。
hive> select c_industry, c_workyear from user_c limit 1;
Total MapReduce CPU Time Spent: 10 seconds 370 msec
OK
120 NULL
Time taken: 46.063 seconds
好像c_workyear的值不能被hive识别。我想这是因为那种类型不正确。但是不应该将int 字节数组存储为int 值,而不是字节数组吗?
有人知道如何解决这个问题吗?
非常感谢。
【问题讨论】:
标签: hadoop client integration hbase hive