序列化RowMutation

时间:2012-08-03 00:02:29

标签: java cassandra

在Github上的Cassandra源干线(https://github.com/apache/cassandra)内,有一个在examples/client_only/src/ClientOnlyExample.java中写入数据的例子:

private static void testWriting() throws Exception
    {
        // do some writing.
        for (int i = 0; i < 100; i++)
        {
            RowMutation change = new RowMutation(KEYSPACE, ByteBufferUtil.bytes(("key" + i)));
            ColumnPath cp = new ColumnPath(COLUMN_FAMILY).setColumn(("colb").getBytes());
            change.add(new QueryPath(cp), ByteBufferUtil.bytes(("value" + i)), 0);

            // don't call change.apply().  The reason is that is makes a static call into Table, which will perform
            // local storage initialization, which creates local directories.
            // change.apply();

            StorageProxy.mutate(Arrays.asList(change), ConsistencyLevel.ONE);
            System.out.println("wrote key" + i);
        }
        System.out.println("Done writing.");
    }

我希望将数据序列化为可读格式(JSON),其中写入似乎发生在org.apache.cassandra.service.StorageProxy方法内performWrite

public static IWriteResponseHandler performWrite(IMutation mutation,
            ConsistencyLevel consistency_level,
            String localDataCenter,
            WritePerformer performer)
            throws UnavailableException, IOException
    {
...

IMutation参数似乎是我想要的,因为RowMutation实现了该类。我可以获取表(键空间)和列族,但似乎无法获得列名/值。如果我在上述方法中,我如何从IMutation mutation获取该信息?

// keyspace
String table = mutation.getTable();

// TODO won't work with batch?
UUID cfId = mutation.getColumnFamilyIds().iterator().next();

// column family name cfMetadata.cfName
CFMetaData cfMetadata = Schema.instance.getCFMetaData(cfId);

// row key 
RowMutation data = new RowMutation(table, mutation.key());
String row = ByteBufferUtil.bytesToHex(data.key());

// column name/values ??
// data. ....

1 个答案:

答案 0 :(得分:1)

我查看了同一个包中的row mutation source code RowMutationSerializer,它有方法

       serialize(RowMutation, DataOutputStream, int)

这可以用于什么吗?