Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zznate
committed
Jun 18, 2010
1 parent
2ff07a6
commit 222006f
Showing
16 changed files
with
741 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Overview | ||
Provides example classes for using Hector to access Apache Cassandra | ||
|
||
The classes in com.vrvm.cassandra.hector.example package can all be executed | ||
from maven via the following command: | ||
mvn -e exec:java -Dexec.mainClass="com.vrvm.cassandra.hector.example.[class name]" | ||
|
||
These classes assume you have an instance of Cassandra running on localhost:9160 with the default configuration. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.vrvm.cassandra.hector</groupId> | ||
<artifactId>hector-examples</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<name>hector-examples</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.6</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.cliffc.high_scale_lib</groupId> | ||
<artifactId>high-scale-lib</artifactId> | ||
<version>1.0.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.reardencommerce</groupId> | ||
<artifactId>clhm-production</artifactId> | ||
<version>1.0.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
<version>2.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cassandra</groupId> | ||
<artifactId>cassandra-javautils</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>me.prettyprint</groupId> | ||
<artifactId>hector</artifactId> | ||
<version>0.6.0-15</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.5.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>log4j-over-slf4j</artifactId> | ||
<version>1.5.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-core</artifactId> | ||
<version>0.9.18</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>libthrift</groupId> | ||
<artifactId>libthrift</artifactId> | ||
<version>917130</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cassandra</groupId> | ||
<artifactId>cassandra</artifactId> | ||
<version>0.6.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>1.3.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-collections</groupId> | ||
<artifactId>commons-collections</artifactId> | ||
<version>3.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
<optimize>true</optimize> | ||
<debug>true</debug> | ||
<showDeprecation>true</showDeprecation> | ||
<showWarnings>true</showWarnings> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>2.3.1</version> | ||
<executions> | ||
<execution> | ||
<id>install libthrift</id> | ||
<phase>initialize</phase> | ||
<goals> | ||
<goal>install-file</goal> | ||
</goals> | ||
<configuration> | ||
<packaging>jar</packaging> | ||
<groupId>libthrift</groupId> | ||
<artifactId>libthrift</artifactId> | ||
<version>917130</version> | ||
<file>${basedir}/lib/libthrift-r917130.jar</file> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>install cassandra</id> | ||
<phase>initialize</phase> | ||
<goals> | ||
<goal>install-file</goal> | ||
</goals> | ||
<configuration> | ||
<packaging>jar</packaging> | ||
<groupId>org.apache.cassandra</groupId> | ||
<artifactId>cassandra</artifactId> | ||
<version>0.6.0</version> | ||
<file>${basedir}/lib/apache-cassandra-0.6.0.jar</file> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>install high-scale-lib</id> | ||
<phase>initialize</phase> | ||
<goals> | ||
<goal>install-file</goal> | ||
</goals> | ||
<configuration> | ||
<packaging>jar</packaging> | ||
<groupId>org.cliffc.high_scale_lib</groupId> | ||
<artifactId>high-scale-lib</artifactId> | ||
<version>1.0.0</version> | ||
<file>${basedir}/lib/high-scale-lib.jar</file> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>install clhm-production</id> | ||
<phase>initialize</phase> | ||
<goals> | ||
<goal>install-file</goal> | ||
</goals> | ||
<configuration> | ||
<packaging>jar</packaging> | ||
<groupId>com.reardencommerce</groupId> | ||
<artifactId>clhm-production</artifactId> | ||
<version>1.0.0</version> | ||
<file>${basedir}/lib/clhm-production.jar</file> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>install cassandra-javautils</id> | ||
<phase>initialize</phase> | ||
<goals> | ||
<goal>install-file</goal> | ||
</goals> | ||
<configuration> | ||
<packaging>jar</packaging> | ||
<groupId>org.apache.cassandra</groupId> | ||
<artifactId>cassandra-javautils</artifactId> | ||
<version>1.0.0</version> | ||
<file>${basedir}/lib/cassandra-javautils.jar</file> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>install hector</id> | ||
<phase>initialize</phase> | ||
<goals> | ||
<goal>install-file</goal> | ||
</goals> | ||
<configuration> | ||
<packaging>jar</packaging> | ||
<groupId>me.prettyprint</groupId> | ||
<artifactId>hector</artifactId> | ||
<version>0.6.0-15</version> | ||
<file>${basedir}/lib/hector-0.6.0-15.jar</file> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<!-- mvn exec:java -Dexec.mainClass="com.example.Main" --> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.1</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/vrvm/cassandra/hector/example/DeleteBatchMutate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.vrvm.cassandra.hector.example; | ||
|
||
import java.util.Arrays; | ||
|
||
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.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. | ||
* | ||
* To run this example from maven: | ||
* mvn -e exec:java -Dexec.mainClass="com.vrvm.cassandra.hector.example.DeleteBatchMutate" | ||
* | ||
* @author zznate | ||
* | ||
*/ | ||
public class DeleteBatchMutate { | ||
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"); | ||
|
||
SlicePredicate slicePredicate = new SlicePredicate(); | ||
slicePredicate.addToColumn_names(StringUtils.bytes("last")); | ||
slicePredicate.addToColumn_names(StringUtils.bytes("middle")); | ||
Deletion deletion = new Deletion(keyspace.createTimestamp()); | ||
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("Verify on CLI with: get Keyspace1.Standard1['jsmith'] "); | ||
System.out.println("Should only return the 'first' column."); | ||
} finally { | ||
pool.releaseClient(keyspace.getClient()); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/vrvm/cassandra/hector/example/DeleteSingleColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.vrvm.cassandra.hector.example; | ||
|
||
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.ColumnPath; | ||
|
||
/** | ||
* Deletes the "first" Column for the key "jsmith" from Standard1 ColumnFamily. | ||
* | ||
* To run this example from maven: | ||
* mvn -e exec:java -Dexec.mainClass="com.vrvm.cassandra.hector.example.DeleteSingleColumn" | ||
* | ||
* @author zznate | ||
*/ | ||
public class DeleteSingleColumn { | ||
|
||
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"); | ||
ColumnPath columnPath = new ColumnPath("Standard1"); | ||
// leave out the line below to delete all columns for the "jsmith" key | ||
columnPath.setColumn(StringUtils.bytes("first")); | ||
keyspace.remove("jsmith", columnPath); | ||
|
||
System.out.println("Deletion successful"); | ||
System.out.println("Verify on CLI with: get Keyspace1.Standard1['jsmith'] "); | ||
System.out.println("Should return 0 results."); | ||
} finally { | ||
// return client to pool. do it in a finally block to make sure it's executed | ||
pool.releaseClient(keyspace.getClient()); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/vrvm/cassandra/hector/example/GetRangeSlicesKeysOnly.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.vrvm.cassandra.hector.example; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
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.ColumnPath; | ||
import org.apache.cassandra.thrift.KeyRange; | ||
import org.apache.cassandra.thrift.SlicePredicate; | ||
import org.apache.cassandra.thrift.SliceRange; | ||
|
||
/** | ||
* Use get_range_slices to retrieve the keys without deserializing the columns. | ||
* For clear results, it's best to run this on an empty ColumnFamily. | ||
* | ||
* To run this example from maven: | ||
* mvn -e exec:java -Dexec.mainClass="com.vrvm.cassandra.hector.example.GetRangeSlicesKeysOnly" | ||
* | ||
* @author zznate | ||
* | ||
*/ | ||
public class GetRangeSlicesKeysOnly { | ||
|
||
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)); | ||
|
||
cp.setColumn(StringUtils.bytes("fake_column_2")); | ||
keyspace.insert("fake_key_"+i, cp, StringUtils.bytes("fake_value_2_" + i)); | ||
} | ||
|
||
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(); | ||
|
||
// 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); | ||
|
||
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 + "'] "); | ||
} | ||
|
||
} finally { | ||
pool.releaseClient(keyspace.getClient()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.