com.hmsonline.cassandra.triggers.CassandraStore Maven / Gradle / Ivy
package com.hmsonline.cassandra.triggers;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.CassandraServer;
import org.apache.cassandra.thrift.CfDef;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnDef;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.IndexType;
import org.apache.cassandra.thrift.KsDef;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CassandraStore {
private static Logger logger = LoggerFactory.getLogger(CassandraStore.class);
private boolean initialized = false;
private String keyspace = null;
private String columnFamily = null;
protected CassandraStore(String keyspace, String columnFamily) throws Exception {
this.keyspace = keyspace;
this.columnFamily = columnFamily;
this.create(new String[] {});
}
protected CassandraStore(String keyspace, String columnFamily, String[] indexedColumns) throws Exception {
this.keyspace = keyspace;
this.columnFamily = columnFamily;
this.create(indexedColumns);
}
Cassandra.Iface getConnection(String keyspace) throws Exception {
CassandraServer server = new CassandraServer();
if (keyspace != null) {
server.set_keyspace(keyspace);
}
return server;
}
public synchronized void create(String[] indexedColumns) throws Exception {
if (!initialized) {
try {
List cfDefList = new ArrayList();
KsDef ksDef = new KsDef(this.getKeyspace(), "org.apache.cassandra.locator.SimpleStrategy", cfDefList);
ksDef.putToStrategy_options("replication_factor", "1");
getConnection(null).system_add_keyspace(ksDef);
} catch (Exception e) {
logger.debug("Did not create [" + this.getKeyspace() + ":" + this.getColumnFamily()
+ "] (probably already there)");
}
try {
CfDef columnFamily = new CfDef(this.getKeyspace(), this.getColumnFamily());
columnFamily.setKey_validation_class("UTF8Type");
columnFamily.setDefault_validation_class("UTF8Type");
columnFamily.setComparator_type("UTF8Type");
// add indexes on columns
if (indexedColumns != null && indexedColumns.length > 0) {
for (Object indexedColumn : indexedColumns) {
if (indexedColumn != null) {
String indexedColumnStr = indexedColumn.toString();
if (StringUtils.isNotBlank(indexedColumnStr)) {
List columnMetadata = columnFamily.getColumn_metadata();
columnMetadata = columnMetadata != null ? columnMetadata : new ArrayList();
ColumnDef colDef = new ColumnDef();
colDef.setName(indexedColumnStr.getBytes());
colDef.index_type = IndexType.KEYS;
colDef.setIndex_name(keyspace + "_" + this.columnFamily + "_" + indexedColumnStr
+ "_INDEX");
columnMetadata.add(colDef);
columnFamily.setColumn_metadata(columnMetadata);
}
}
}
}
getConnection(this.getKeyspace()).system_add_column_family(columnFamily);
initialized = true;
} catch (Exception e) {
logger.debug("Did not create [" + this.getKeyspace() + ":" + this.getColumnFamily()
+ "] (probably already there)");
}
}
}
public String getKeyspace() {
return keyspace;
}
public String getColumnFamily() {
return columnFamily;
}
// Utility Methods
protected Mutation getMutation(String name, String value) {
return getMutation(name, ByteBufferUtil.bytes(value));
}
protected Mutation getMutation(String name, ByteBuffer value) {
return getMutation(ByteBufferUtil.bytes(name), value);
}
protected Mutation getMutation(ByteBuffer name, OperationType value) {
return getMutation(name, ByteBufferUtil.bytes(value.toString()));
}
protected Mutation getMutation(ByteBuffer name, ByteBuffer value) {
Column c = new Column();
c.setName(name);
c.setValue(value);
c.setTimestamp(System.currentTimeMillis() * 1000);
Mutation m = new Mutation();
ColumnOrSuperColumn cc = new ColumnOrSuperColumn();
cc.setColumn(c);
m.setColumn_or_supercolumn(cc);
return m;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy