【问题标题】:CQL select a column of composite keyCQL 选择一列复合键
【发布时间】:2015-01-08 19:46:50
【问题描述】:

我正在使用 Cassandra 数据库,想使用cqlsh 来查看以复合键格式存储在其中的数据的一些特定信息。数据模型是这样的:

rowkey(username)  column1(id)  column2(city:<city>)  value

Alice             12           city:Boston           100
Tom               13           city:New York         200
Bill              22           state:CA              111

如您所见,数据使用复合键存储,column2 具有以下模式:citystate(String) + another String(可能会有所不同)。然后在cqlsh 中,我该怎么做才能看到value 根据column2 的模式:city+:+cityname?例如,它会在column2 中列出所有带有city: 模式的“值”?

ps:架构

CREATE TABLE info (
key text,
column1 bigint,
column2 text,
value bigint,
PRIMARY KEY (key, column1, column2)
) WITH COMPACT STORAGE AND
bloom_filter_fp_chance=0.010000 AND
caching='KEYS_ONLY' AND
comment='' AND
dclocal_read_repair_chance=0.000000 AND
gc_grace_seconds=864000 AND
read_repair_chance=0.100000 AND
replicate_on_write='true' AND
populate_io_cache_on_flush='false' AND
compaction={'class': 'SizeTieredCompactionStrategy'} AND
compression={'sstable_compression': 'SnappyCompressor'};

【问题讨论】:

  • 能否正确添加完整的列架构?
  • 为什么不清楚? rowkey 是用户名,具有组合键 idsome specific string
  • 如果粘贴 cqlsh 命令“describe table table_name”的输出会更清楚。我同意 Jaya 的观点……我不得不读了几遍文字墙,但我仍然不确定你的意思。该架构将使其 100% 清晰。
  • @JayaAnanthram 请查看已编辑的问题。我列出了架构。
  • 是的,我现在看起来好多了:) 请参阅下面的答案它可能会对您有所帮助。

标签: cassandra cql


【解决方案1】:

不,使用您的架构不可能实现您的目标。 Cassandra 不支持like query。 @catpaws 的回答是实现您目标的解决方案之一。他想说的是,您可以拆分为两列(locationType 和 locationName),而不是使用单个列(对于 column2),并制作 locationType as one of the primary keysecondary indexed column。以下架构描述了as one of the primary key 策略

CREATE TABLE info (
key text,
column1 bigint,
locationType text,
locationName text,
value bigint,
PRIMARY KEY (key, column1, locationType)
)

这样就可以使用 where 子句进行查询。例如,

select * from info where key = 'Tom' and column1 = 13 and locationType = 'city'

以下架构描述了secondary indexed column 策略

CREATE TABLE info (
key text,
column1 bigint,
locationType text,
locationName text,
value bigint,
PRIMARY KEY (key, column1)
) 

CREATE INDEX info_locationType ON info (locationType);

这样就可以使用 where 子句进行查询。例如,

select * from info where key = 'Tom' and locationType = 'city'

但是,如果您使用具有低基数的二级索引(这意味着 locationType 将只有两个值city or state 中的任何一个),这将影响您的查询性能。使用二级索引时要记住的另一点是,频繁更改列值不应使用二级索引(但在您的情况下,我猜 locationType 不会经常更改),因此请尝试在主键中使用 locationType。

如果你想实现like的用法,那就去Solandrahttps://github.com/tjake/Solandra

【讨论】:

  • thisselect * from info where key = 'Tom' and locationType = 'city' 似乎也无法正常工作。有什么问题?
  • 现在查询已被编辑。问题是如果您想在 locationType 上指定 where 子句,那么 PRIMARY KEY (key, column1, locationType) 是否作为主键,然后是前两个键 'key ' 和 'column1' 也应该需要在 where 子句中给出。
【解决方案2】:

在 Cassandra 2.1 及更高版本中:

创建表用户( 用户名文本, id int, 位置图, 价值整数, 主键(用户名,id)); 插入用户(用户名、id、位置、值)VALUES ('Alice', 12, {'city': 'Boston'}, 100); 插入用户(用户名、id、位置、值)VALUES ('Tom', 13, {'city': 'New York'}, 200); 插入用户(用户名、id、位置、值)VALUES ('Bill', 22, {'state': 'CA'}, 111); 在用户(位置)上创建索引 mymapvalues; 从位置包含“纽约”的用户中选择 *; 用户名 |编号 |位置 |价值 ----------+----+----------+------ 汤姆 | 13 | {'城市':'纽约'} | 200 (1 行) 删除索引 mymapvalues; 在用户上创建索引 mymapvalueindex (KEYS(location)); 从位置包含关键“城市”的用户中选择 *; 用户名 |编号 |位置 |价值 ----------+----+----------+------ 汤姆 | 13 | {'城市':'纽约'} | 200 爱丽丝 | 12 | {'城市':'波士顿'} | 100 (2 行)

在早期版本中,使用此过程SELECT Specific Value from map

【讨论】:

  • 请查看已编辑的问题,我展示了我使用的架构。位置不是map,只是text
  • 我认为这篇文章中的答案说明了如何获取详细信息(无地图):stackoverflow.com/questions/16024839/…
猜你喜欢
  • 1970-01-01
  • 2015-04-18
  • 2012-09-02
  • 2015-03-24
  • 2012-08-31
  • 2013-07-15
  • 1970-01-01
  • 2019-09-16
  • 2014-05-26
相关资源
最近更新 更多