/
GetIndexedSlices.java
60 lines (48 loc) · 2.71 KB
/
GetIndexedSlices.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.riptano.cassandra.hector.example;
import me.prettyprint.cassandra.model.IndexedSlicesQuery;
import me.prettyprint.cassandra.model.KeyspaceOperator;
import me.prettyprint.cassandra.model.Mutator;
import me.prettyprint.cassandra.model.OrderedRows;
import me.prettyprint.cassandra.model.Result;
import me.prettyprint.cassandra.serializers.LongSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.Cluster;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
/**
* Shows off the usage of the shiny new Column indexing feature via
* get_indexed_slices.
*
* To run this example from maven:
* mvn -e exec:java -Dexec.mainClass="com.riptano.cassandra.hector.example.GetIndexedSlices"
*
* @author zznate
*
*/
public class GetIndexedSlices {
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);
for (int i = 0; i < 20; i++) {
mutator.addInsertion("fake_key_" + i, "Indexed1", HFactory.createStringColumn("fake_column_0", "fake_value_0_" + i))
.addInsertion("fake_key_" + i, "Indexed1", HFactory.createStringColumn("fake_column_1", "fake_value_1_" + i))
.addInsertion("fake_key_" + i, "Indexed1", HFactory.createStringColumn("fake_column_2", "fake_value_2_" + i))
.addInsertion("fake_key_" + i, "Indexed1", HFactory.createColumn("birthdate",new Long(i%5),stringSerializer,LongSerializer.get()));
}
mutator.execute();
IndexedSlicesQuery<String, String, Long> indexedSlicesQuery =
HFactory.createIndexedSlicesQuery(keyspaceOperator, stringSerializer, stringSerializer, LongSerializer.get());
indexedSlicesQuery.addEqualsExpression("birthdate", 4L);
indexedSlicesQuery.setColumnNames("birthdate","fake_column_0");
indexedSlicesQuery.setColumnFamily("Indexed1");
indexedSlicesQuery.setStartKey("");
Result<OrderedRows<String, String, Long>> result = indexedSlicesQuery.execute();
System.out.println("The results should only have 4 entries: " + result.get());
} catch (HectorException he) {
he.printStackTrace();
}
}
}