3
我有一些数据,看起来像这样:在HBase的,如何存储列表或数组结构
{“一个名”:“V1”,“V2”,“V3” ...] }
现在,我将存储到HBase中,列名是a-name
,如何存储值(['v1','v2','v3'...])?
我有一些数据,看起来像这样:在HBase的,如何存储列表或数组结构
{“一个名”:“V1”,“V2”,“V3” ...] }
现在,我将存储到HBase中,列名是a-name
,如何存储值(['v1','v2','v3'...])?
HBase中的值只存储为一组字节,这意味着数组的序列化和反序列化是您的应用程序的责任。您可以使用Writables(请参阅下面的示例)手动执行此操作,或使用Avro/Thrift/JSON /等。序列化,反序列化数据
这里是你如何能做到这一个例子:
public class test {
public static Writable toWritable(ArrayList<String> list) {
Writable[] content = new Writable[list.size()];
for (int i = 0; i < content.length; i++) {
content[i] = new Text(list.get(i));
}
return new ArrayWritable(Text.class, content);
}
public static ArrayList<String> fromWritable(ArrayWritable writable) {
Writable[] writables = ((ArrayWritable) writable).get();
ArrayList<String> list = new ArrayList<String>(writables.length);
for (Writable wrt : writables) {
list.add(((Text)wrt).toString());
}
return list;
}
public static void main (String[] args) throws IOException {
ArrayList<String> arr = Lists.newArrayList("a", "b", "c");
HTable table = new HTable(HBaseConfiguration.create(), "t1");
Put p = new Put(Bytes.toBytes("key1"));
p.add(Bytes.toBytes("f1"), Bytes.toBytes("a"), WritableUtils.toByteArray(toWritable(arr)));
table.put(p);
Get g = new Get(Bytes.toBytes("key1"));
Result r = table.get(g);
ArrayWritable w = new ArrayWritable(Text.class);
w.readFields(
new DataInputStream(
new ByteArrayInputStream(
r.getValue(Bytes.toBytes("f1"), Bytes.toBytes("a"))
)
)
);
ArrayList<String> arr2 = fromWritable(w);
System.out.println(arr2.toString());
}
}
下面是一些序列化更一般的代码/反序列化不同类型的成writables:https://github.com/elasticsearch/elasticsearch-hadoop/blob/master/mr/src/main/java/org/elasticsearch/hadoop/util/WritableUtils.java