cassandra,简单查询
Posted
技术标签:
【中文标题】cassandra,简单查询【英文标题】:cassandra, simple query 【发布时间】:2011-07-19 15:25:37 【问题描述】:我从 cassandra 开始,但我有些难以理解查询的工作原理。
我有这个
[default@Keyspace1] get Users[jsmith];
=> (column=adress, value=London, timestamp=1311087089506000)
=> (column=age, value=*, timestamp=1311086896514000)
=> (column=first, value=John, timestamp=1311086884652000)
=> (column=last, value=Smith, timestamp=1311086891619000)
Returned 4 results.
在 SQL 中我可以做到:SELECT last FROM Keyspace1 WHERE first="John"
但在 cassandra 语言中?
我正在尝试这样的事情,但我承认我对 sintax 完全感到困惑。
[default@Keyspace1] keyspace1.get("column_family","John","last")
...
【问题讨论】:
基本上我想转换一个 sql 查询。等价物。 【参考方案1】:[default@***] set Users['jsmith']['address']=London;
Value inserted.
[default@***] set Users['jsmith']['age']=42;
Value inserted.
[default@***] set Users['jsmith']['first']=john;
Value inserted.
[default@***] set Users['jsmith']['last']=smith;
Value inserted.
[default@***] get Users['jsmith']['last'];
=> (column=last, value=smith, timestamp=1311090805707000)
但是,您必须知道行键。做这种搜索会很困难; look at this thread, they discuss the same.
更新(感谢@jbellis的提示)
似乎在 Cassandra 0.7 和更新版本中添加了很多东西。我一直在研究 0.6。所以,是的,你可以做到。
在 Cassandra 0.7 中,您可以
[default@unknown] connect localhost/9160;
Connected to: "Test Cluster" on localhost/9160
[default@unknown] use ***;
[default@***] create column family IndexedUsers with
comparator=UTF8Type and column_metadata=[
column_name:address, validation_class:UTF8Type,
column_name:age, validation_class:UTF8Type,
column_name:first, validation_class:UTF8Type, index_type:KEYS,
column_name:last, validation_class:UTF8Type];
79d6ebce-b279-11e0-9ddf-e700f669bcfc
Waiting for schema agreement...
... schemas agree across the cluster
[default@***] set IndexedUsers['jsmith']['age']=42;
Value inserted.
[default@***] set IndexedUsers['jsmith']['address']='London';
Value inserted.
[default@***] set IndexedUsers['jsmith']['first']='John';
Value inserted.
[default@***] set IndexedUsers['jsmith']['last']='Smith';
Value inserted.
[default@***] get IndexedUsers where first='John';
-------------------
RowKey: jsmith
=> (column=address, value=London, timestamp=1311129646984000)
=> (column=age, value=42, timestamp=1311129627578000)
=> (column=first, value=John, timestamp=1311129676977000)
=> (column=last, value=Smith, timestamp=1311129698125000)
1 Row Returned.
但不是
[default@***] get IndexedUsers[last] where first='John';
Syntax error at position 23: missing EOF at 'where'
因为:
[default@***] help get;
get <cf>['<key>'];
get <cf>['<key>']['<col>'] (as <type>)*;
get <cf>['<key>']['<super>'];
get <cf>['<key>'][<function>];
get <cf>['<key>'][<function>(<super>)][<function>(<col>)];
get <cf> where <column> = <value> [and <column> > <value> and ...] [limit <integer>];
Default LIMIT is 100. Available operations: =, >, >=, <, <=
CQL 似乎是一个更好的选择。它是 Cassandra 0.8 版本的一部分。 Refer this。在那里你可以:
CREATE INDEX [index_name] ON <column_family> (column_name);
然后
SELECT [FIRST N] [REVERSED] col_name1, col_name2 FROM <COLUMN FAMILY>
[USING <CONSISTENCY>] [WHERE <CLAUSE>] [LIMIT N];
Refer complete CQL commands here.
【讨论】:
不是这样,从两个版本开始就可以在 Cassandra 中创建索引了。【参考方案2】:这篇文章解释了如何在 Cassandra 中创建索引并执行您想要的查询:http://www.datastax.com/dev/blog/whats-new-cassandra-07-secondary-indexes
【讨论】:
以上是关于cassandra,简单查询的主要内容,如果未能解决你的问题,请参考以下文章
Cassandra:NoHostAvailableException:所有主机尝试查询失败