3

在 Github ( https://github.com/apache/cassandra ) 上的 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. ....
4

1 回答 1

1

我查看了行突变源代码,其中在同一个包上有一个具有方法的 RowMutationSerializer

       serialize(RowMutation, DataOutputStream, int)

这能起到什么作用吗?

于 2012-08-14T08:00:25.093 回答