Cassandra二级索引问题 - 返回零行

时间:2014-09-22 00:09:04

标签: cassandra

我遇到一个问题,即二级索引在cassandra中返回零行:

我跟着入门文档:

http://www.datastax.com/documentation/getting_started/doc/getting_started/gettingStartedCQL.html

基于此,我有以下cassandra脚本

/* hello.cql */
drop keyspace test;
CREATE KEYSPACE test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
use test;
CREATE TABLE users (  user_id int PRIMARY KEY,   fname text,   lname text);
DESCRIBE TABLES;
INSERT INTO users (user_id,  fname, lname) 
  VALUES (1745, 'john', 'smith');
INSERT INTO users (user_id,  fname, lname) 
  VALUES (1744, 'john', 'doe');
INSERT INTO users (user_id,  fname, lname) 
  VALUES (1746, 'john', 'smith');
SELECT * FROM users;
CREATE INDEX ON users (lname);

/* These queries both return 0 rows ??? */
SELECT * FROM users WHERE lname = 'smith';
SELECT * FROM users WHERE lname = 'doe';

...然而

cqlsh < hello.cql 

users


 user_id | fname | lname
---------+-------+-------
    1745 |  john | smith
    1744 |  john |   doe
    1746 |  john | smith

(3 rows)

(0 rows)

(0 rows)

这应该是直截了当的 - 我错过了什么吗?

3 个答案:

答案 0 :(得分:4)

对于2 SELECT次查询返回结果意味着CREATE INDEX将同步执行它将在所有现有数据后返回将被编入索引。

如果在插入任何数据之前更改脚本中的顺序以定义索引,我希望2选择返回结果。

答案 1 :(得分:1)

使用Cassandra 2.1.0,无论索引是在插入数据之前还是之后创建的,我都会得到结果。

    Connected to Test Cluster at 127.0.0.1:9042.
    [cqlsh 5.0.1 | Cassandra 2.1.0 | CQL spec 3.2.0 | Native protocol v3]
    Use HELP for help.
    cqlsh> 
    cqlsh> CREATE KEYSPACE test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
    cqlsh> use test;
    cqlsh:test> CREATE TABLE users (  user_id int PRIMARY KEY,   fname text,   lname text);
    cqlsh:test> INSERT INTO users (user_id,  fname, lname) 
        ...   VALUES (1745, 'john', 'smith');
    cqlsh:test> INSERT INTO users (user_id,  fname, lname) 
        ...   VALUES (1744, 'john', 'doe');
    cqlsh:test> INSERT INTO users (user_id,  fname, lname) 
        ...   VALUES (1746, 'john', 'smith');
    cqlsh:test> CREATE INDEX ON users (lname);
    cqlsh:test> SELECT * FROM users WHERE lname = 'smith';

     user_id | fname | lname
    ---------+-------+-------
        1745 |  john | smith
        1746 |  john | smith

    (2 rows)

    cqlsh:test> SELECT * FROM users WHERE lname = 'doe';

     user_id | fname | lname
    ---------+-------+-------
        1744 |  john |   doe

    (1 rows)

答案 2 :(得分:0)

以下是我的安装的平台和版本信息:

john@piggies:~/Dropbox/source/casandra$ nodetool -h localhost version
ReleaseVersion: 2.0.10
john@piggies:~/Dropbox/source/casandra$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:    14.04
Codename:   trusty
john@piggies:~/Dropbox/source/casandra$ ^C
john@piggies:~/Dropbox/source/casandra$