cassandra 3.x triigger:获取“聚类键及其值”

frebpwbc  于 2021-06-13  发布在  Cassandra
关注(0)|答案(1)|浏览(255)

我需要找到一种方法,不仅通过分区对象获取分区密钥,还可以通过分区对象获取集群密钥。我知道如何从对象中获取实际的分区键及其值,而不是“集群键”
到目前为止,我已经尝试过使用“unfilterediterator”,但是它只返回常规列(不是集群键/值)
我的c*表如下所示

CREATE TABLE user.foo (
ac_id timeuuid,
mapping_id timeuuid,
country text,
state text,
PRIMARY KEY (ac_id, mapping_id) ) WITH CLUSTERING ORDER BY (mapping_id DESC) ...

到目前为止我的代码是:

public static String getKeyText(Partition update) {
    List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
    CFMetaData cfm = update.metadata();
    Map<String, String> map = new HashMap<String, String>();
    try {
      UnfilteredRowIterator it = update.unfilteredIterator();
      while (it.hasNext()) {
          Unfiltered un = it.next();
          Clustering clt = (Clustering) un.clustering();
          Iterator<Cell> cells = update.getRow(clt).cells().iterator();
          Iterator<ColumnDefinition> columnss = update.getRow(clt).columns().iterator();
          while(columnss.hasNext()){
              ColumnDefinition columnDef = columnss.next();
              Cell cell = cells.next();
          }
      }
  } catch (Exception e) {

  }
}

目标是获取acu id并Map列名称和值
感谢您的帮助

juzqafwq

juzqafwq1#

我用以下方法解决了这个问题:

public static String getKeyText(Partition update) {
    List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
    CFMetaData cfm = update.metadata();
    Map<String, String> map = new HashMap<String, String>();

    String localKey = getKey(update.partitionKey().getKey(), cfm);
    // go over all columns and only add those that are clustering and partition keys
    List<ColumnDefinition> partitionKeyColumns = cfm.partitionKeyColumns();
    for (ColumnDefinition partitionColumn : partitionKeyColumns) {
      map.put(partitionColumn.name.toString(), localKey);
    }
    //Now work on clustering keys ONLY
    try {
      List<ColumnDefinition> clusteringKeyColumns = cfm.clusteringColumns();
      UnfilteredRowIterator it = update.unfilteredIterator();
      while (it.hasNext()) {
        Unfiltered un = it.next();
        Clustering clt = (Clustering) un.clustering();
        ByteBuffer[] clusteringKeyValues = clt.getRawValues();
        int i = 0;
        for (ColumnDefinition column : clusteringKeyColumns) {
          map.put(column.name.toString(), cfm.getKeyValidator().getString(clusteringKeyValues[i]));
          i++;
        }
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    listOfMaps.add(map);
    // Now process Clustering keys
    return gson.toJson(listOfMaps);
  }

然而,我不确定这是否是解决这个问题的最佳方法

相关问题