Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import org.apache.storm.cassandra.client.CassandraConf;
import org.apache.storm.cassandra.client.SimpleClient;
import org.apache.storm.cassandra.client.SimpleClientProvider;
import org.apache.storm.cassandra.query.CQLResultSetValuesMapper;
Expand All @@ -49,6 +52,8 @@ public class CassandraState implements State {
private final Map conf;
private final Options options;

private CassandraConf cassandraConf;

private Session session;
private SimpleClient client;

Expand All @@ -62,6 +67,7 @@ public static final class Options implements Serializable {
private CQLStatementTupleMapper cqlStatementTupleMapper;
private CQLResultSetValuesMapper cqlResultSetValuesMapper;
private BatchStatement.Type batchingType;
private Map<String, Object> cassandraConfig;

public Options(SimpleClientProvider clientProvider) {
this.clientProvider = clientProvider;
Expand All @@ -82,6 +88,11 @@ public Options withBatching(BatchStatement.Type batchingType) {
return this;
}

public Options withCassandraConfig(Map<String, Object> config) {
cassandraConfig = config;
return this;
}

}

@Override
Expand All @@ -97,6 +108,10 @@ public void commit(Long txid) {
public void prepare() {
Preconditions.checkNotNull(options.cqlStatementTupleMapper, "CassandraState.Options should have cqlStatementTupleMapper");

Map<String, Object> cassandraClientConfig = options.cassandraConfig != null ? options.cassandraConfig : conf;

cassandraConf = new CassandraConf(cassandraClientConfig);

client = options.clientProvider.getClient(conf);
session = client.connect();
}
Expand All @@ -113,17 +128,32 @@ public void cleanup() {

public void updateState(List<TridentTuple> tuples, final TridentCollector collector) {

List<Statement> statements = new ArrayList<>();
for (TridentTuple tuple : tuples) {
statements.addAll(options.cqlStatementTupleMapper.map(conf, session, tuple));
}
Iterable<List<TridentTuple>> partitionedTuples = Iterables.partition(tuples, cassandraConf.getBatchSizeRows());

Iterable<List<Statement>> partitionedStatements = Iterables.transform(partitionedTuples, new Function<List<TridentTuple>, List<Statement>>() {
@Override
public List<Statement> apply(List<TridentTuple> l) {
List<Statement> result = new ArrayList<>();

if (options.batchingType != null) {
BatchStatement batchStatement = new BatchStatement(options.batchingType);
result.add(batchStatement);

for (TridentTuple tuple : l) {
batchStatement.addAll(options.cqlStatementTupleMapper.map(conf, session, tuple));
}
} else {
for (TridentTuple tuple : l) {
result.addAll(options.cqlStatementTupleMapper.map(conf, session, tuple));
}
}

return result;
}
});

try {
if (options.batchingType != null) {
BatchStatement batchStatement = new BatchStatement(options.batchingType);
batchStatement.addAll(statements);
session.execute(batchStatement);
} else {
for (List<Statement> statements : partitionedStatements) {
for (Statement statement : statements) {
session.execute(statement);
}
Expand Down