Search in sources :

Example 66 with Tuple

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

the class JoinBolt method doProjection.

// Performs projection on the tuples based on 'projectionFields'
protected ArrayList<Object> doProjection(ArrayList<Tuple> tuples, FieldSelector[] projectionFields) {
    ArrayList<Object> result = new ArrayList<>(projectionFields.length);
    // Todo: optimize this computation... perhaps inner loop can be outside to avoid rescanning tuples
    for (int i = 0; i < projectionFields.length; i++) {
        boolean missingField = true;
        for (Tuple tuple : tuples) {
            Object field = lookupField(projectionFields[i], tuple);
            if (field != null) {
                result.add(field);
                missingField = false;
                break;
            }
        }
        if (missingField) {
            // add a null for missing fields (usually in case of outer joins)
            result.add(null);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Tuple(org.apache.storm.tuple.Tuple)

Example 67 with Tuple

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

the class TestJoinBolt method testThreeStreamInnerJoin.

@Test
public void testThreeStreamInnerJoin() 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, storesStream, cityStream);
    JoinBolt bolt = new JoinBolt(JoinBolt.Selector.STREAM, "users", userFields[2]).join("stores", "city", "users").join("cities", "cityName", "stores").select("name,storeName,city,country");
    MockCollector collector = new MockCollector();
    bolt.prepare(null, null, collector);
    bolt.execute(window);
    printResults(collector);
    Assert.assertEquals(6, collector.actualResults.size());
}
Also used : TupleWindow(org.apache.storm.windowing.TupleWindow) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 68 with Tuple

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

the class TestJoinBolt method makeStream.

private static ArrayList<Tuple> makeStream(String streamName, String[] fieldNames, Object[][] data) {
    ArrayList<Tuple> result = new ArrayList<>();
    MockContext mockContext = new MockContext(fieldNames);
    for (Object[] record : data) {
        TupleImpl rec = new TupleImpl(mockContext, Arrays.asList(record), 0, streamName);
        result.add(rec);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) TupleImpl(org.apache.storm.tuple.TupleImpl) Tuple(org.apache.storm.tuple.Tuple)

Example 69 with Tuple

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

the class TestJoinBolt method makeNestedEventsStream.

private static ArrayList<Tuple> makeNestedEventsStream(String streamName, String[] fieldNames, Object[][] records) {
    MockContext mockContext = new MockContext(new String[] { "outer" });
    ArrayList<Tuple> result = new ArrayList<>(records.length);
    // convert each record into a HashMap using fieldNames as keys
    for (Object[] record : records) {
        HashMap<String, Object> recordMap = new HashMap<>(fieldNames.length);
        for (int i = 0; i < fieldNames.length; i++) {
            recordMap.put(fieldNames[i], record[i]);
        }
        ArrayList<Object> tupleValues = new ArrayList<>(1);
        tupleValues.add(recordMap);
        TupleImpl tuple = new TupleImpl(mockContext, tupleValues, 0, streamName);
        result.add(tuple);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TupleImpl(org.apache.storm.tuple.TupleImpl) Tuple(org.apache.storm.tuple.Tuple)

Example 70 with Tuple

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

the class TestJoinBolt method testProjection_FieldsWithStreamName.

@Test
public void testProjection_FieldsWithStreamName() throws Exception {
    ArrayList<Tuple> userStream = makeStream("users", userFields, users);
    ArrayList<Tuple> storeStream = makeStream("stores", storeFields, stores);
    TupleWindow window = makeTupleWindow(storeStream, userStream);
    // join users and stores on city name
    JoinBolt bolt = new JoinBolt(JoinBolt.Selector.STREAM, "users", userFields[2]).join("stores", "city", "users").select("userId,name,storeName,users:city,stores:city");
    MockCollector collector = new MockCollector();
    bolt.prepare(null, null, collector);
    bolt.execute(window);
    printResults(collector);
    Assert.assertEquals(storeStream.size() + 1, collector.actualResults.size());
    // ensure 5 fields per tuple and no null fields
    for (List<Object> tuple : collector.actualResults) {
        Assert.assertEquals(5, tuple.size());
        for (Object o : tuple) {
            Assert.assertNotNull(o);
        }
    }
}
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