11. Cassandra1.2の新機能について
CQL3 Sets, Maps, Lists のコレクション対応
CQL3ではSets Maps Lists の型がカラムとして入れられるようになった
利点
・シリアライズ化して突っ込まなくてもよくなった
難点
・プライマリーキーとして宣言は出来ない
CREATE TABLE points (
id text PRIMARY KEY,
point list<int>
); id | point
insert into points (id, point) values ('test',[2,4,7,9]);
= ------+--------
test | [2, 7]
update points set point = point - [4, 9] where id = 'test‘;
select * from points;
19. CQL3を使用してみる
Keyspaceとtableを作成
CREATE KEYSPACE space WITH strategy_class = 'SimpleStrategy'
AND strategy_options:replication_factor=1
しかしうまくいかな
い・・・
実は1.1のCQL3と1.2のCQL3は別物
(1.2のCQLとして1.1のCQLが資料として乗っている場合がある)
20. CQL3を使用してみる
気を取り直してKeyspaceとtableを作成
CREATE KEYSPACE space WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': '1'
}; 定義が違う
use space;
CREATE TABLE books ( 複合キーを使用する場合は
title text, 連ねて宣言する
chapter int,
page int,
line int,
value text,
PRIMARY KEY (title,chapter,page,line)
);
Insert into books(title,chapter,page,line,value) values (‘test’,1,1,1,‘testString’);
21. CQL3を使用してみる
select文でwhere句を試そう
select * from books where title = 'test' ○取得できた
select * from books where chapter = 1 ×allow filter?
select * from books where chapter = 1 allow filtering ○取得できた
select * from books where title = 'test' and chapter = 1 ○取得できた
select * from books where page = 1 ×取得できない
select * from books where title = 'test' and page = 1 ×取得できない
select * from books where title = 'test' and chapter = 1 and page = 1 ○取得できた
select * from books where title = 'test' and chapter = 1 and line = 1 ×取得できない
select * from books where value = 'testString' ×取得できない
なぜ?! ここでRDBMSから1.2に直接入ってきた人間はじわじわ死ぬ
23. CQL3を使用してみる
Table を変更してもう一度試してみよう
select * from books where title = 'test' ×取得できない
select * from books where chapter = 1 ×取得できない
select * from books where chapter = 1 allow filtering ×取得できない
select * from books where title = 'test' and chapter = 1 ×取得できない
select * from books where page = 1 ×取得できない
select * from books where title = 'test' and page = 1 ×取得できない
select * from books where title = 'test' and chapter = 1 and page = 1 ×取得できない
select * from books where title = 'test' and chapter = 1 and line = 1 ×取得できない
select * from books where value = 'testString' ×取得できない
悪化したんですけどお!!!!11!
29. 実際のwhere文について
PRIMARY KEY ((title,chapter),page,line)
この場合のRowKey部分のwhere句
・RowKeyを含める場合は確実にRowKey全てを含める事
○ select * from books where title = 'book1' and chapter = 1
× select * from books where chapter = 1
× select * from books where title = 'book1'
・RowKeyで範囲検索をする際はtokenを使用する事
(パーテショナーを理解してから使用する事)
○ select * from books where token(title,chapter) >= token('book1', 1)
and token(title,chapter) <= token('book1', 99)
× select * from books where token(title) >= token('book1')
and token(title) <= token('book1')
30. 実際のwhere文について
PRIMARY KEY ((title,chapter),page,line)
この場合のColumn部分のwhere句
・Columnで検索する際は宣言した順番に指定する必要がある
またallow filtering は複数のRowKeyから指定したColumnを取得する場合に付与しなければならない
○ select * from books where page = 1 allow filtering
× select * from books where line = 1 allow filtering
○ select * from books where page = 1 and line = 1 allow filtering
・取得するRowKeyが一つの場合は allow filtering は付与しなくてもよ
い○ select * from books where title = 'book1' and chapter = 1 and page = 1
○ select * from books where title = 'book1' and chapter = 1 and page = 1 allow filtering
・Columnで範囲検索をする場合は一つのColumnのみ指定できる
○ select * from books where title = 'book1' and chapter = 1 and page >= 1 and page <= 99
× select * from books where title = 'book1' and chapter = 1 and page >= 1 and line >= 1
31. 実際のwhere文について
Primary key 以外のカラムをwhere文で使用したい場合
そのカラムにインデックスを張る
CREATE INDEX [<indexname>] ON <cfname> ( <colname> )
カラムファミリ booksのvalueというカラムにインデックスを張る場合
CREATE INDEX ON books (value);
value を使用して問い合わせる
select * from books where value = ‘test’
パーテーションキーが複合キーの場合
where句でパーテーションキーとの併用が・・・・
あと COMPACT STRAGE には使用できない・・・・
33. ByteOrderedPartitioner
にする事で可能なクエリ
・パーテーションキーの範囲検索がColumnと同じように可能
例えば
CREATE TABLE books (
title text,
chapter int,
page int,
line int,
value text,
PRIMARY KEY ((title,chapter),page,line)
);
の場合
Select * from books where title = ‘text’ and chapter > 3
and page = 1
and line >= 3 and line < 8 allow filtering
34. ByteOrderedPartitioner
にする事で可能なクエリ
・複合パーテーションキーでのインデックス2個目の範囲検索
例えば
CREATE TABLE books (
title text,
chapter int, で、こんなクエリが可能
page int,
line int, Select * from books where title = ‘text’ and chapter > 3
value1 text, and page = 1
value2 text, and line >= 3 and line < 8
value3 text, and value1 = ‘test’
PRIMARY KEY ((title,chapter),page,line) and value2 > ‘a’ and value2 < ‘z’
); allow filtering
CREATE INDEX idx_001 ON books (value1);
CREATE INDEX idx_002 ON books (value2);
逆を言えば複合パーテーションキーでインデックス2個目の範囲検索は
ByteOrdered以外の場合は出来ない