Cassandra选择没有主键名称的主键

时间:2014-05-13 17:58:15

标签: primary-key cql3 cassandra-2.0

使用CQL 3我基本上想要这样做一个语句......

SELECT * FROM columnFamilyName WHERE <PK>=1;

其中<PK>是主键的通用术语。

如果我有2个表,其中主键名是ID,而其中一个表是Index,则上述语句应适用于两者。

从某种意义上说,aerospike支持这个(airospike中的主键根本没有名字),但我不确定如何在cassandra中获得相同的通用主键名称。

2 个答案:

答案 0 :(得分:1)

Ukemi,我不知道这是可能的,因为Cassandra表可以有复合主键,主键的第一部分本身也可以是复合分区键。

E.g。在这种情况下:

CREATE TABLE .... PRIMARY KEY ((a, b), c, d) ..

&lt; PK&gt;参考a,b,c或d?

当然,如果可能的话,我也想知道!

干杯,

答案 1 :(得分:1)

CQL没有主键的通用术语;相反,你需要明确提供它。据推测,从Cassandra读取的应用程序或脚本已经具有模式的知识,因此会知道关键名称。

但是,如果您想以编程方式获取主键列名称,请参考以下方法:使用Cassandra系统表查询表定义,并解析结果以选择键名称(或多个)。

例如,这里有一个简单的主键(分区键):

cqlsh:key1> create table table1 (a int, b int, c int, primary key (a));
cqlsh:key1> select column_name,component_index,type from system.schema_columns 
        ... where keyspace_name='key1' and columnfamily_name='table1';

 column_name | component_index | type
-------------+-----------------+---------------
           a |            null | partition_key
           b |               0 |       regular
           c |               0 |       regular

这是一个稍微复杂的主键,如@reggoodwin建议:

cqlsh:key1> create table table2 (a int, b int, c int, d int, e int, 
        ... primary key ((a, b), c, d));
cqlsh:key1> select column_name,component_index,type from system.schema_columns
        ... where keyspace_name='key1' and columnfamily_name='table1';

 column_name | component_index | type
-------------+-----------------+----------------
           a |               0 |  partition_key
           b |               1 |  partition_key
           c |               0 | clustering_key
           d |               1 | clustering_key
           e |               2 |        regular

有关详细信息,请查看Querying a system table上的文档页面。

相关问题