1

我想通过 CQL 3 在复合列族中插入数据。

create column family marks with 
comparator = 'CompositeType(DateType,UTF8Type,UTF8Type)' AND 
key_validation_class=UTF8Type AND
default_validation_class=UTF8Type;

该结构将使用 cassandra-cli shell 生成一次,结果将保存在 cassandra 中

**63**  (2013-06-04 00:00:00 UTC, Science, 89.00): ''
        (2013-06-04 00:00:00 UTC, Mathematics, 80.00): ''
        (2013-06-04 00:00:00 UTC, English, 90.00):''

这里的行键是 63,它是主键和唯一键。并且数据将像上面一样保存在 cassandra 中,仅在列名中。对此的插入查询将是什么,以及实现此 CQL3 或节俭的最合适的驱动程序是什么。

4

1 回答 1

1

在 CQL3 中,您可以使用复合主键来执行此操作:

CREATE TABLE marks (
  id text,
  date timestamp,
  subject text,
  mark text,
  PRIMARY KEY (id, date, subject, mark)
)

使用此模式,id 是行键,因为它首先列出。列名是 date:subject:mark 的组合。

然后您可以插入:

insert into marks (id, date, subject, mark) values ('63', '2013-06-04 00:00:00 UTC', 'Science', '89.00');
insert into marks (id, date, subject, mark) values ('63', '2013-06-04 00:00:00 UTC', 'Mathematics', '80.00');
insert into marks (id, date, subject, mark) values ('63', '2013-06-04 00:00:00 UTC', 'English', '90.00');

并列出:

> select * from marks;

 id | date                     | subject     | mark
----+--------------------------+-------------+-------
 63 | 2013-06-04 01:00:00+0100 |     English | 90.00
 63 | 2013-06-04 01:00:00+0100 | Mathematics | 80.00
 63 | 2013-06-04 01:00:00+0100 |     Science | 89.00

您可能希望将标记存储为 int(或可能是 float),以便您可以在查询中进行数字比较。

您还可以将标记存储在列值而不是列名中。为此,请从主键中删除标记。然后,您可以例如在标记上建立二级索引。

于 2013-06-06T10:50:49.180 回答