Search in sources :

Example 21 with Tuple

use of org.apache.storm.tuple.Tuple in project storm by apache.

the class StatefulBoltExecutor method handleCheckpoint.

@Override
protected void handleCheckpoint(Tuple checkpointTuple, Action action, long txid) {
    LOG.debug("handleCheckPoint with tuple {}, action {}, txid {}", checkpointTuple, action, txid);
    if (action == PREPARE) {
        if (boltInitialized) {
            bolt.prePrepare(txid);
            state.prepareCommit(txid);
            preparedTuples.addAll(collector.ackedTuples());
        } else {
            /*
                 * May be the task restarted in the middle and the state needs be initialized.
                 * Fail fast and trigger recovery.
                  */
            LOG.debug("Failing checkpointTuple, PREPARE received when bolt state is not initialized.");
            collector.fail(checkpointTuple);
            return;
        }
    } else if (action == COMMIT) {
        bolt.preCommit(txid);
        state.commit(txid);
        ack(preparedTuples);
    } else if (action == ROLLBACK) {
        bolt.preRollback();
        state.rollback();
        fail(preparedTuples);
        fail(collector.ackedTuples());
    } else if (action == INITSTATE) {
        if (!boltInitialized) {
            bolt.initState((T) state);
            boltInitialized = true;
            LOG.debug("{} pending tuples to process", pendingTuples.size());
            for (Tuple tuple : pendingTuples) {
                doExecute(tuple);
            }
            pendingTuples.clear();
        } else {
            LOG.debug("Bolt state is already initialized, ignoring tuple {}, action {}, txid {}", checkpointTuple, action, txid);
        }
    }
    collector.emit(CheckpointSpout.CHECKPOINT_STREAM_ID, checkpointTuple, new Values(txid, action));
    collector.delegate.ack(checkpointTuple);
}
Also used : COMMIT(org.apache.storm.spout.CheckPointState.Action.COMMIT) Values(org.apache.storm.tuple.Values) Tuple(org.apache.storm.tuple.Tuple)

Example 22 with Tuple

use of org.apache.storm.tuple.Tuple in project storm by apache.

the class TestJoinBolt method testLeftJoin.

@Test
public void testLeftJoin() throws Exception {
    ArrayList<Tuple> userStream = makeStream("users", userFields, users);
    ArrayList<Tuple> orderStream = makeStream("orders", orderFields, orders);
    TupleWindow window = makeTupleWindow(orderStream, userStream);
    JoinBolt bolt = new JoinBolt(JoinBolt.Selector.STREAM, "users", userFields[0]).leftJoin("orders", "userId", "users").select("userId,name,price");
    MockCollector collector = new MockCollector();
    bolt.prepare(null, null, collector);
    bolt.execute(window);
    printResults(collector);
    Assert.assertEquals(12, collector.actualResults.size());
}
Also used : TupleWindow(org.apache.storm.windowing.TupleWindow) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 23 with Tuple

use of org.apache.storm.tuple.Tuple in project storm by apache.

the class TestJoinBolt method testTrivial.

@Test
public void testTrivial() throws Exception {
    ArrayList<Tuple> orderStream = makeStream("orders", orderFields, orders);
    TupleWindow window = makeTupleWindow(orderStream);
    JoinBolt bolt = new JoinBolt(JoinBolt.Selector.STREAM, "orders", orderFields[0]).select("orderId,userId,itemId,price");
    MockCollector collector = new MockCollector();
    bolt.prepare(null, null, collector);
    bolt.execute(window);
    printResults(collector);
    Assert.assertEquals(orderStream.size(), collector.actualResults.size());
}
Also used : TupleWindow(org.apache.storm.windowing.TupleWindow) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 24 with Tuple

use of org.apache.storm.tuple.Tuple in project storm by apache.

the class TestJoinBolt method testInnerJoin.

@Test
public void testInnerJoin() throws Exception {
    ArrayList<Tuple> userStream = makeStream("users", userFields, users);
    ArrayList<Tuple> orderStream = makeStream("orders", orderFields, orders);
    TupleWindow window = makeTupleWindow(orderStream, userStream);
    JoinBolt bolt = new JoinBolt(JoinBolt.Selector.STREAM, "users", userFields[0]).join("orders", "userId", "users").select("userId,name,price");
    MockCollector collector = new MockCollector();
    bolt.prepare(null, null, collector);
    bolt.execute(window);
    printResults(collector);
    Assert.assertEquals(orders.length, collector.actualResults.size());
}
Also used : TupleWindow(org.apache.storm.windowing.TupleWindow) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 25 with Tuple

use of org.apache.storm.tuple.Tuple in project storm by apache.

the class TestJoinBolt method testThreeStreamLeftJoin_1.

@Test
public void testThreeStreamLeftJoin_1() throws Exception {
    ArrayList<Tuple> userStream = makeStream("users", userFields, users);
    ArrayList<Tuple> storesStream = makeStream("stores", storeFields, stores);
    ArrayList<Tuple> cityStream = makeStream("cities", cityFields, cities);
    TupleWindow window = makeTupleWindow(userStream, cityStream, storesStream);
    JoinBolt bolt = new JoinBolt(JoinBolt.Selector.STREAM, "users", userFields[2]).leftJoin("stores", "city", "users").leftJoin("cities", "cityName", "users").select("name,storeName,city,country");
    MockCollector collector = new MockCollector();
    bolt.prepare(null, null, collector);
    bolt.execute(window);
    printResults(collector);
    Assert.assertEquals(users.length, collector.actualResults.size());
}
Also used : TupleWindow(org.apache.storm.windowing.TupleWindow) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Aggregations

Tuple (org.apache.storm.tuple.Tuple)85 Test (org.junit.Test)30 Fields (org.apache.storm.tuple.Fields)13 OutputCollector (org.apache.storm.task.OutputCollector)11 Values (org.apache.storm.tuple.Values)11 ArrayList (java.util.ArrayList)10 HiveOptions (org.apache.storm.hive.common.HiveOptions)10 TupleWindow (org.apache.storm.windowing.TupleWindow)9 HashMap (java.util.HashMap)7 Test (org.testng.annotations.Test)7 GlobalStreamId (org.apache.storm.generated.GlobalStreamId)6 DelimitedRecordHiveMapper (org.apache.storm.hive.bolt.mapper.DelimitedRecordHiveMapper)6 HashSet (java.util.HashSet)5 JsonRecordHiveMapper (org.apache.storm.hive.bolt.mapper.JsonRecordHiveMapper)5 TopologyContext (org.apache.storm.task.TopologyContext)5 TupleImpl (org.apache.storm.tuple.TupleImpl)5 BasicOutputCollector (org.apache.storm.topology.BasicOutputCollector)4 Map (java.util.Map)3 Callback (org.apache.kafka.clients.producer.Callback)3 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)3