Search in sources :

Example 11 with TridentTuple

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

the class TestMongoDataSourcesProvider method mockTupleList.

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

Example 12 with TridentTuple

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

the class TestSocketDataSourceProvider method testSocketSink.

@Test
public void testSocketSink() throws IOException {
    ISqlTridentDataSource ds = DataSourcesRegistry.constructTridentDataSource(URI.create("socket://localhost:8888"), null, null, new Properties(), FIELDS);
    Assert.assertNotNull(ds);
    ISqlTridentDataSource.SqlTridentConsumer consumer = ds.getConsumer();
    Assert.assertEquals(SocketState.Factory.class, consumer.getStateFactory().getClass());
    Assert.assertEquals(SocketStateUpdater.class, consumer.getStateUpdater().getClass());
    // makeState() fails on creating State so we just mock SocketState anyway
    SocketState mockState = mock(SocketState.class);
    StateUpdater stateUpdater = consumer.getStateUpdater();
    List<TridentTuple> tupleList = mockTupleList();
    stateUpdater.updateState(mockState, tupleList, null);
    for (TridentTuple t : tupleList) {
        String serializedValue = new String(SERIALIZER.write(t.getValues(), null).array());
        verify(mockState).write(serializedValue + "\n");
    }
}
Also used : ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) Properties(java.util.Properties) SocketState(org.apache.storm.sql.runtime.datasource.socket.trident.SocketState) StateUpdater(org.apache.storm.trident.state.StateUpdater) SocketStateUpdater(org.apache.storm.sql.runtime.datasource.socket.trident.SocketStateUpdater) TridentTuple(org.apache.storm.trident.tuple.TridentTuple) Test(org.junit.Test)

Example 13 with TridentTuple

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

the class TestRedisDataSourcesProvider 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(t1.getValueByField("ID")).thenReturn(2);
    when(t1.getValueByField("val")).thenReturn("3");
    doReturn(Lists.<Object>newArrayList(2, "3")).when(t1).getValues();
    tupleList.add(t0);
    tupleList.add(t1);
    return tupleList;
}
Also used : ArrayList(java.util.ArrayList) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 14 with TridentTuple

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

the class SocketStateUpdater method updateState.

@Override
public void updateState(SocketState state, List<TridentTuple> tuples, TridentCollector collector) {
    try {
        for (TridentTuple tuple : tuples) {
            byte[] array = outputSerializer.write(tuple.getValues(), null).array();
            String data = new String(array);
            state.write(data + "\n");
        }
        state.flush();
    } catch (IOException e) {
        LOG.error("Error while updating state.", e);
        collector.reportError(e);
        throw new RuntimeException("Error while updating state.", e);
    }
}
Also used : IOException(java.io.IOException) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 15 with TridentTuple

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

the class DruidBeamState method update.

public List<E> update(List<TridentTuple> tuples, TridentCollector collector) {
    List<E> events = new ArrayList<>(tuples.size());
    for (TridentTuple tuple : tuples) {
        events.add(druidEventMapper.getEvent(tuple));
    }
    LOG.info("Sending [{}] events", events.size());
    scala.collection.immutable.List<E> scalaList = scala.collection.JavaConversions.collectionAsScalaIterable(events).toList();
    Collection<Future<SendResult>> futureList = scala.collection.JavaConversions.asJavaCollection(beam.sendAll(scalaList));
    List<E> discardedEvents = new ArrayList<>();
    int index = 0;
    for (Future<SendResult> future : futureList) {
        try {
            SendResult result = Await.result(future);
            if (!result.sent()) {
                discardedEvents.add(events.get(index));
            }
        } catch (Exception e) {
            LOG.error("Failed in writing messages to Druid", e);
        }
        index++;
    }
    return discardedEvents;
}
Also used : ArrayList(java.util.ArrayList) SendResult(com.metamx.tranquility.beam.SendResult) Future(com.twitter.util.Future) 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