├── LICENSE ├── README.mdown ├── src └── main │ └── java │ └── com │ └── riptano │ └── cassandra │ └── hector │ └── example │ ├── InsertSingleColumn.java │ ├── InsertSuperColumn.java │ ├── GetRangeSlicesKeysOnly.java │ ├── MultigetSliceRetrieval.java │ ├── GetIndexedSlices.java │ ├── ResultDetailsDemo.java │ ├── PaginateGetRangeSlices.java │ ├── SchemaManipulation.java │ └── TombstonedGetRangeSlices.java └── pom.xml /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010 Nate McCall (a.k.a zznate) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.mdown: -------------------------------------------------------------------------------- 1 | Overview 2 | ============ 3 | Note: though useful, this project has been largely superceeded by: 4 | 5 | https://github.com/zznate/cassandra-tutorial 6 | 7 | 8 | Provides example classes for using Hector to access Apache Cassandra (0.7.0). 9 | These examples were inspired by the same questions appearing reapeatedly on: 10 | hector-users@googlegroups.com from users with a big variance in skill level. 11 | 12 | The goals for each of these classes are: 13 | 14 | * be completely atomic 15 | * be as simple as possible 16 | * have minimal dependencies 17 | * demonstrate a specific API call with the Hector client 18 | * sacrifice reuse and encapsulation for the sake of clarity 19 | 20 | 21 | Each class can be directly executed via maven like so: 22 | 23 | mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.[class name]" 24 | 25 | These classes assume you have an instance of Cassandra running on localhost:9160 with the default configuration. If this is one of the first times you have started Apache Cassandra, make sure you have loaded the default schema: http://wiki.apache.org/cassandra/LiveSchemaUpdates 26 | 27 | 28 | Additional Sources of Information 29 | ---------------------------------- 30 | Mail List: hector-users@googlegroups.com 31 | 32 | Hector Wiki: http://wiki.github.com/rantav/hector/ 33 | 34 | Hector Doc at Riptano: http://www.riptano.com/sites/default/files/hector-v2-client-doc.pdf 35 | 36 | This software is designed solely for demonstartion purposes of basic capabilities regarding the Hector client API for Apache Cassandra. It is available under an MIT license (see LICENSE for details). Use at your own risk. 37 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/InsertSingleColumn.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | 4 | import me.prettyprint.cassandra.serializers.StringSerializer; 5 | import me.prettyprint.hector.api.Cluster; 6 | import me.prettyprint.hector.api.Keyspace; 7 | import me.prettyprint.hector.api.beans.HColumn; 8 | import me.prettyprint.hector.api.exceptions.HectorException; 9 | import me.prettyprint.hector.api.factory.HFactory; 10 | import me.prettyprint.hector.api.mutation.Mutator; 11 | import me.prettyprint.hector.api.query.ColumnQuery; 12 | import me.prettyprint.hector.api.query.QueryResult; 13 | 14 | /** 15 | * Inserts the value "John" under the Column "first" for the 16 | * key "jsmith" in the Standard1 ColumnFamily 17 | * 18 | * To run this example from maven: 19 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.InsertSingleColumn" 20 | * 21 | * @author zznate 22 | * 23 | */ 24 | public class InsertSingleColumn { 25 | 26 | private static StringSerializer stringSerializer = StringSerializer.get(); 27 | 28 | public static void main(String[] args) throws Exception { 29 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 30 | 31 | Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); 32 | try { 33 | Mutator mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get()); 34 | mutator.insert("jsmith", "Standard1", HFactory.createStringColumn("first", "John")); 35 | 36 | ColumnQuery columnQuery = HFactory.createStringColumnQuery(keyspaceOperator); 37 | columnQuery.setColumnFamily("Standard1").setKey("jsmith").setName("first"); 38 | QueryResult> result = columnQuery.execute(); 39 | 40 | System.out.println("Read HColumn from cassandra: " + result.get()); 41 | System.out.println("Verify on CLI with: get Keyspace1.Standard1['jsmith'] "); 42 | 43 | } catch (HectorException e) { 44 | e.printStackTrace(); 45 | } 46 | cluster.getConnectionManager().shutdown(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.riptano.cassandra.hector 6 | hector-examples 7 | 0.7 8 | jar 9 | hector-examples 10 | 11 | 12 | 13 | junit 14 | junit 15 | 4.6 16 | test 17 | 18 | 19 | commons-lang 20 | commons-lang 21 | 2.4 22 | 23 | 24 | me.prettyprint 25 | hector-core 26 | 1.0-2 27 | 28 | 29 | org.slf4j 30 | slf4j-api 31 | 1.5.11 32 | 33 | 34 | org.slf4j 35 | slf4j-log4j12 36 | 1.5.11 37 | 38 | 39 | commons-io 40 | commons-io 41 | 1.3.1 42 | 43 | 44 | commons-collections 45 | commons-collections 46 | 3.0 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-compiler-plugin 55 | 56 | 1.6 57 | 1.6 58 | true 59 | true 60 | true 61 | true 62 | 63 | 64 | 65 | 66 | 67 | org.codehaus.mojo 68 | exec-maven-plugin 69 | 1.1 70 | 71 | 72 | 73 | java 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/InsertSuperColumn.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | import java.util.Arrays; 4 | import java.util.Iterator; 5 | 6 | 7 | import me.prettyprint.cassandra.serializers.StringSerializer; 8 | import me.prettyprint.cassandra.utils.StringUtils; 9 | import me.prettyprint.hector.api.Cluster; 10 | import me.prettyprint.hector.api.Keyspace; 11 | import me.prettyprint.hector.api.beans.HSuperColumn; 12 | import me.prettyprint.hector.api.exceptions.HectorException; 13 | import me.prettyprint.hector.api.factory.HFactory; 14 | import me.prettyprint.hector.api.mutation.Mutator; 15 | import me.prettyprint.hector.api.query.ColumnQuery; 16 | import me.prettyprint.hector.api.query.QueryResult; 17 | import me.prettyprint.hector.api.query.SuperColumnQuery; 18 | 19 | import org.apache.cassandra.thrift.Column; 20 | import org.apache.cassandra.thrift.ColumnPath; 21 | import org.apache.cassandra.thrift.SuperColumn; 22 | 23 | /** 24 | * Like {@link InsertSingleColumn} except with a SuperColumn 25 | * 26 | * To run this example from maven: 27 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.InsertSuperColumn" 28 | * 29 | * @author zznate 30 | * 31 | */ 32 | public class InsertSuperColumn { 33 | 34 | private static StringSerializer stringSerializer = StringSerializer.get(); 35 | 36 | public static void main(String[] args) throws Exception { 37 | 38 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 39 | 40 | Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); 41 | 42 | try { 43 | Mutator mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); 44 | mutator.insert("billing", "Super1", HFactory.createSuperColumn("jsmith", 45 | Arrays.asList(HFactory.createStringColumn("first", "John")), 46 | stringSerializer, stringSerializer, stringSerializer)); 47 | 48 | SuperColumnQuery superColumnQuery = 49 | HFactory.createSuperColumnQuery(keyspaceOperator, stringSerializer, stringSerializer, 50 | stringSerializer, stringSerializer); 51 | superColumnQuery.setColumnFamily("Super1").setKey("billing").setSuperName("jsmith"); 52 | 53 | QueryResult> result = superColumnQuery.execute(); 54 | 55 | System.out.println("Read HSuperColumn from cassandra: " + result.get()); 56 | System.out.println("Verify on CLI with: get Keyspace1.Super1['billing']['jsmith'] "); 57 | 58 | } catch (HectorException e) { 59 | e.printStackTrace(); 60 | } 61 | cluster.getConnectionManager().shutdown(); 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/GetRangeSlicesKeysOnly.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | 4 | import me.prettyprint.cassandra.serializers.StringSerializer; 5 | import me.prettyprint.hector.api.Cluster; 6 | import me.prettyprint.hector.api.Keyspace; 7 | import me.prettyprint.hector.api.beans.OrderedRows; 8 | import me.prettyprint.hector.api.beans.Row; 9 | import me.prettyprint.hector.api.exceptions.HectorException; 10 | import me.prettyprint.hector.api.factory.HFactory; 11 | import me.prettyprint.hector.api.mutation.Mutator; 12 | import me.prettyprint.hector.api.query.QueryResult; 13 | import me.prettyprint.hector.api.query.RangeSlicesQuery; 14 | 15 | /** 16 | * Use get_range_slices to retrieve the keys without deserializing the columns. 17 | * For clear results, it's best to run this on an empty ColumnFamily. 18 | * 19 | * To run this example from maven: 20 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.GetRangeSlicesKeysOnly" 21 | * 22 | * @author zznate 23 | * 24 | */ 25 | public class GetRangeSlicesKeysOnly { 26 | 27 | private static StringSerializer stringSerializer = StringSerializer.get(); 28 | 29 | public static void main(String[] args) throws Exception { 30 | 31 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 32 | 33 | Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); 34 | 35 | try { 36 | Mutator mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); 37 | 38 | for (int i = 0; i < 5; i++) { 39 | mutator.addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i)) 40 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i)) 41 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i)); 42 | } 43 | mutator.execute(); 44 | 45 | RangeSlicesQuery rangeSlicesQuery = 46 | HFactory.createRangeSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer); 47 | rangeSlicesQuery.setColumnFamily("Standard1"); 48 | rangeSlicesQuery.setKeys("fake_key_", ""); 49 | //rangeSlicesQuery.setReturnKeysOnly(); 50 | 51 | rangeSlicesQuery.setRowCount(5); 52 | QueryResult> result = rangeSlicesQuery.execute(); 53 | OrderedRows orderedRows = result.get(); 54 | 55 | Row lastRow = orderedRows.peekLast(); 56 | 57 | System.out.println("Contents of rows: \n"); 58 | for (Row r : orderedRows) { 59 | System.out.println(" " + r); 60 | } 61 | 62 | 63 | } catch (HectorException he) { 64 | he.printStackTrace(); 65 | } 66 | cluster.getConnectionManager().shutdown(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/MultigetSliceRetrieval.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | 4 | import java.util.ArrayList; 5 | 6 | import me.prettyprint.cassandra.serializers.StringSerializer; 7 | import me.prettyprint.hector.api.Cluster; 8 | import me.prettyprint.hector.api.Keyspace; 9 | import me.prettyprint.hector.api.beans.Row; 10 | import me.prettyprint.hector.api.beans.Rows; 11 | import me.prettyprint.hector.api.exceptions.HectorException; 12 | import me.prettyprint.hector.api.factory.HFactory; 13 | import me.prettyprint.hector.api.mutation.Mutator; 14 | import me.prettyprint.hector.api.query.MultigetSliceQuery; 15 | import me.prettyprint.hector.api.query.QueryResult; 16 | 17 | /** 18 | * Use MultigetSliceQuery when you want to get the same columns from a known set of keys 19 | * 20 | * To run this example from maven: 21 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.MultigetSliceRetrieval" 22 | * 23 | * @author zznate 24 | * 25 | */ 26 | public class MultigetSliceRetrieval { 27 | 28 | private static StringSerializer stringSerializer = StringSerializer.get(); 29 | 30 | public static void main(String[] args) throws Exception { 31 | 32 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 33 | 34 | Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); 35 | 36 | try { 37 | Mutator mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); 38 | 39 | for (int i = 0; i < 20; i++) { 40 | mutator.addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i)) 41 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i)) 42 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i)); 43 | } 44 | mutator.execute(); 45 | 46 | MultigetSliceQuery multigetSliceQuery = 47 | HFactory.createMultigetSliceQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer); 48 | multigetSliceQuery.setColumnFamily("Standard1"); 49 | multigetSliceQuery.setKeys("fake_key_0", "fake_key_1","fake_key_2", "fake_key_3", "fake_key_4"); 50 | 51 | // set null range for empty byte[] on the underlying predicate 52 | multigetSliceQuery.setRange(null, null, false, 3); 53 | System.out.println(multigetSliceQuery); 54 | 55 | QueryResult> result = multigetSliceQuery.execute(); 56 | Rows orderedRows = result.get(); 57 | 58 | System.out.println("Contents of rows: \n"); 59 | for (Row r : orderedRows) { 60 | System.out.println(" " + r); 61 | } 62 | System.out.println("Should have 5 rows: " + orderedRows.getCount()); 63 | 64 | 65 | } catch (HectorException he) { 66 | he.printStackTrace(); 67 | } 68 | cluster.getConnectionManager().shutdown(); 69 | } 70 | 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/GetIndexedSlices.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | import me.prettyprint.cassandra.model.IndexedSlicesQuery; 4 | import me.prettyprint.cassandra.serializers.LongSerializer; 5 | import me.prettyprint.cassandra.serializers.StringSerializer; 6 | import me.prettyprint.hector.api.Cluster; 7 | import me.prettyprint.hector.api.Keyspace; 8 | import me.prettyprint.hector.api.beans.OrderedRows; 9 | import me.prettyprint.hector.api.exceptions.HectorException; 10 | import me.prettyprint.hector.api.factory.HFactory; 11 | import me.prettyprint.hector.api.mutation.Mutator; 12 | import me.prettyprint.hector.api.query.QueryResult; 13 | 14 | /** 15 | * Shows off the usage of the shiny new Column indexing feature via 16 | * get_indexed_slices. Note the use of stacked index clauses on a column that is not 17 | * indexed (birthmonth). All get_indexed_slices queries have to contain an EQ 18 | * expression though. 19 | * 20 | * To run this example from maven: 21 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.GetIndexedSlices" 22 | * 23 | * @author zznate 24 | * 25 | */ 26 | public class GetIndexedSlices { 27 | 28 | private static StringSerializer stringSerializer = StringSerializer.get(); 29 | 30 | public static void main(String[] args) throws Exception { 31 | 32 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 33 | 34 | Keyspace keyspace = HFactory.createKeyspace("Keyspace1", cluster); 35 | 36 | try { 37 | Mutator mutator = HFactory.createMutator(keyspace, stringSerializer); 38 | 39 | for (int i = 0; i < 24; i++) { 40 | mutator.addInsertion("fake_key_" + i, "Indexed1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i)) 41 | .addInsertion("fake_key_" + i, "Indexed1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i)) 42 | .addInsertion("fake_key_" + i, "Indexed1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i)) 43 | .addInsertion("fake_key_" + i, "Indexed1", HFactory.createColumn("birthdate",new Long(1974+(i%2)),stringSerializer,LongSerializer.get())) 44 | .addInsertion("fake_key_" + i, "Indexed1", HFactory.createColumn("birthmonth",new Long(i%12),stringSerializer,LongSerializer.get())); 45 | } 46 | mutator.execute(); 47 | 48 | IndexedSlicesQuery indexedSlicesQuery = 49 | HFactory.createIndexedSlicesQuery(keyspace, stringSerializer, stringSerializer, LongSerializer.get()); 50 | indexedSlicesQuery.addEqualsExpression("birthdate", 1975L); 51 | indexedSlicesQuery.addGtExpression("birthmonth", 6L); 52 | indexedSlicesQuery.addLtExpression("birthmonth", 8L); 53 | indexedSlicesQuery.setColumnNames("birthdate","birthmonth"); 54 | indexedSlicesQuery.setColumnFamily("Indexed1"); 55 | indexedSlicesQuery.setStartKey(""); 56 | 57 | QueryResult> result = indexedSlicesQuery.execute(); 58 | 59 | System.out.println("The results should only have 4 entries: " + result.get()); 60 | 61 | } catch (HectorException he) { 62 | he.printStackTrace(); 63 | } 64 | cluster.getConnectionManager().shutdown(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/ResultDetailsDemo.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | 4 | import me.prettyprint.cassandra.serializers.StringSerializer; 5 | import me.prettyprint.hector.api.Cluster; 6 | import me.prettyprint.hector.api.Keyspace; 7 | import me.prettyprint.hector.api.beans.HColumn; 8 | import me.prettyprint.hector.api.beans.OrderedRows; 9 | import me.prettyprint.hector.api.exceptions.HectorException; 10 | import me.prettyprint.hector.api.factory.HFactory; 11 | import me.prettyprint.hector.api.mutation.MutationResult; 12 | import me.prettyprint.hector.api.mutation.Mutator; 13 | import me.prettyprint.hector.api.query.ColumnQuery; 14 | import me.prettyprint.hector.api.query.QueryResult; 15 | import me.prettyprint.hector.api.query.RangeSlicesQuery; 16 | 17 | /** 18 | * Shows off the new ExecutionResult hierarchy 19 | * 20 | * To run this example from maven: 21 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.ResultDetailsDemo" 22 | * 23 | * @author zznate 24 | * 25 | */ 26 | public class ResultDetailsDemo { 27 | 28 | private static StringSerializer stringSerializer = StringSerializer.get(); 29 | 30 | public static void main(String[] args) throws Exception { 31 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 32 | 33 | Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); 34 | try { 35 | Mutator mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); 36 | // add 10 rows 37 | for (int i = 0; i < 10; i++) { 38 | mutator.addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i)) 39 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i)) 40 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i)); 41 | } 42 | MutationResult me = mutator.execute(); 43 | System.out.println("MutationResult from 10 row insertion: " + me); 44 | 45 | RangeSlicesQuery rangeSlicesQuery = 46 | HFactory.createRangeSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer); 47 | rangeSlicesQuery.setColumnFamily("Standard1"); 48 | rangeSlicesQuery.setKeys("", ""); 49 | rangeSlicesQuery.setRange("", "", false, 3); 50 | 51 | rangeSlicesQuery.setRowCount(10); 52 | QueryResult> result = rangeSlicesQuery.execute(); 53 | System.out.println("Result from rangeSlices query: " + result.toString()); 54 | 55 | ColumnQuery columnQuery = HFactory.createStringColumnQuery(keyspaceOperator); 56 | columnQuery.setColumnFamily("Standard1").setKey("fake_key_0").setName("fake_column_0"); 57 | QueryResult> colResult = columnQuery.execute(); 58 | System.out.println("Execution time: " + colResult.getExecutionTimeMicro()); 59 | System.out.println("CassandraHost used: " + colResult.getHostUsed()); 60 | System.out.println("Query Execute: " + colResult.getQuery()); 61 | 62 | } catch (HectorException he) { 63 | he.printStackTrace(); 64 | } 65 | cluster.getConnectionManager().shutdown(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/PaginateGetRangeSlices.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | 4 | import me.prettyprint.cassandra.serializers.StringSerializer; 5 | import me.prettyprint.hector.api.Cluster; 6 | import me.prettyprint.hector.api.Keyspace; 7 | import me.prettyprint.hector.api.beans.OrderedRows; 8 | import me.prettyprint.hector.api.beans.Row; 9 | import me.prettyprint.hector.api.exceptions.HectorException; 10 | import me.prettyprint.hector.api.factory.HFactory; 11 | import me.prettyprint.hector.api.mutation.Mutator; 12 | import me.prettyprint.hector.api.query.QueryResult; 13 | import me.prettyprint.hector.api.query.RangeSlicesQuery; 14 | 15 | /** 16 | * A simple example showing what it takes to page over results using 17 | * get_range_slices. 18 | * 19 | * To run this example from maven: 20 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.PaginateGetRangeSlices" 21 | * 22 | * @author zznate 23 | * 24 | */ 25 | public class PaginateGetRangeSlices { 26 | 27 | private static StringSerializer stringSerializer = StringSerializer.get(); 28 | 29 | public static void main(String[] args) throws Exception { 30 | 31 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 32 | 33 | Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); 34 | 35 | try { 36 | Mutator mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); 37 | 38 | for (int i = 0; i < 20; i++) { 39 | mutator.addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i)) 40 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i)) 41 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i)); 42 | } 43 | mutator.execute(); 44 | 45 | RangeSlicesQuery rangeSlicesQuery = 46 | HFactory.createRangeSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer); 47 | rangeSlicesQuery.setColumnFamily("Standard1"); 48 | rangeSlicesQuery.setKeys("", ""); 49 | rangeSlicesQuery.setRange("", "", false, 3); 50 | 51 | rangeSlicesQuery.setRowCount(11); 52 | QueryResult> result = rangeSlicesQuery.execute(); 53 | OrderedRows orderedRows = result.get(); 54 | 55 | 56 | Row lastRow = orderedRows.peekLast(); 57 | 58 | System.out.println("Contents of rows: \n"); 59 | for (Row r : orderedRows) { 60 | System.out.println(" " + r); 61 | } 62 | System.out.println("Should have 11 rows: " + orderedRows.getCount()); 63 | 64 | rangeSlicesQuery.setKeys(lastRow.getKey(), ""); 65 | orderedRows = rangeSlicesQuery.execute().get(); 66 | 67 | System.out.println("2nd page Contents of rows: \n"); 68 | for (Row row : orderedRows) { 69 | System.out.println(" " + row); 70 | } 71 | 72 | } catch (HectorException he) { 73 | he.printStackTrace(); 74 | } 75 | cluster.getConnectionManager().shutdown(); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/SchemaManipulation.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import me.prettyprint.cassandra.model.BasicColumnDefinition; 7 | import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition; 8 | import me.prettyprint.cassandra.serializers.StringSerializer; 9 | import me.prettyprint.cassandra.service.ThriftCfDef; 10 | import me.prettyprint.hector.api.Cluster; 11 | import me.prettyprint.hector.api.ddl.*; 12 | import me.prettyprint.hector.api.exceptions.HectorException; 13 | import me.prettyprint.hector.api.factory.HFactory; 14 | 15 | /** 16 | * Creates a keyspace and adds a column family. This column family 17 | * contains an index on the column named 'birthdate' which is exptected 18 | * to be a Long. 19 | * 20 | * To run this example from maven: 21 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.SchemaManipulation" 22 | * 23 | * @author zznate 24 | * 25 | */ 26 | public class SchemaManipulation { 27 | 28 | private static final String DYN_KEYSPACE = "DynamicKeyspace"; 29 | private static final String DYN_CF = "DynamicCf"; 30 | private static final String CF_SUPER = "SuperCf"; 31 | 32 | private static StringSerializer stringSerializer = StringSerializer.get(); 33 | 34 | public static void main(String[] args) throws Exception { 35 | 36 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 37 | 38 | try { 39 | if ( cluster.describeKeyspace(DYN_KEYSPACE) != null ) { 40 | cluster.dropKeyspace(DYN_KEYSPACE); 41 | } 42 | 43 | BasicColumnDefinition columnDefinition = new BasicColumnDefinition(); 44 | columnDefinition.setName(stringSerializer.toByteBuffer("birthdate")); 45 | columnDefinition.setIndexName("birthdate_idx"); 46 | columnDefinition.setIndexType(ColumnIndexType.KEYS); 47 | columnDefinition.setValidationClass(ComparatorType.LONGTYPE.getClassName()); 48 | 49 | BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition(); 50 | columnFamilyDefinition.setKeyspaceName(DYN_KEYSPACE); 51 | columnFamilyDefinition.setName(DYN_CF); 52 | columnFamilyDefinition.addColumnDefinition(columnDefinition); 53 | 54 | BasicColumnFamilyDefinition superCfDefinition = new BasicColumnFamilyDefinition(); 55 | superCfDefinition.setKeyspaceName(DYN_KEYSPACE); 56 | superCfDefinition.setName(CF_SUPER); 57 | superCfDefinition.setColumnType(ColumnType.SUPER); 58 | 59 | 60 | 61 | ColumnFamilyDefinition cfDefStandard = new ThriftCfDef(columnFamilyDefinition); 62 | ColumnFamilyDefinition cfDefSuper = new ThriftCfDef(superCfDefinition); 63 | 64 | KeyspaceDefinition keyspaceDefinition = 65 | HFactory.createKeyspaceDefinition(DYN_KEYSPACE, "org.apache.cassandra.locator.SimpleStrategy", 66 | 1, Arrays.asList(cfDefStandard, cfDefSuper)); 67 | 68 | cluster.addKeyspace(keyspaceDefinition); 69 | 70 | // insert some data 71 | 72 | List keyspaces = cluster.describeKeyspaces(); 73 | for (KeyspaceDefinition kd : keyspaces) { 74 | if ( kd.getName().equals(DYN_KEYSPACE) ) { 75 | System.out.println("Name: " +kd.getName()); 76 | System.out.println("RF: " +kd.getReplicationFactor()); 77 | System.out.println("strategy class: " +kd.getStrategyClass()); 78 | List cfDefs = kd.getCfDefs(); 79 | for (ColumnFamilyDefinition def : cfDefs) { 80 | System.out.println(" CF Type: " +def.getColumnType()); 81 | System.out.println(" CF Name: " +def.getName()); 82 | System.out.println(" CF Metadata: " +def.getColumnMetadata()); 83 | } 84 | 85 | 86 | } 87 | } 88 | 89 | 90 | } catch (HectorException he) { 91 | he.printStackTrace(); 92 | } 93 | cluster.getConnectionManager().shutdown(); 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/riptano/cassandra/hector/example/TombstonedGetRangeSlices.java: -------------------------------------------------------------------------------- 1 | package com.riptano.cassandra.hector.example; 2 | 3 | import me.prettyprint.cassandra.serializers.StringSerializer; 4 | 5 | import me.prettyprint.hector.api.Cluster; 6 | import me.prettyprint.hector.api.Keyspace; 7 | import me.prettyprint.hector.api.beans.ColumnSlice; 8 | import me.prettyprint.hector.api.beans.OrderedRows; 9 | import me.prettyprint.hector.api.beans.Row; 10 | import me.prettyprint.hector.api.exceptions.HectorException; 11 | import me.prettyprint.hector.api.factory.HFactory; 12 | import me.prettyprint.hector.api.mutation.Mutator; 13 | import me.prettyprint.hector.api.query.QueryResult; 14 | import me.prettyprint.hector.api.query.RangeSlicesQuery; 15 | import me.prettyprint.hector.api.query.SliceQuery; 16 | 17 | /** 18 | * Shows what tombstoned data looks like in the result sets of 19 | * get_range_slices and get_slice 20 | * 21 | * To run this example from maven: 22 | * mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.TombstonedGetRangeSlices" 23 | * 24 | * @author zznate 25 | * 26 | */ 27 | public class TombstonedGetRangeSlices { 28 | 29 | private static StringSerializer stringSerializer = StringSerializer.get(); 30 | // - add data from mutator 31 | // - delete the odd rows 32 | // - display results 33 | 34 | public static void main(String[] args) throws Exception { 35 | Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); 36 | 37 | Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); 38 | try { 39 | Mutator mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); 40 | // add 10 rows 41 | for (int i = 0; i < 10; i++) { 42 | mutator.addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i)) 43 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i)) 44 | .addInsertion("fake_key_" + i, "Standard1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i)); 45 | } 46 | mutator.execute(); 47 | mutator.discardPendingMutations(); 48 | // delete the odd rows 49 | for (int i = 0; i < 10; i++) { 50 | if ( i % 2 == 0 ) continue; 51 | mutator.addDeletion("fake_key_"+i, "Standard1", null, stringSerializer); 52 | } 53 | mutator.execute(); 54 | 55 | RangeSlicesQuery rangeSlicesQuery = 56 | HFactory.createRangeSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer); 57 | rangeSlicesQuery.setColumnFamily("Standard1"); 58 | rangeSlicesQuery.setKeys("", ""); 59 | rangeSlicesQuery.setRange("", "", false, 3); 60 | 61 | rangeSlicesQuery.setRowCount(10); 62 | QueryResult> result = rangeSlicesQuery.execute(); 63 | OrderedRows orderedRows = result.get(); 64 | for (Row row : orderedRows) { 65 | int keyNum = Integer.valueOf(row.getKey().substring(9)); 66 | System.out.println("+-----------------------------------"); 67 | if ( keyNum % 2 == 0 ) { 68 | System.out.println("| result key:" + row.getKey() + " which should have values: " + row.getColumnSlice()); 69 | } else { 70 | System.out.println("| TOMBSTONED result key:" + row.getKey() + " has values: " + row.getColumnSlice()); 71 | } 72 | SliceQuery q = HFactory.createSliceQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer); 73 | q.setColumnFamily("Standard1"); 74 | q.setRange("", "", false, 3); 75 | q.setKey(row.getKey()); 76 | 77 | QueryResult> r = q.execute(); 78 | System.out.println("|-- called directly via get_slice, the value is: " +r); 79 | // For a tombstone, you just get a null back from ColumnQuery 80 | System.out.println("|-- try the first column via getColumn: " + HFactory.createColumnQuery(keyspaceOperator, 81 | stringSerializer, stringSerializer, stringSerializer).setColumnFamily("Standard1").setKey(row.getKey()).setName("fake_column_0").execute()); 82 | 83 | System.out.println("|-- verify on CLI with: get Keyspace1.Standard1['" + row.getKey() + "'] "); 84 | } 85 | 86 | 87 | } catch (HectorException he) { 88 | he.printStackTrace(); 89 | } 90 | cluster.getConnectionManager().shutdown(); 91 | } 92 | } 93 | --------------------------------------------------------------------------------