2017-04-12 54 views
0

我正在使用Datastax driver來訪問cassandra。我的方法需要cql string paramater。 由於cql是任意的,我不知道cql字符串中表的primary keys如何以編程方式知道Cassandra中的表的主鍵

在ResultSet中,我沒有找到與primary keys關聯的metadata。 我只能得到和valuesall columns

我想知道哪些列是主鍵。 如何programmatically得到在cql中的表的主鍵?

public Map<String, Object> search(String cql) { 

    SimpleStatement statement = new SimpleStatement(cql); 
    ResultSet result = session.execute(statement); 
    // ... 
} 

回答

0

您可以使用TableMetadata.getPrimaryKey()方法

下面是一個示例演示獲得keyspace'test'的主鍵和表格ashraful_test

try (Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withCredentials("cassandra", "cassandra").build(); Session session = cluster.connect("test")) { 
    System.out.println(cluster.getMetadata().getKeyspace(session.getLoggedKeyspace()).getTable("ashraful_test").getPrimaryKey()); 
} 
0

我不知道的任何驅動程序的東西,但如果一切都失敗,你可以做到以下幾點:

cqlsh> select column_name, kind from system_schema.columns where keyspace_name ='test' and table_name = 'authorization'; 

column_name    | kind 
-------------------------+--------------- 
authorization_reference |  regular 
      order_number | partition_key 
       partner_id | clustering 
     sales_channel_id | clustering 
+0

我也知道這一點。但我只想要一個更好的解決方案。無論如何,非常感謝。 – niaomingjian

相關問題