在HBase中,如何存储List或Array结构

时间:2014-11-08 12:24:18

标签: hbase

我有一些数据,如下所示:

{' a-name':[' v1',' v2',' v3' ...]}

现在,我将存储到HBase,列名为a-name,如何存储值([' v1',' v2',' v3' ...])?

1 个答案:

答案 0 :(得分:1)

HBase中的值只存储为一组字节,这意味着数组的序列化和反序列化是应用程序的责任。您可以使用Writables(请参阅下面的示例)手动执行此操作,或使用Avro / Thrift / JSON / etc。序列化 - 反序列化您的数据

以下是您如何做到这一点的示例:

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());
    }
}

以下是一些更通用的代码,用于将不同类型序列化/反序列化为可写:https://github.com/elasticsearch/elasticsearch-hadoop/blob/master/mr/src/main/java/org/elasticsearch/hadoop/util/WritableUtils.java