Skip to content

Instantly share code, notes, and snippets.

@emaxerrno
Created March 26, 2012 19:27
Show Gist options
  • Save emaxerrno/2208947 to your computer and use it in GitHub Desktop.
Save emaxerrno/2208947 to your computer and use it in GitHub Desktop.
DynamicComposite sliceQuery not working or I don't get it
import java.util.Arrays;
import java.util.List;
import me.prettyprint.cassandra.model.HColumnImpl;
import me.prettyprint.cassandra.serializers.BytesArraySerializer;
import me.prettyprint.cassandra.serializers.DynamicCompositeSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.ColumnSlice;
import me.prettyprint.hector.api.beans.DynamicComposite;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
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;
public class HectorTest {
public static String KEYSPACE_NAME = "AwesomeKeySpace";
public static String CF = "Family";
public static String ReplicationStrategy = "SimpleStrategy";
public static String ClusterName = "Test Culster";
public static String Host = "10.3.2.12:9160";
public static int ReplicationFactor = 1;
public static Keyspace getKeySpace() {
Cluster cluster = HFactory.getOrCreateCluster(ClusterName, Host);
KeyspaceDefinition keyspaceDef = cluster
.describeKeyspace(KEYSPACE_NAME);
if (keyspaceDef == null) {
// Step 2: Create ALL the Column Families here.
// ----- Creates the column family for User Events
ColumnFamilyDefinition cfdUserEvents = HFactory
.createColumnFamilyDefinition(KEYSPACE_NAME, CF,
// for column keys!
ComparatorType.DYNAMICCOMPOSITETYPE);
cfdUserEvents.setComparatorTypeAlias("(l=> LongType)");
// for row keys
cfdUserEvents.setKeyValidationClass("BytesType");
// column values, not keys
cfdUserEvents.setDefaultValidationClass(ComparatorType.BYTESTYPE
.getClassName());
// ----
KeyspaceDefinition myKs = HFactory.createKeyspaceDefinition(
KEYSPACE_NAME, ReplicationStrategy, ReplicationFactor,
Arrays.asList(cfdUserEvents));
// Step 3: Add schema to the cluster and waits for agreement from
// cluster
cluster.addKeyspace(myKs, true);
}
return HFactory.createKeyspace(KEYSPACE_NAME, cluster);
}
public static void main(String... args) {
Keyspace ksp = getKeySpace();
Mutator<byte[]> mutator = HFactory.createMutator(ksp,
BytesArraySerializer.get());
HColumnImpl<DynamicComposite, byte[]> column = new HColumnImpl<DynamicComposite, byte[]>(
DynamicCompositeSerializer.get(), BytesArraySerializer.get());
column.setClock(ksp.createClock());
byte[] rowKey = "uniqueKey".getBytes();
DynamicComposite colKey = new DynamicComposite();
colKey.add(0, 0L);
colKey.add(1, 0L);
colKey.add(2, 1L);
colKey.add(3, 0L);
colKey.add(4, 0L);
column.setName(colKey);
column.setValue("Value".getBytes());
// insert on RowKey:
mutator.insert(rowKey, CF, column);
// NOW lets search.
DynamicComposite start = new DynamicComposite();
DynamicComposite end = new DynamicComposite();
// dummy values for type
start.add(0, 0L);
end.add(0, Long.MAX_VALUE);
start.add(1, 0L);
end.add(1, Long.MAX_VALUE);
start.add(2, 0L);
end.add(2, 0L); // THIS SHOULD FAIL ??? Set to 1 for correct range?
start.add(3, 0L);
end.add(3, Long.MAX_VALUE);
start.add(4, 0L);
end.add(4, Long.MAX_VALUE);
SliceQuery<byte[], DynamicComposite, byte[]> sliceQuery = HFactory
.createSliceQuery(ksp, BytesArraySerializer.get(),
DynamicCompositeSerializer.get(),
BytesArraySerializer.get());
sliceQuery.setColumnFamily(CF);
sliceQuery.setKey(rowKey);
sliceQuery.setRange(start, end, false /* reversed? */, Integer.MAX_VALUE);
QueryResult<ColumnSlice<DynamicComposite, byte[]>> result = sliceQuery
.execute();
List<HColumn<DynamicComposite, byte[]>> listCols = result.get()
.getColumns();
for (HColumn<DynamicComposite, byte[]> col : listCols) {
System.out.println(new String(col.getValue()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment