さくらVPSでCassandra 0.7のSecondary indexesを利用する(get_indexed_slicesメソッド)
「さくらVPSでApache Cassandra 0.7のSecondary indexesを利用してみる」の続きでJavaから利用してみた。
get_indexed_slicesメソッドを利用する
「Cassandra0.7.0のSecondary indexesを使用したJava APIサンプル」のものをそのまま利用させて頂きました。
SlicePredicate predicate = new SlicePredicate(); predicate.setColumn_names(Arrays.asList(buffer("full_name"), buffer("birth_date"), buffer("blood"))); ColumnParent parent = new ColumnParent(cfName); // get users where birth_date > 1000 and blood = 'A' 相当 IndexClause clause = new IndexClause(); clause.setStart_key(new byte[0]); clause.setExpressions(Arrays.asList( new IndexExpression(buffer("birth_date"), IndexOperator.GT, FBUtilities.toByteBuffer(1000L)), new IndexExpression(buffer("blood"), IndexOperator.EQ, buffer("A")))); List<KeySlice> results = client.get_indexed_slices(parent, clause, predicate, ConsistencyLevel.ONE); System.out.println("----------"); for (KeySlice keySlice : results) { List<ColumnOrSuperColumn> coscs = keySlice.getColumns(); for (ColumnOrSuperColumn cosc : coscs) { Column column = cosc.getColumn(); System.out.printf("%s=%s%n", new String(column.getName(), CHARSET), new String(column.getValue(), CHARSET)); } System.out.println("----------"); }
org.apache.cassandra.thrift.IndexOperatorクラスのメンバには、以下のようなものがある
No | Operator | Description |
---|---|---|
1 | EQ | 等価 |
2 | GTE | 以上または、等価 |
3 | GT | 以上 |
4 | LTE | 以下または、等価 |
5 | LT | 以下 |
次の例では、
new IndexExpression(buffer("birth_date"), IndexOperator.GT, FBUtilities.toByteBuffer(1000L))
数値1000よりbirth_dateが大きかった場合、「birth_date > 1000」という条件となる。
実行すると
---------- birth_date= blood=A full_name=takahashimaiko ----------
となる。*1
今まで利用したメソッドをまとめてみる
「Cassandra Wiki」の「API07」を参考にした
No | メソッド名 | 引数 | 戻り値 | 説明 |
---|---|---|---|---|
1 | get | key, column_path, consistency_level | ColumnOrSuperColumn | 特定キーからカラムまたはスーパーカラム1つを取り出す |
2 | insert | key, column_path, column, consistency_level | void | 特定キーで1カラムデータを作成する |
3 | remove | key, column_path, timestamp, consistency_level | void | 特定キーで1カラムデータを削除する |
4 | get_slice(setSlice_range) | key, column_parent, predicate, consistency_level | list |
スライスされたカラムまたはスーパーカラムの集合を返す(範囲抽出) |
5 | get_slice(setColumn_names) | key, column_parent, predicate, consistency_level | list |
スライスされたカラムまたはスーパーカラムの集合を返す(カラム指定) |
6 | multiget_slice | keys, column_parent, predicate, consistency_level | map |
複数キーに対して複数のget_sliceを並列にこなす |
7 | batch_mutate | mutation_map, consistency_level | void | 複数キーに対して複数のデータを作成/更新/削除する |
8 | get_indexed_slices | column_parent, index_clause, predicate, consistency_level | list |
インデックスを利用した検索?IndexClauseを利用した検索 |
その他、未確認のメソッド
No | メソッド名 | 引数 | 戻り値 | 説明 |
---|---|---|---|---|
1 | login | keyspace, auth_request | void | 認証系 |
2 | get_count | key, column_parent, consistency_level | i32 | データ数? |
3 | get_range_slices | column_parent, predicate, range, consistency_level | list |
Cassandra Wiki APIの0.6用から消えている物*2
No | メソッド名 | 引数 | 戻り値 | 説明 |
---|---|---|---|---|
1 | truncate | string column_family | void | rowをすべて削除する |
2 | system_add_column_family | CFDef cf_def | string | |
3 | system_drop_column_family | ColumnFamily column_family | string | |
4 | system_rename_column_family | string old_name, string new_name | string | |
5 | system_add_keyspace | KSDef ks_def | string | |
6 | system_drop_keyspace | string keyspace | string | |
7 | system_rename_keyspace | string old_name, string new_name | string |
続きは、またいろいろ確認してから書きます。