-
Notifications
You must be signed in to change notification settings - Fork 353
Home
elandau edited this page Jul 29, 2011
·
22 revisions
Welcome to the astyanax wiki! We’re working on getting real documentation up. In the meantime here is some sample code to help you get started.
// Configuring and creating the client
ConnectionPoolConfigurationImpl config = new ConnectionPoolConfigurationImpl("ClusterName", "KeyspaceName");
config.setSeeds("127.0.0.1:9160");
config.setPort(9160);
config.setSocketTimeout(30000);
config.setMaxTimeoutWhenExhausted(30);
config.setClock(new MillisecondsClock());
Keyspace keyspace = new ThriftKeyspaceImpl(config);
keyspace.start();
ColumnFamily<String, String> CF_STANDARD1 =
new ColumnFamily<String, String>(
"Standard1", // Name
StringSerializer.get(), // Key Serializer
StringSerializer.get()); // Column Serializer
// Inserting data
MutationBatch m = keyspace.prepareMutationBatch();
m.withRow(CF_USER_INFO, "acct1234")
.putColumn("address", "555 Elm St", null)
.putColumn("age", 30, null)
.deleteColumn("car”)
.delete();
m.withRow(CF_USER_STATS, “acct1234”)
.incrementCounterColumn("loginCount", 1);
try {
OperationResult<Void> result = m.execute();
} catch (ConnectionException e) {
}
// Querying cassandra for an entire row
OperationResult<ColumnList<String>> result =
ks.prepareQuery(CF_USER_INFO, keyName)
.getKey(“Key1”).
.execute();
ColumnList<String> columns = result.getResult();
// Lookup columns in response by name
int age = columns.getColumnByName("age").getIntegerValue();
long counter = columns.getColumnByName("loginCount").getLongValue();
String address = columns.getColumnByName("address").getStringValue();
// Or, iterate through the columns
for (Column<String> c : result.getResult()) {
System.out.println(c.getName());
}
// Querying cassandra for a set of non-contiguous rows
OperationResult<Rows<String, String>> result =
ks.prepareQuery(CF_STANDARD1)
.getKeySlice(“Key1”, “Key2”, “Key3”)
.execute();
// Iterate rows and their columns
for (Row<String, String> row : result.getResult()) {
System.out.println(row.getKey());
for (Column<String> column : row.getColumns()) {
System.out.println(column.getName());
}
}
// Annotated composite class
Class SessionEvent{
@Component String sessiondId;
@Component TimeUUID timestamp;
}
static AnnotatedCompositeSerializer<SessionEvent> eventSerializer
= new AnnotatedCompositeSerializer<SessionEvent>(SessionEvent.class);
static ColumnFamily<String, SessionEvent> CF_SESSION_EVENTS
= new ColumnFamily<String, SessionEvent>(”SessionEvents",
StringSerializer.get(), eventSerializer);
// Querying cassandra for an entire row
keyspace.prepareQuery(CF_SESSION_EVENTS)
.getKey(”SomeSessionId")
.withColumnRange(
eventSerializer.makeEndpoint(“sessionid1", Equality.EQUAL).toBytes(),
eventSerializer.makeEndpoint("sessionid1", Equality.LESS_THAN_EQUAL).toBytes(),
false, 100)
.execute();
A Netflix Original Production
Tech Blog | Twitter @NetflixOSS | Jobs
- Getting-Started
- Configuration
- Features
- Monitoring
- Thread Safety
- Timeouts
- Recipes
- Examples
- Javadoc
- Utilities
- Cassandra-Compatibility
- FAQ
- End-to-End Examples
- Astyanax Integration with Java Driver