Search in sources :

Example 26 with TridentTuple

use of org.apache.storm.trident.tuple.TridentTuple in project storm by apache.

the class EsState method updateState.

/**
     * Store current state to ElasticSearch.
     *
     * @param tuples list of tuples for storing to ES.
     *               Each tuple should have relevant fields (source, index, type, id) for EsState's tupleMapper to extract ES document.
     */
public void updateState(List<TridentTuple> tuples) {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for (TridentTuple tuple : tuples) {
        String source = tupleMapper.getSource(tuple);
        String index = tupleMapper.getIndex(tuple);
        String type = tupleMapper.getType(tuple);
        String id = tupleMapper.getId(tuple);
        bulkRequest.add(client.prepareIndex(index, type, id).setSource(source));
    }
    BulkResponse bulkResponse = bulkRequest.execute().actionGet();
    if (bulkResponse.hasFailures()) {
        LOG.warn("failed processing bulk index requests " + bulkResponse.buildFailureMessage());
        throw new FailedException();
    }
}
Also used : FailedException(org.apache.storm.topology.FailedException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 27 with TridentTuple

use of org.apache.storm.trident.tuple.TridentTuple in project storm by apache.

the class CassandraState method updateState.

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));
    }
    try {
        if (options.batchingType != null) {
            BatchStatement batchStatement = new BatchStatement(options.batchingType);
            batchStatement.addAll(statements);
            session.execute(batchStatement);
        } else {
            for (Statement statement : statements) {
                session.execute(statement);
            }
        }
    } catch (Exception e) {
        LOG.warn("Batch write operation is failed.");
        collector.reportError(e);
        throw new FailedException(e);
    }
}
Also used : Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) FailedException(org.apache.storm.topology.FailedException) BatchStatement(com.datastax.driver.core.BatchStatement) ArrayList(java.util.ArrayList) FailedException(org.apache.storm.topology.FailedException) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 28 with TridentTuple

use of org.apache.storm.trident.tuple.TridentTuple in project storm by apache.

the class CassandraState method batchRetrieve.

public List<List<Values>> batchRetrieve(List<TridentTuple> tridentTuples) {
    Preconditions.checkNotNull(options.cqlResultSetValuesMapper, "CassandraState.Options should have cqlResultSetValuesMapper");
    List<List<Values>> batchRetrieveResult = new ArrayList<>();
    try {
        for (TridentTuple tridentTuple : tridentTuples) {
            List<Statement> statements = options.cqlStatementTupleMapper.map(conf, session, tridentTuple);
            for (Statement statement : statements) {
                List<List<Values>> values = options.cqlResultSetValuesMapper.map(session, statement, tridentTuple);
                batchRetrieveResult.addAll(values);
            }
        }
    } catch (Exception e) {
        LOG.warn("Batch retrieve operation is failed.");
        throw new FailedException(e);
    }
    return batchRetrieveResult;
}
Also used : Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) FailedException(org.apache.storm.topology.FailedException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FailedException(org.apache.storm.topology.FailedException) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 29 with TridentTuple

use of org.apache.storm.trident.tuple.TridentTuple in project storm by apache.

the class TestSocketDataSourceProvider method mockTupleList.

private static List<TridentTuple> mockTupleList() {
    List<TridentTuple> tupleList = new ArrayList<>();
    TridentTuple t0 = mock(TridentTuple.class);
    TridentTuple t1 = mock(TridentTuple.class);
    when(t0.getValueByField("ID")).thenReturn(1);
    when(t0.getValueByField("val")).thenReturn("2");
    doReturn(Lists.<Object>newArrayList(1, "2")).when(t0).getValues();
    when(t0.size()).thenReturn(2);
    when(t1.getValueByField("ID")).thenReturn(2);
    when(t1.getValueByField("val")).thenReturn("3");
    doReturn(Lists.<Object>newArrayList(2, "3")).when(t1).getValues();
    when(t1.size()).thenReturn(2);
    tupleList.add(t0);
    tupleList.add(t1);
    return tupleList;
}
Also used : ArrayList(java.util.ArrayList) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 30 with TridentTuple

use of org.apache.storm.trident.tuple.TridentTuple in project storm by apache.

the class HBaseState method updateState.

public void updateState(List<TridentTuple> tuples, TridentCollector collector) {
    List<Mutation> mutations = Lists.newArrayList();
    for (TridentTuple tuple : tuples) {
        byte[] rowKey = options.mapper.rowKey(tuple);
        ColumnList cols = options.mapper.columns(tuple);
        mutations.addAll(hBaseClient.constructMutationReq(rowKey, cols, options.durability));
    }
    try {
        hBaseClient.batchMutate(mutations);
    } catch (Exception e) {
        collector.reportError(e);
        throw new FailedException(e);
    }
}
Also used : FailedException(org.apache.storm.topology.FailedException) ColumnList(org.apache.storm.hbase.common.ColumnList) FailedException(org.apache.storm.topology.FailedException) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Aggregations

TridentTuple (org.apache.storm.trident.tuple.TridentTuple)46 ArrayList (java.util.ArrayList)18 FailedException (org.apache.storm.topology.FailedException)11 List (java.util.List)10 Values (org.apache.storm.tuple.Values)8 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 TridentTopology (org.apache.storm.trident.TridentTopology)5 Consumer (org.apache.storm.trident.operation.Consumer)5 StateUpdater (org.apache.storm.trident.state.StateUpdater)5 Stream (org.apache.storm.trident.Stream)4 Fields (org.apache.storm.tuple.Fields)4 Map (java.util.Map)3 FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)3 BatchStatement (com.datastax.driver.core.BatchStatement)2 Statement (com.datastax.driver.core.Statement)2 IOException (java.io.IOException)2 Future (java.util.concurrent.Future)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2