さくらVPSでCassandra 0.7のbatch_mutate(削除処理)を試す
既に「さくらVPSでApache Cassandra 0.7でいろいろデータを取得してみる」で
batch_mutateは利用してみたが削除処理がまだだったので試してみた
batch_mutateメソッドを利用する
「Cassandra--The-Definitive-Guide」のBatchDeleteExample.javaを参考に
試してみました。
テストデータに関して
Keyspace1のカラムファミリー「Standard2」に下記の様なデータが存在しているとします。
list Standard2; Using default limit of 100 ------------------- RowKey: michibatajessica3 ------------------- RowKey: xxx => (column=birth_date, value=00000000000007cf, timestamp=1295514962094) => (column=blood, value=4f, timestamp=1295514962094) => (column=full_name, value=5461726f2059616d616461, timestamp=1295514962094) ------------------- RowKey: yyy => (column=birth_date, value=00000000000007c6, timestamp=1295514962094) => (column=blood, value=4142, timestamp=1295514962094) => (column=full_name, value=49636869726f2053757a756b69, timestamp=1295514962094) ------------------- RowKey: michibatajessica ------------------- RowKey: sasakinozomi3 => (column=age, value=3232, timestamp=1295423487396) => (column=first, value=6e6f7a6f6d69, timestamp=1295423487396) => (column=last, value=736173616b69, timestamp=1295423487396) 5 Rows Returned.
batch_mutateメソッドを実行してみる
ByteBuffer key = ByteBufferUtil.bytes("xxx"); // this is the row key long ts = System.currentTimeMillis(); SlicePredicate delPred = new SlicePredicate(); SlicePredicate delPred2 = new SlicePredicate(); SlicePredicate delPred3 = new SlicePredicate(); List<ByteBuffer> delCols = new ArrayList<ByteBuffer>(); List<ByteBuffer> delCols2 = new ArrayList<ByteBuffer>(); List<ByteBuffer> delCols3 = new ArrayList<ByteBuffer>(); // let's delete the column named 'b', though we could add more delCols.add(ByteBufferUtil.bytes("birth_date")); delPred.column_names = delCols; delCols2.add(ByteBufferUtil.bytes("blood")); delPred2.column_names = delCols2; delCols3.add(ByteBufferUtil.bytes("full_name")); delPred3.column_names = delCols3; Deletion deletion = new Deletion(); deletion.predicate = delPred; deletion.timestamp = ts; Mutation mutation = new Mutation(); mutation.deletion = deletion; Deletion deletion2 = new Deletion(); deletion2.predicate = delPred2; deletion2.timestamp = ts; Mutation mutation2 = new Mutation(); mutation2.deletion = deletion2; Deletion deletion3 = new Deletion(); deletion3.predicate = delPred3; deletion3.timestamp = ts; Mutation mutation3 = new Mutation(); mutation3.deletion = deletion3; Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); List<Mutation> mutationList = new ArrayList<Mutation>(); mutationList.add(mutation); mutationList.add(mutation2); mutationList.add(mutation3); Map<String, List<Mutation>> m = new HashMap<String, List<Mutation>>(); m.put(columnFamily, mutationList); // just for this row key, though we could add more mutationMap.put(key, m); client.batch_mutate(mutationMap, ConsistencyLevel.ALL); System.out.println("Delete mutation done.");
List
実行結果は、下記のような形となる。
list Standard2; Using default limit of 100 ------------------- RowKey: michibatajessica3 ------------------- RowKey: xxx ------------------- RowKey: yyy => (column=birth_date, value=00000000000007c6, timestamp=1295516237209) => (column=blood, value=4142, timestamp=1295516237209) => (column=full_name, value=49636869726f2053757a756b69, timestamp=1295516237209) ------------------- RowKey: michibatajessica ------------------- RowKey: sasakinozomi3 => (column=age, value=3232, timestamp=1295423487396) => (column=first, value=6e6f7a6f6d69, timestamp=1295423487396) => (column=last, value=736173616b69, timestamp=1295423487396) 5 Rows Returned.
実行しているときは良いが、対象クラスが多いので素の状態で利用する時は
結構、戸惑ってしまいました。