Permalink
Browse files

next round of updates

  • Loading branch information...
1 parent b8ff88e commit 4ffc52bdc2c7390ef18269ae0374f2a00e262a81 zznate committed Sep 8, 2010
@@ -1,84 +0,0 @@
-package com.riptano.cassandra.hector.example;
-
-import java.util.Arrays;
-import java.util.List;
-
-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.Keyspace;
-import me.prettyprint.cassandra.utils.StringUtils;
-
-import org.apache.cassandra.thrift.Column;
-import org.apache.commons.lang.RandomStringUtils;
-
-/**
- * Uses BatchMutation from a use case off of hector-users@googlegroups.com
- * Original author: Shamik Bandopadhyay
- *
- * To run this example from maven:
- * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.FromUsersBatchMutate"
- *
- * @author zznate
- *
- */
-public class FromUsersBatchMutate {
-
- 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");
-
- List<String> columnFamilies = Arrays.asList("Standard1");
- BatchMutation batchMutation = new BatchMutation();
- int counter = 0;
- for (int i = 0; i <= 25000; i++) {
- for (int j = 0; j < 5; j++) {
- Column lang = new Column(StringUtils.bytes("language_" + j),
- StringUtils.bytes("English"),
- keyspace.createTimestamp());
- String key = String.format("%d_key_%s", counter, RandomStringUtils.randomAlphanumeric(6));
- batchMutation.addInsertion(key, columnFamilies, lang);
- Column query = new Column(StringUtils.bytes("query_" + j),
- StringUtils.bytes("search for Auto CAD_" +
- i), keyspace.createTimestamp());
- batchMutation.addInsertion(key,
- columnFamilies, query);
- Column url = new Column(StringUtils.bytes("url_" +
- j),
- StringUtils.bytes("www.autodesk.com"),
- keyspace.createTimestamp());
- batchMutation.addInsertion(key,
- columnFamilies, url);
- Column authorCol = new Column(StringUtils.bytes("position_" + j),
- StringUtils.bytes(String.valueOf(i)),
- keyspace.createTimestamp());
- batchMutation.addInsertion(key,
- columnFamilies, authorCol);
- Column userId = new Column(StringUtils.bytes("userId_" + j),
- StringUtils.bytes("bandops_" + i),
- keyspace.createTimestamp());
- batchMutation.addInsertion(key,
- columnFamilies, userId);
- }
-
- if (i % 500 == 0) {
- System.out.println(String.format("current counter %1$d with 'i' of %2$d", counter, i));
- keyspace.batchMutate(batchMutation);
- batchMutation = new BatchMutation();
- }
-
- counter++;
- }
- keyspace.batchMutate(batchMutation);
- } finally {
- pool.releaseClient(keyspace.getClient());
- }
- */
- }
-}
@@ -1,74 +0,0 @@
-package com.riptano.cassandra.hector.example;
-
-import java.util.Arrays;
-import java.util.List;
-
-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.Keyspace;
-import me.prettyprint.cassandra.utils.StringUtils;
-
-import org.apache.cassandra.thrift.Column;
-import org.apache.cassandra.thrift.ColumnParent;
-import org.apache.cassandra.thrift.SlicePredicate;
-import org.apache.cassandra.thrift.SliceRange;
-
-/**
- * Uses BatchMutation to insert several rows under to same key. The
- * get_slice method is then used with a SliceRange and SlicePredicate
- * to retrieve the rows.
- *
- * To run this example from maven:
- * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.InsertColumnsBatchMutate"
- *
- * @author zznate
- *
- */
-public class InsertColumnsBatchMutate {
- 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");
-
- Column columnFirst = new Column(StringUtils.bytes("first"),
- StringUtils.bytes("John"),
- keyspace.createTimestamp());
- Column columnLast = new Column(StringUtils.bytes("last"),
- StringUtils.bytes("Smith"),
- keyspace.createTimestamp());
- Column columnMiddle = new Column(StringUtils.bytes("middle"),
- StringUtils.bytes("Q"),
- keyspace.createTimestamp());
-
- BatchMutation batchMutation = new BatchMutation();
- batchMutation.addInsertion("jsmith", Arrays.asList("Standard1"), columnFirst);
- batchMutation.addInsertion("jsmith", Arrays.asList("Standard1"), columnLast);
- batchMutation.addInsertion("jsmith", Arrays.asList("Standard1"), columnMiddle);
-
- keyspace.batchMutate(batchMutation);
-
- ColumnParent columnParent = new ColumnParent("Standard1");
- SliceRange sliceRange = new SliceRange(new byte[0], new byte[0], false, 3);
- SlicePredicate slicePredicate = new SlicePredicate();
- slicePredicate.setSlice_range(sliceRange);
- List<Column> cols = keyspace.getSlice("jsmith", columnParent, slicePredicate);
-
- System.out.println("Read from cassandra: " + cols);
- System.out.println("Verify on CLI with: get Keyspace1.Standard1['jsmith'] ");
-
- int count = keyspace.getCount("jsmith", columnParent);
-
- System.out.println("Count has: " + count);
-
- } finally {
- pool.releaseClient(keyspace.getClient());
- }
- */
- }
-}
@@ -1,22 +1,14 @@
package com.riptano.cassandra.hector.example;
-import me.prettyprint.cassandra.model.ColumnQuery;
import me.prettyprint.cassandra.model.HColumn;
-import me.prettyprint.cassandra.model.HFactory;
import me.prettyprint.cassandra.model.HectorException;
import me.prettyprint.cassandra.model.KeyspaceOperator;
import me.prettyprint.cassandra.model.Mutator;
import me.prettyprint.cassandra.model.Result;
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 org.apache.cassandra.thrift.Column;
-import org.apache.cassandra.thrift.ColumnPath;
+import me.prettyprint.hector.api.factory.HFactory;
+import me.prettyprint.hector.api.query.ColumnQuery;
/**
* Inserts the value "John" under the Column "first" for the
@@ -1,10 +1,5 @@
package com.riptano.cassandra.hector.example;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.List;
-
-import me.prettyprint.cassandra.model.HFactory;
import me.prettyprint.cassandra.model.HectorException;
import me.prettyprint.cassandra.model.KeyspaceOperator;
import me.prettyprint.cassandra.model.Mutator;
@@ -13,8 +8,8 @@
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.Cluster;
+import me.prettyprint.hector.api.factory.HFactory;
/**
* A simple example showing what it takes to page over results using
@@ -56,14 +51,14 @@ public static void main(String[] args) throws Exception {
Result<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
OrderedRows<String, String, String> orderedRows = result.get();
- List<Row<String,String,String>> rows = new ArrayList<Row<String,String,String>>(orderedRows.getList());
- Row<String,String,String> lastRow = new ArrayDeque<Row<String,String,String>>(rows).peekLast();
- rows.remove(lastRow);
+
+ Row<String,String,String> lastRow = orderedRows.peekLast();
+
System.out.println("Contents of rows: \n");
- for (Row<String, String, String> r : rows) {
+ for (Row<String, String, String> r : orderedRows) {
System.out.println(" " + r);
}
- System.out.println("Should have 10 rows: " + rows.size());
+ System.out.println("Should have 11 rows: " + orderedRows.getCount());
rangeSlicesQuery.setKeys(lastRow.getKey(), "");
orderedRows = rangeSlicesQuery.execute().get();
@@ -1,15 +1,28 @@
package com.riptano.cassandra.hector.example;
+import static me.prettyprint.hector.api.factory.HFactory.createSliceQuery;
+
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import me.prettyprint.cassandra.model.ColumnSlice;
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.model.SliceQuery;
+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;
@@ -28,75 +41,72 @@
*
*/
public class TombstonedGetRangeSlices {
+
+ private static StringSerializer stringSerializer = StringSerializer.get();
+ // - add data from mutator
+ // - delete the odd rows
+ // - display results
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");
+
+ KeyspaceOperator keyspaceOperator = HFactory.createKeyspaceOperator("Keyspace1", cluster);
try {
- keyspace = client.getKeyspace("Keyspace1");
- // Insert 10 rows with 3 columns each of dummy data
- ColumnPath cp = new ColumnPath("Standard1");
- List<String> keySet = new ArrayList<String>(10);
- for (int i = 0; i < 10; i++) {
- String key = "fake_key_" + i;
- cp.setColumn(StringUtils.bytes("fake_column_0"));
- keyspace.insert(key, cp, StringUtils.bytes("fake_value_0_" + i));
-
- cp.setColumn(StringUtils.bytes("fake_column_1"));
- keyspace.insert(key, cp, StringUtils.bytes("fake_value_1_" + i));
-
- cp.setColumn(StringUtils.bytes("fake_column_2"));
- keyspace.insert(key, cp, StringUtils.bytes("fake_value_2_" + i));
- keySet.add(key);
+ 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));
}
- cp.unsetColumn();
- // now delete the odd rows
+ mutator.execute();
+ mutator.discardPendingMutations();
+ // delete the odd rows
for (int i = 0; i < 10; i++) {
if ( i % 2 == 0 ) continue;
- keyspace.remove("fake_key_"+i, cp);
+ mutator.addDeletion("fake_key_"+i, "Standard1", null, stringSerializer);
}
+ mutator.execute();
- ColumnParent columnParent = new ColumnParent("Standard1");
- SlicePredicate sp = new SlicePredicate();
- sp.addToColumn_names(StringUtils.bytes("fake_column_"));
-
- Map<String, List<Column>> results = keyspace.multigetSlice(keySet, columnParent, sp);
-
- // 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<String, String, String> rangeSlicesQuery =
+ HFactory.createRangeSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer);
+ rangeSlicesQuery.setColumnFamily("Standard1");
+ rangeSlicesQuery.setKeys("", "");
+ rangeSlicesQuery.setRange("", "", false, 3);
- // setup ColumnPath with the first column for verification
- ColumnPath verifyColumnPath = new ColumnPath("Standard1");
- verifyColumnPath.setColumn(StringUtils.bytes("fake_column_0"));
-
- // what various methods look like with tombstoned data
- for (String key : keySet) {
- int keyNum = Integer.valueOf(key.substring(9));
+ rangeSlicesQuery.setRowCount(10);
+ Result<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
+ OrderedRows<String, String, String> orderedRows = result.get();
+ for (Row<String, String, String> row : orderedRows) {
+ int keyNum = Integer.valueOf(row.getKey().substring(9));
System.out.println("+-----------------------------------");
if ( keyNum % 2 == 0 ) {
- System.out.println("| result key:" + key + " which should have values: " + results.get(key));
+ System.out.println("| result key:" + row.getKey() + " which should have values: " + row.getColumnSlice());
} else {
- System.out.println("| TOMBSTONED result key:" + key + " has values: " + results.get(key));
+ System.out.println("| TOMBSTONED result key:" + row.getKey() + " has values: " + row.getColumnSlice());
}
+ SliceQuery<String, String, String> q = HFactory.createSliceQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer);
+ q.setColumnFamily("Standard1");
+ q.setRange("", "", false, 3);
+ q.setKey(row.getKey());
+ // try with column name first
- System.out.println("|-- called directly via get_slice, the value is: " +keyspace.getSlice(key, columnParent, slicePredicate));
+ 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: " + keyspace.getColumn(key, verifyColumnPath));
+ 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]");
}
- System.out.println("|-- verify on CLI with: get Keyspace1.Standard1['" + key + "'] ");
+
+ System.out.println("|-- verify on CLI with: get Keyspace1.Standard1['" + row.getKey() + "'] ");
}
- //
- } finally {
- pool.releaseClient(keyspace.getClient());
+
+ } catch (HectorException he) {
+ he.printStackTrace();
}
- */
+
}
}

0 comments on commit 4ffc52b

Please sign in to comment.