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;
}
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());
}
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;
}
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;
}
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);
}
}
}
Aggregations