在HBase中,如何存储列表或数组结构
我有一些数据,看起来像这样: {'a-name':['v1','v2','v3'…]}在HBase中,如何存储列表或数组结构,hbase,Hbase,我有一些数据,看起来像这样: {'a-name':['v1','v2','v3'…]} 现在,我将存储到HBase,列名是a-name,如何存储值(['v1','v2','v3'…]) HBase中的值存储为一组字节,这意味着数组的序列化和反序列化是应用程序的责任。您可以通过使用Writables(参见下面的示例)手动完成此操作,或者使用Avro/Thrift/JSON/等来序列化数据 下面是一个如何做到这一点的示例: public class test { public static
现在,我将存储到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());
}
}
公共类测试{
公共静态可写toWritable(ArrayList列表){
可写[]内容=新的可写[list.size()];
for(int i=0;i
下面是一些将不同类型序列化/反序列化为可写类型的通用代码:如何将WrappedArray[GenericRowWithSchema]放入Hbase,GenericRowWithSchema实现可序列化。假设我有Array[Struct],我想把这样的数组放到hbase中。如果你有一个例子将帮助我。