2017-04-12 50 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

相关问题