In HBase, how to store a List or Array structure

I have some data, looks like this:

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

Now I will store in HBase, the column name a-name

, how to store the value (['v1', 'v2', 'v3' ...])?

+3


source to share


1 answer


Values ​​in HBase are stored as just a bunch of bytes, which means that serializing and deserializing your array is the responsibility of your application. You can manually do this with Writables (see example below) or use Avro / Thrift / JSON / etc. for serialization-deserialization of data

Here's an example of how you can do it:



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

      

Here's some more generic code to serialize / deserialize different types in the post: https://github.com/elasticsearch/elasticsearch-hadoop/blob/master/mr/src/main/java/org/elasticsearch/hadoop/util/WritableUtils. java

+1


source







All Articles