Permalink
Browse files

cleaner getRangeSlicesKO, added ResultsDetailsDemo

  • Loading branch information...
zznate
zznate committed Sep 10, 2010
1 parent 4ffc52b commit 7d6d2e66832d25d998a77a456edf061e63afecad
@@ -1,20 +1,29 @@
package com.riptano.cassandra.hector.example;
import java.util.Arrays;
+import java.util.List;
+import me.prettyprint.cassandra.model.KeyspaceOperator;
+import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.BatchMutation;
import me.prettyprint.cassandra.service.CassandraClient;
import me.prettyprint.cassandra.service.CassandraClientPool;
import me.prettyprint.cassandra.service.CassandraClientPoolFactory;
+import me.prettyprint.cassandra.service.Cluster;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.utils.StringUtils;
+import me.prettyprint.hector.api.factory.HFactory;
+import org.apache.cassandra.thrift.Clock;
+import org.apache.cassandra.thrift.Column;
+import org.apache.cassandra.thrift.ColumnParent;
+import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.Deletion;
import org.apache.cassandra.thrift.SlicePredicate;
/**
- * Uses BatchMutation object to delete 2 of the 3 rows inserted from the
- * {@link InsertColumnsBatchMutate} example, leaving only the "first" column.
+ * Uses BatchMutation object to delete 2 of the 3 rows inserted. Uses the low-level API
+ * directly to give an example of what is happening under Mutator class.
*
* To run this example from maven:
* mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.DeleteBatchMutate"
@@ -23,32 +32,62 @@
*
*/
public class DeleteBatchMutate {
+ private static StringSerializer stringSerializer = StringSerializer.get();
+
public static void main(String[] args) throws Exception {
- CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
- CassandraClient client = pool.borrowClient("localhost", 9160);
- Keyspace keyspace = null;
- /*
+ Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160");
+ CassandraClient client = cluster.borrowClient();
+ Keyspace keyspace = client.getKeyspace("Keyspace1");
try {
- keyspace = client.getKeyspace("Keyspace1");
+ Clock clock = keyspace.createClock();
+ Column columnFirst = new Column(StringUtils.bytes("first"),
+ StringUtils.bytes("John"),
+ clock);
+ Column columnLast = new Column(StringUtils.bytes("last"),
+ StringUtils.bytes("Smith"),
+ clock);
+ Column columnMiddle = new Column(StringUtils.bytes("middle"),
+ StringUtils.bytes("Q"),
+ clock);
+ // create the batchMutation
+ BatchMutation<String> batchMutation = new BatchMutation<String>(stringSerializer);
+ batchMutation.addInsertion("jsmith", Arrays.asList("Standard1"), columnFirst);
+ batchMutation.addInsertion("jsmith", Arrays.asList("Standard1"), columnLast);
+ batchMutation.addInsertion("jsmith", Arrays.asList("Standard1"), columnMiddle);
+ keyspace.batchMutate(batchMutation);
+ // create the SlicePredicate for verification and deletion
SlicePredicate slicePredicate = new SlicePredicate();
slicePredicate.addToColumn_names(StringUtils.bytes("last"));
slicePredicate.addToColumn_names(StringUtils.bytes("middle"));
- Deletion deletion = new Deletion(keyspace.createTimestamp());
+
+ List<Column> cols = keyspace.getSlice("jsmith", new ColumnParent("Standard1"), slicePredicate);
+ System.out.println("Verify insertion of columns we are about to delete: " + cols);
+
+ // re-init the BatchMutation for deletion
+ batchMutation = new BatchMutation<String>(stringSerializer);
+
+ Deletion deletion = new Deletion(keyspace.createClock());
deletion.setPredicate(slicePredicate);
- BatchMutation batchMutation = new BatchMutation();
+
batchMutation.addDeletion("jsmith", Arrays.asList("Standard1"), deletion);
keyspace.batchMutate(batchMutation);
- System.out.println("Deletion successful.");
+ System.out.println("Deletion successful.");
+
+ cols = keyspace.getSlice("jsmith", new ColumnParent("Standard1"), slicePredicate);
+ System.out.println("Verify deletion of columns (should have an empty result): " + cols);
+
+
System.out.println("Verify on CLI with: get Keyspace1.Standard1['jsmith'] ");
System.out.println("Should only return the 'first' column.");
} finally {
- pool.releaseClient(keyspace.getClient());
+ // release off the keyspace-ref in case of failover
+ cluster.releaseClient(keyspace.getClient());
}
- */
+
}
}
@@ -5,11 +5,21 @@
import java.util.Map;
import java.util.Set;
+import me.prettyprint.cassandra.model.HectorException;
+import me.prettyprint.cassandra.model.KeyspaceOperator;
+import me.prettyprint.cassandra.model.Mutator;
+import me.prettyprint.cassandra.model.OrderedRows;
+import me.prettyprint.cassandra.model.RangeSlicesQuery;
+import me.prettyprint.cassandra.model.Result;
+import me.prettyprint.cassandra.model.Row;
+import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.CassandraClient;
import me.prettyprint.cassandra.service.CassandraClientPool;
import me.prettyprint.cassandra.service.CassandraClientPoolFactory;
+import me.prettyprint.cassandra.service.Cluster;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.utils.StringUtils;
+import me.prettyprint.hector.api.factory.HFactory;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnParent;
@@ -30,53 +40,45 @@
*/
public class GetRangeSlicesKeysOnly {
+ private static StringSerializer stringSerializer = StringSerializer.get();
+
public static void main(String[] args) throws Exception {
- CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
- CassandraClient client = pool.borrowClient("localhost", 9160);
- Keyspace keyspace = null;
- /*
- try {
- keyspace = client.getKeyspace("Keyspace1");
- // Insert 10 rows with 3 columns each of dummy data
- for (int i = 0; i < 10; i++) {
- ColumnPath cp = new ColumnPath("Standard1");
- cp.setColumn(StringUtils.bytes("fake_column_0"));
- keyspace.insert("fake_key_"+i, cp, StringUtils.bytes("fake_value_0_" + i));
-
- cp.setColumn(StringUtils.bytes("fake_column_1"));
- keyspace.insert("fake_key_"+i, cp, StringUtils.bytes("fake_value_1_" + i));
+ Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160");
+
+ KeyspaceOperator keyspaceOperator = HFactory.createKeyspaceOperator("Keyspace1", cluster);
- cp.setColumn(StringUtils.bytes("fake_column_2"));
- keyspace.insert("fake_key_"+i, cp, StringUtils.bytes("fake_value_2_" + i));
+ try {
+ Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer);
+
+ for (int i = 0; i < 5; i++) {
+ mutator.addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i))
+ .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i))
+ .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i));
}
+ mutator.execute();
- ColumnParent columnParent = new ColumnParent("Standard1");
- SlicePredicate sp = new SlicePredicate();
- sp.setColumn_names(new ArrayList<byte[]>());
- KeyRange keyRange = new KeyRange();
- keyRange.setCount(10);
- // prefix matching is okay here regardless of partitioner
- keyRange.setStart_key("fake_key_");
- keyRange.setEnd_key("");
- Map<String, List<Column>> results = keyspace.getRangeSlices(columnParent, sp, keyRange);
- Set<String> keySet = results.keySet();
+ RangeSlicesQuery<String, String, String> rangeSlicesQuery =
+ HFactory.createRangeSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer);
+ rangeSlicesQuery.setColumnFamily("Standard1");
+ rangeSlicesQuery.setKeys("fake_key_", "");
+ rangeSlicesQuery.setReturnKeysOnly();
- // setup slicing and predicate for the verification query
- SliceRange sliceRange = new SliceRange(new byte[0], new byte[0], false, 3);
- SlicePredicate slicePredicate = new SlicePredicate();
- slicePredicate.setSlice_range(sliceRange);
+ rangeSlicesQuery.setRowCount(5);
+ Result<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
+ OrderedRows<String, String, String> orderedRows = result.get();
- for (String key : keySet) {
- System.out.println("result key:" + key + " which should have null: " + results.get(key));
- System.out.println("|-- called directly via get_slice, the value is: " +keyspace.getSlice(key, columnParent, slicePredicate));
- System.out.println("|-- verify on CLI with: get Keyspace1.Standard1['" + key + "'] ");
- }
+ Row<String,String,String> lastRow = orderedRows.peekLast();
- } finally {
- pool.releaseClient(keyspace.getClient());
+ System.out.println("Contents of rows: \n");
+ for (Row<String, String, String> r : orderedRows) {
+ System.out.println(" " + r);
+ }
+
+
+ } catch (HectorException he) {
+ he.printStackTrace();
}
- */
}
}
@@ -0,0 +1,56 @@
+package com.riptano.cassandra.hector.example;
+
+import me.prettyprint.cassandra.model.HectorException;
+import me.prettyprint.cassandra.model.KeyspaceOperator;
+import me.prettyprint.cassandra.model.MutationResult;
+import me.prettyprint.cassandra.model.Mutator;
+import me.prettyprint.cassandra.model.OrderedRows;
+import me.prettyprint.cassandra.model.RangeSlicesQuery;
+import me.prettyprint.cassandra.model.Result;
+import me.prettyprint.cassandra.serializers.StringSerializer;
+import me.prettyprint.cassandra.service.Cluster;
+import me.prettyprint.hector.api.factory.HFactory;
+
+/**
+ * Shows off the new ExecutionResult hierarchy
+ *
+ * To run this example from maven:
+ * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.ResultDetailsDemo"
+ *
+ * @author zznate
+ *
+ */
+public class ResultDetailsDemo {
+
+ private static StringSerializer stringSerializer = StringSerializer.get();
+
+ public static void main(String[] args) throws Exception {
+ Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160");
+
+ KeyspaceOperator keyspaceOperator = HFactory.createKeyspaceOperator("Keyspace1", cluster);
+ try {
+ Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer);
+ // add 10 rows
+ for (int i = 0; i < 10; i++) {
+ mutator.addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i))
+ .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i))
+ .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i));
+ }
+ MutationResult me = mutator.execute();
+ System.out.println("MutationResult from 10 row insertion: " + me);
+
+ RangeSlicesQuery<String, String, String> rangeSlicesQuery =
+ HFactory.createRangeSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer);
+ rangeSlicesQuery.setColumnFamily("Standard1");
+ rangeSlicesQuery.setKeys("", "");
+ rangeSlicesQuery.setRange("", "", false, 3);
+
+ rangeSlicesQuery.setRowCount(10);
+ Result<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
+ System.out.println("Result from rangeSlices query: " + result.toString());
+
+ } catch (HectorException he) {
+ he.printStackTrace();
+ }
+ }
+}
@@ -89,16 +89,12 @@ public static void main(String[] args) throws Exception {
q.setColumnFamily("Standard1");
q.setRange("", "", false, 3);
q.setKey(row.getKey());
- // try with column name first
Result<ColumnSlice<String, String>> r = q.execute();
System.out.println("|-- called directly via get_slice, the value is: " +r);
- try {
- System.out.println("|-- try the first column via getColumn: " + HFactory.createColumnQuery(keyspaceOperator,
- stringSerializer, stringSerializer, stringSerializer).setColumnFamily("Standard1").setKey(row.getKey()).setName("fake_column_0").execute());
- } catch (HectorException he) {
- System.out.println("|-- try the first column via getColumn: [a NotFoundException was caught]");
- }
+ // For a tombstone, you just get a null back from ColumnQuery
+ System.out.println("|-- try the first column via getColumn: " + HFactory.createColumnQuery(keyspaceOperator,
+ stringSerializer, stringSerializer, stringSerializer).setColumnFamily("Standard1").setKey(row.getKey()).setName("fake_column_0").execute());
System.out.println("|-- verify on CLI with: get Keyspace1.Standard1['" + row.getKey() + "'] ");
}

0 comments on commit 7d6d2e6

Please sign in to comment.