Created
July 14, 2011 20:15
-
-
Save zznate/1083338 to your computer and use it in GitHub Desktop.
sample code
This file contains hidden or 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
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.UUID; | |
import me.prettyprint.cassandra.serializers.BytesArraySerializer; | |
import me.prettyprint.cassandra.serializers.StringSerializer; | |
import me.prettyprint.cassandra.serializers.UUIDSerializer; | |
import me.prettyprint.cassandra.service.template.ColumnFamilyResult; | |
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate; | |
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate; | |
import me.prettyprint.cassandra.utils.TimeUUIDUtils; | |
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.HColumn; | |
import me.prettyprint.hector.api.beans.HSuperColumn; | |
import me.prettyprint.hector.api.beans.Row; | |
import me.prettyprint.hector.api.beans.Rows; | |
import me.prettyprint.hector.api.beans.SuperRows; | |
import me.prettyprint.hector.api.beans.SuperSlice; | |
import me.prettyprint.hector.api.exceptions.HectorException; | |
import me.prettyprint.hector.api.factory.HFactory; | |
import me.prettyprint.hector.api.mutation.Mutator; | |
import me.prettyprint.hector.api.query.QueryResult; | |
public class TestingPayloadByModelStd { | |
private static final StringSerializer stringSer = StringSerializer.get(); | |
// private static final BigIntegerSerializer bigintSer = BigIntegerSerializer.get(); | |
private static final UUIDSerializer uuidSer = UUIDSerializer.get(); | |
private static final BytesArraySerializer byteArraySer = BytesArraySerializer.get(); | |
// FIXME | |
// paylaod -> should be payload | |
private static final String CN_PAYLOAD = "payload"; | |
// private static final String CF_PAYLOAD = "Payload"; | |
// private static final String CF_PAYLOAD_BY_MODEL = "PayloadByModel"; | |
private static final String CF_PAYLOAD = "Super5"; | |
private static final String CF_PAYLOAD_BY_MODEL = "Std6"; | |
private static final int ONE_DAY_IN_SECONDS = 86400; //60 * 60 * 24; | |
private static final int CF_PAYLOAD_COLUMN_TTL = ONE_DAY_IN_SECONDS * 7; | |
private static String getCompositeKey(String modelName, String key) { | |
return modelName + "/" + key; | |
} | |
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH"); | |
private static String getPayloadKey() { | |
return sdf.format(System.currentTimeMillis()); | |
} | |
public static void insertRadarPayload(Keyspace ksp, | |
String key, | |
byte []payload, String modelName) { | |
// String key = getPayloadKey(); | |
Mutator<String> mutator = HFactory.createMutator(ksp, stringSer); | |
List<HColumn<String, byte []>> columns = new ArrayList<HColumn<String, byte []>>(); | |
columns.add(HFactory.createColumn(CN_PAYLOAD, payload, | |
CF_PAYLOAD_COLUMN_TTL, stringSer, byteArraySer)); | |
UUID uuid = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); | |
mutator.addInsertion(key, | |
CF_PAYLOAD, | |
HFactory.createSuperColumn(uuid, columns, uuidSer, stringSer, byteArraySer)); | |
if (modelName != null) { | |
mutator.addInsertion(getCompositeKey(modelName, key), | |
CF_PAYLOAD_BY_MODEL, | |
HFactory.createColumn(uuid, "", uuidSer, stringSer)); | |
} | |
mutator.execute(); | |
} | |
public static List<byte []> getRadarPayloadsByModel(Keyspace ksp, String key, | |
String modelName) { | |
ColumnSlice<UUID, String> columnSlice = | |
HFactory.createSliceQuery(ksp, stringSer, uuidSer, stringSer) | |
.setColumnFamily(CF_PAYLOAD_BY_MODEL) | |
.setKey(getCompositeKey(modelName, key)) | |
.setRange(null, null, false, 100) | |
.execute() | |
.get(); | |
System.out.println("getRadarPayloadsByModel: " + columnSlice.toString()); | |
List<UUID> list = new ArrayList<UUID>(columnSlice.getColumns().size()); | |
for (HColumn<UUID, String> sc : columnSlice.getColumns()) { | |
System.out.println(sc.toString()); | |
list.add(sc.getName()); | |
} | |
return getRadarPayloads(ksp, list); | |
} | |
public static List<byte []> getRadarPayloads(Keyspace ksp, List<UUID> uuidList) { | |
System.out.println("\n" + uuidList.toString()); | |
// WORKS FINE - can see the see the column payload | |
// SuperRows<String, UUID, String, byte []> sr = | |
// HFactory.createMultigetSuperSliceQuery(ksp, stringSer, uuidSer, stringSer, byteArraySer) | |
// .setColumnFamily(CF_PAYLOAD) | |
// .setKeys(Arrays.asList("2011:07:08:10", "2011:07:08:11")) | |
// .setRange(null, null, false, 1000) | |
// .execute() | |
// .get(); | |
// | |
// System.out.println(sr); | |
// works fine - can see the column payload | |
// SuperRows<String, UUID, String, byte []> sr = | |
// HFactory.createMultigetSuperSliceQuery(ksp, stringSer, uuidSer, stringSer, byteArraySer) | |
// .setColumnFamily(CF_PAYLOAD) | |
// .setRange(null, null, false, 100) | |
// .setKeys(Arrays.asList("2011:07:08:10", "2011:07:08:11")) | |
// .execute() | |
// .get(); | |
// System.out.println(sr); | |
Rows<UUID, String, byte[]> rows = | |
HFactory.createMultigetSliceQuery(ksp, uuidSer, stringSer, byteArraySer) | |
.setColumnFamily(CF_PAYLOAD) | |
.setRange(null, null, false, 100) | |
.setKeys(uuidList) | |
.execute() | |
.get(); | |
System.out.println("rows: " + rows); | |
List<byte []> list = new ArrayList<byte []>(rows.getCount()); | |
for (Row<UUID, String, byte []> row : rows) { | |
System.out.println(row); | |
} | |
// for (UUID uuid : uuidList) { | |
// boolean columnInfoAvailable = false; | |
// for (HColumn<String, byte []> c : rows.getByKey(uuid).getColumnSlice().getColumns()) { | |
// System.out.println("column info: " + c.toString()); | |
// columnInfoAvailable = true; | |
// } | |
// if (columnInfoAvailable == false) { | |
// System.out.println("column info not available"); | |
// } | |
// HColumn<String, byte []> column = rows.getByKey(uuid).getColumnSlice().getColumnByName(CN_PAYLOAD); | |
// if (column != null) { | |
// list.add(column.getValue()); | |
// } | |
// } | |
return list; | |
} | |
public static List<byte []> getRadarPayloads(Keyspace ksp, String key) { | |
QueryResult<SuperSlice<UUID, String, byte []>> result = | |
HFactory.createSuperSliceQuery(ksp, stringSer, uuidSer, stringSer, byteArraySer) | |
.setColumnFamily(CF_PAYLOAD) | |
.setKey(key) | |
.setRange(null, null, false, 1000) | |
.execute(); | |
List<byte []> list = new ArrayList<byte []>(result.get().getSuperColumns().size()); | |
for (HSuperColumn<UUID, String, byte []> sc : result.get().getSuperColumns()) { | |
for (HColumn<String, byte []> col : sc.getColumns()) { | |
if (col.getName().equals(CN_PAYLOAD)) { | |
list.add(col.getValue()); | |
} | |
} | |
} | |
return list; | |
} | |
public static void retrieveRadarPayloads(Keyspace ksp, String key) { | |
QueryResult<SuperSlice<UUID, String, byte []>> result = | |
HFactory.createSuperSliceQuery(ksp, stringSer, uuidSer, stringSer, byteArraySer) | |
.setColumnFamily(CF_PAYLOAD) | |
.setKey(key) | |
.setRange(null, null, false, 2000) | |
.execute(); | |
System.out.println("query: " + result.getQuery().toString()); | |
SuperSlice<UUID, String, byte []> slice = result.get(); | |
System.out.println("Results for key: " + key); | |
for (HSuperColumn<UUID, String, byte []> sc : slice.getSuperColumns()) { | |
for (HColumn<String, byte []> hc : sc.getColumns()) { | |
System.out.format(" %s=%s%n", hc.getName(), new String(hc.getValue())); | |
} | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
String hosts = "localhost:9160"; | |
Cluster cluster = HFactory.getOrCreateCluster("TestCluster", hosts); | |
String keys[] = { "2011:07:08:10", "2011:07:08:11", "2011:07:08:12", | |
"2011:07:08:13", }; | |
String model[] = { "model1", "model2", "model1", | |
"model2", }; | |
try { | |
Keyspace ksp = HFactory.createKeyspace("Keyspace1", cluster); | |
int idx = 0; | |
String modelName; | |
for (String key : keys) { | |
modelName = model[idx++]; | |
for (int i = 0; i < 2; i++) { | |
String payload = key + "_" + modelName + "=> payload data - " + i; | |
insertRadarPayload(ksp, key, payload.getBytes("UTF-8"), modelName); | |
} | |
} | |
for (String key : keys) { | |
retrieveRadarPayloads(ksp, key); | |
} | |
String key = "2011:07:08:12"; | |
System.out.println("payloads for key " + key); | |
for (byte []bytes : getRadarPayloads(ksp, key)) { | |
System.out.println(" " + new String(bytes)); | |
} | |
modelName = "model1"; | |
System.out.println("payloads for model " + modelName); | |
for (byte []bytes : getRadarPayloadsByModel(ksp, "2011:07:08:12", modelName)) { | |
System.out.println("payload: " + new String(bytes)); | |
} | |
} | |
catch (HectorException e) { | |
e.printStackTrace(); | |
} | |
cluster.getConnectionManager().shutdown(); | |
System.out.println("End..."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment