簡體   English   中英

Cassandra復合列的結構和插入

[英]Cassandra composite columns structure and insert

我想通過CQL 3在復合列系列中插入數據。

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

該結構將使用cassandra-cli外殼制作一次,結果將保存在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或節儉的驅動程序。

在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(或可能是浮點型),以便可以在查詢中進行數字比較。

您也可以將標記存儲在列值而不是列名中。 為此,請從主鍵上刪除標記。 然后,您可以例如在標記上建立輔助索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM