Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new composite and dynamic composite examples
- Loading branch information
zznate
committed
Sep 19, 2011
1 parent
d902366
commit 61167f3
Showing
3 changed files
with
271 additions
and
2 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/main/java/com/datastax/tutorial/DynamicCompositeIndex.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,140 @@ | ||
package com.datastax.tutorial; | ||
|
||
import me.prettyprint.cassandra.model.HColumnImpl; | ||
import me.prettyprint.cassandra.serializers.DynamicCompositeSerializer; | ||
import me.prettyprint.cassandra.serializers.LongSerializer; | ||
import me.prettyprint.hector.api.Keyspace; | ||
import me.prettyprint.hector.api.ResultStatus; | ||
import me.prettyprint.hector.api.beans.AbstractComposite; | ||
import me.prettyprint.hector.api.beans.ColumnSlice; | ||
import me.prettyprint.hector.api.beans.Composite; | ||
import me.prettyprint.hector.api.beans.DynamicComposite; | ||
import me.prettyprint.hector.api.factory.HFactory; | ||
import me.prettyprint.hector.api.mutation.Mutator; | ||
import me.prettyprint.hector.api.query.QueryResult; | ||
import me.prettyprint.hector.api.query.SliceQuery; | ||
|
||
/** | ||
* Uses DynamicComparator to store two different indexes within the same | ||
* key: NPA first and City first. | ||
* | ||
* To run this example from maven: | ||
* mvn -e exec:java -Dexec.args="dynamic_comp_index" -Dexec.mainClass="com.datastax.tutorial.TutorialRunner" | ||
*/ | ||
public class DynamicCompositeIndex extends TutorialCommand { | ||
|
||
private static DynamicCompositeSerializer dcs = new DynamicCompositeSerializer(); | ||
|
||
public DynamicCompositeIndex(Keyspace keyspace) { | ||
super(keyspace); | ||
} | ||
|
||
@Override | ||
public ResultStatus execute() { | ||
Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); | ||
|
||
HColumnImpl<DynamicComposite, String> column = new HColumnImpl<DynamicComposite, String>(dcs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
DynamicComposite dc = new DynamicComposite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 7516L); | ||
dc.add(2, 225L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexDynamic", column); | ||
|
||
column = new HColumnImpl<DynamicComposite, String>(dcs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new DynamicComposite(); | ||
dc.add(0, "225"); | ||
//dc.add(1, 7516L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexDynamic", column); | ||
|
||
column = new HColumnImpl<DynamicComposite, String>(dcs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new DynamicComposite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 7516L); | ||
dc.add(2, 334L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexDynamic", column); | ||
|
||
column = new HColumnImpl<DynamicComposite, String>(dcs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new DynamicComposite(); | ||
dc.add(0, "334"); | ||
dc.add(1, 7516L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexDynamic", column); | ||
|
||
column = new HColumnImpl<DynamicComposite, String>(dcs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new DynamicComposite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 7516L); | ||
dc.add(2, 439L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexDynamic", column); | ||
|
||
column = new HColumnImpl<DynamicComposite, String>(dcs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
|
||
dc = new DynamicComposite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 5830L); | ||
dc.add(2, 215L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexDynamic", column); | ||
|
||
column = new HColumnImpl<DynamicComposite, String>(dcs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new DynamicComposite(); | ||
dc.add(0, "Lockhart"); | ||
dc.add(1, 9533L); | ||
dc.add(2, 227L); | ||
column.setName(dc); | ||
column.setValue("SOUTHWESTERN BELL"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexDynamic", column); | ||
|
||
mutator.execute(); | ||
|
||
SliceQuery<String, DynamicComposite, String> sliceQuery = | ||
HFactory.createSliceQuery(keyspace, stringSerializer, dcs, stringSerializer); | ||
sliceQuery.setColumnFamily("StateNpaIndexDynamic"); | ||
sliceQuery.setKey("TX:512"); | ||
|
||
DynamicComposite startRange = new DynamicComposite(); | ||
//startRange.add(0, "225"); | ||
startRange.add(0, "Austin"); | ||
//startRange.addComponent(new Long(5830), LongSerializer.get(), "LongType", AbstractComposite.ComponentEquality.LESS_THAN_EQUAL); | ||
//startRange.add(1, 5830L); | ||
|
||
|
||
DynamicComposite endRange = new DynamicComposite(); | ||
//endRange.add(0, "225" + Character.MAX_VALUE); | ||
endRange.add(0, "Austin"); | ||
endRange.addComponent(new Long(5830), LongSerializer.get(), "LongType", AbstractComposite.ComponentEquality.GREATER_THAN_EQUAL); | ||
|
||
sliceQuery.setRange(startRange, endRange, false, 10); | ||
|
||
QueryResult<ColumnSlice<DynamicComposite, String>> result = sliceQuery.execute(); | ||
return result; | ||
} | ||
|
||
// findByNpaOnly | ||
// startRange.add(0, "225") | ||
|
||
// rangeToOneResult | ||
// startRange.add(0, "Austin"); | ||
// endRange.add(0, "Austin"); | ||
// endRange.addComponent(new Long(5830), LongSerializer.get(), "LongType", AbstractComposite.ComponentEquality.GREATER_THAN_EQUAL); | ||
|
||
// rangeToCityNpaMax | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/com/datastax/tutorial/StaticCompositeIndex.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,110 @@ | ||
package com.datastax.tutorial; | ||
|
||
import me.prettyprint.cassandra.model.HColumnImpl; | ||
import me.prettyprint.cassandra.serializers.CompositeSerializer; | ||
import me.prettyprint.hector.api.Keyspace; | ||
import me.prettyprint.hector.api.ResultStatus; | ||
import me.prettyprint.hector.api.beans.ColumnSlice; | ||
import me.prettyprint.hector.api.beans.Composite; | ||
import me.prettyprint.hector.api.factory.HFactory; | ||
import me.prettyprint.hector.api.mutation.Mutator; | ||
import me.prettyprint.hector.api.query.QueryResult; | ||
import me.prettyprint.hector.api.query.SliceQuery; | ||
|
||
/** | ||
* | ||
* To run this example from maven: | ||
* mvn -e exec:java -Dexec.args="static_comp_index" -Dexec.mainClass="com.datastax.tutorial.TutorialRunner" | ||
*/ | ||
public class StaticCompositeIndex extends TutorialCommand { | ||
|
||
private static final CompositeSerializer cs = new CompositeSerializer(); | ||
|
||
public StaticCompositeIndex(Keyspace keyspace) { | ||
super(keyspace); | ||
} | ||
|
||
@Override | ||
public ResultStatus execute() { | ||
Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); | ||
|
||
HColumnImpl<Composite, String> column = new HColumnImpl<Composite, String>(cs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
Composite dc = new Composite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 7516L); | ||
dc.add(2, 225L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexStatic", column); | ||
|
||
column = new HColumnImpl<Composite, String>(cs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new Composite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 7516L); | ||
dc.add(2, 334L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexStatic", column); | ||
|
||
column = new HColumnImpl<Composite, String>(cs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new Composite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 7516L); | ||
dc.add(2, 439L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexStatic", column); | ||
|
||
column = new HColumnImpl<Composite, String>(cs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new Composite(); | ||
dc.add(0, "Austin"); | ||
dc.add(1, 5830L); | ||
dc.add(2, 215L); | ||
column.setName(dc); | ||
column.setValue("TIME WARNER COMMUNICATIONS AXS OF AUSTIN, TX"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexStatic", column); | ||
|
||
column = new HColumnImpl<Composite, String>(cs, stringSerializer); | ||
column.setClock(keyspace.createClock()); | ||
dc = new Composite(); | ||
dc.add(0, "Lockhart"); | ||
dc.add(1, 9533L); | ||
dc.add(2, 227L); | ||
column.setName(dc); | ||
column.setValue("SOUTHWESTERN BELL"); | ||
mutator.addInsertion("TX:512", "StateNpaIndexStatic", column); | ||
|
||
mutator.execute(); | ||
|
||
SliceQuery<String, Composite, String> sliceQuery = | ||
HFactory.createSliceQuery(keyspace, stringSerializer, cs, stringSerializer); | ||
sliceQuery.setColumnFamily("StateNpaIndexStatic"); | ||
sliceQuery.setKey("TX:512"); | ||
|
||
Composite startRange = new Composite(); | ||
startRange.add(0, "Austin"); | ||
//startRange.add(1, 7516L); | ||
//startRange.addComponent(new Long(0), LongSerializer.get(), "LongType", AbstractComposite.ComponentEquality.GREATER_THAN_EQUAL); | ||
//startRange.addComponent(439L, LongSerializer.get(), "LongType", AbstractComposite.ComponentEquality.EQUAL); | ||
//startRange.add(1, Long.MIN_VALUE); | ||
//startRange.add(2, 439L); | ||
|
||
|
||
Composite endRange = new Composite(); | ||
endRange.add(0, "Austin" + Character.MAX_VALUE); | ||
// the following statement is effectively identical for the purposes of restricting to 'Austin' | ||
//endRange.addComponent("Austin", StringSerializer.get(), "UTF8Type", AbstractComposite.ComponentEquality.GREATER_THAN_EQUAL); | ||
|
||
sliceQuery.setRange(startRange, endRange, false, 10); | ||
|
||
QueryResult<ColumnSlice<Composite, String>> result = sliceQuery.execute(); | ||
return result; | ||
|
||
|
||
} | ||
|
||
} |
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