2012-06-13 32 views
3

任何人都可以提供一個清晰的例子,說明如何使用thrift刪除Java中的超列?如何使用java/thrift刪除超列

編輯:找到一個解決方案,這允許我刪除任何數量的超級列,我想在一個批處理變異。

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

回答

1

找到了一個解決方案,這允許我刪除任何數量的超級列,我想在一個批次mutate。

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

權,行刪除不符合節儉一個問題,你的問題是如何刪除列的家庭?OK剛纔看到您的評論上面,在這種情況下,就這樣做,所有其他客戶端也擴展Thrift,因此應該可以使用Thrift刪除CF – 2012-06-15 10:44:06

2

我不知道,我也用生節儉,而是你應該使用更高級別的client

赫克託加入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 

documentation

+0

不幸的是我用的節儉卡住了,我肯定會使用赫克託如果我能 – mathisonian