CQL插入创建的额外列(与cli比较)

时间:2013-05-27 21:29:33

标签: cassandra cql cassandra-cli

当我使用cql与cli进行比较时,我看到在我的列族中创建了额外的列。

使用CQL创建表并插入行:

cqlsh:cassandraSample> CREATE TABLE bedbugs(
               ...     id varchar,
               ...     name varchar,
               ...     description varchar,
               ...     primary key(id, name)
               ... )  ;

cqlsh:cassandraSample> insert into bedbugs (id, name, description) 
values ('Cimex','Cimex lectularius','http://en.wikipedia.org/wiki/Bed_bug');

现在使用cli插入列:

[default@cassandraSample] set bedbugs['BatBedBug']['C. pipistrelli:description']='google.com';
Value inserted.
Elapsed time: 1.82 msec(s).
[default@cassandraSample] list bedbugs
...     ;
Using default limit of 100
Using default column limit of 100
-------------------
RowKey: Cimex
=> (column=Cimex lectularius:, value=, timestamp=1369682957658000)
=> (column=Cimex lectularius:description, value=http://en.wikipedia.org/wiki/Bed_bug, timestamp=1369682957658000)
-------------------
RowKey: BatBedBug
=> (column=C. pipistrelli:description, value=google.com, timestamp=1369688651442000)

2 Rows Returned.


cqlsh:cassandraSample> select * from bedbugs;

 id        | name              | description
-----------+-------------------+--------------------------------------
     Cimex | Cimex lectularius | http://en.wikipedia.org/wiki/Bed_bug
 BatBedBug |    C. pipistrelli |                           google.com

因此,cql为每一行创建一个额外的列,其中包含空的非主键列。这不是浪费空间吗?

2 个答案:

答案 0 :(得分:0)

当您使用CQLSh和指定的主键(Id,name)创建列族时,您使cassandra创建两个数据索引,一个用于按ID排序的数据,另一个用于按名称排序的数据。但是当您通过cassandra-cli执行此操作时,您的列族没有索引列。 cassandra-cli不支持二级索引。我希望我对你有意义,我缺乏言语来解释我的理解。

答案 1 :(得分:0)

为了与cassandra-cli兼容并防止创建这个额外的列,请将create table语句更改为包含" WITH COMPACT STORAGE"。

described here

所以

CREATE TABLE bedbugs(
  id varchar,
  name varchar,
  description varchar,
  primary key(id, name)
);

变为

CREATE TABLE bedbugs(
  id varchar,
  name varchar,
  description varchar,
  primary key(id, name)
) WITH COMPACT STORAGE;

WITH COMPACT STORAGE也是你在cql中支持wide rows的方法。