3

谁能提供一个清晰的例子来说明如何使用 thrift 在 Java 中删除超列?

编辑:找到了一个解决方案,这允许我在一批变异中删除我想要的任意数量的超级列。

    List<Mutation> mutations = new ArrayList<Mutation>();
    Map<String, List<Mutation>> keyMutations = new HashMap<String, List<Mutation>>();
    Map<ByteBuffer, Map<String, List<Mutation>>> mutationsMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
    try {
        tr.open();
        client.set_keyspace("my_keyspace");

        Deletion deletion = new Deletion();
        SlicePredicate slicePredicate = new SlicePredicate();

        List<ByteBuffer> columns = new ArrayList<ByteBuffer>();

        // Add as many supercolumns as you want here 
        columns.add(toByteBuffer("supercolumn_name"));

        slicePredicate.setColumn_names(columns);
        deletion.setPredicate(slicePredicate);

        // timestamp in microseconds        
        deletion.setTimestamp(System.currentTimeMillis() * 1000);

        Mutation m = new Mutation();
        m.setDeletion(deletion);

        mutations.add(m);

        keyMutations.put("column_family_name", mutations);

        mutationsMap.put(toByteBuffer("row_id_in_column_family"), keyMutations);
        client.batch_mutate(mutationsMap, ConsistencyLevel.ONE);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
         tr.flush();
         tr.close();
    }
4

2 回答 2

2

我不知道,我也使用原始 Thrift,但您应该使用更高级别的客户端

使用 Hector 添加 CF 是这样完成的:

String KEYSPACE = "Keyspace";
Cluster cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170");
CfDef cfDef = new CfDef(KEYSPACE, "Users").setComparator_type(BytesType.class.getSimpleName()).setKey_cache_size(0)
            .setRow_cache_size(0).setGc_grace_seconds(86400));

try {
    cluster.addColumnFamily(new ThriftCfDef(cfDef));
} catch (Throwable e) {
    logger.error("Exception while creating CF, " + cfDef.getName()
        + " - probably already exists", e);
    }
}

下降更容易:

cluster.dropColumnFamily(KEYSPACE, "Users"); //with exceptions

文件

于 2012-06-14T09:09:34.603 回答
1

找到了一个解决方案,这使我可以在一批变异中删除我想要的任意数量的超级列。

List<Mutation> mutations = new ArrayList<Mutation>();
Map<String, List<Mutation>> keyMutations = new HashMap<String, List<Mutation>>();
Map<ByteBuffer, Map<String, List<Mutation>>> mutationsMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
try {
    tr.open();
    client.set_keyspace("my_keyspace");

    Deletion deletion = new Deletion();
    SlicePredicate slicePredicate = new SlicePredicate();

    List<ByteBuffer> columns = new ArrayList<ByteBuffer>();

    // Add as many supercolumns as you want here 
    columns.add(toByteBuffer("supercolumn_name"));

    slicePredicate.setColumn_names(columns);
    deletion.setPredicate(slicePredicate);

    // timestamp in microseconds        
    deletion.setTimestamp(System.currentTimeMillis() * 1000);

    Mutation m = new Mutation();
    m.setDeletion(deletion);

    mutations.add(m);

    keyMutations.put("column_family_name", mutations);

    mutationsMap.put(toByteBuffer("row_id_in_column_family"), keyMutations);
    client.batch_mutate(mutationsMap, ConsistencyLevel.ONE);
} catch (Exception e) {
    e.printStackTrace();
} finally {
     tr.flush();
     tr.close();
}
于 2012-06-14T18:53:07.617 回答