use of org.apache.storm.tuple.Values in project storm by apache.
the class TridentTopologySource method getTopology.
public StormTopology getTopology(Config config) {
this.spout = new FixedBatchSpout(new Fields("sentence"), 20, new Values("one two"), new Values("two three"), new Values("three four"), new Values("four five"), new Values("five six"));
TridentTopology trident = new TridentTopology();
trident.newStream("wordcount", spout).name("sentence").parallelismHint(1).shuffle().each(new Fields("sentence"), new Split(), new Fields("word")).parallelismHint(1).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(1);
return trident.build();
}
use of org.apache.storm.tuple.Values in project storm by apache.
the class StatefulWordCounter method execute.
@Override
public void execute(Tuple tuple) {
String word = tuple.getString(0);
Long count = wordCounts.get(word, 0L);
count++;
wordCounts.put(word, count);
collector.emit(tuple, new Values(word, count));
collector.ack(tuple);
}
use of org.apache.storm.tuple.Values in project storm by apache.
the class TestStormSql method testAggFnNonSqlReturnType.
@Test
public void testAggFnNonSqlReturnType() throws Exception {
List<String> stmt = new ArrayList<>();
stmt.add("CREATE EXTERNAL TABLE FOO (ID INT PRIMARY KEY, SALARY INT, PCT DOUBLE, NAME VARCHAR) LOCATION 'mockgroup:///foo'");
stmt.add("CREATE FUNCTION TOPN AS 'org.apache.storm.sql.TestUtils$TopN'");
stmt.add("SELECT STREAM ID, SUM(SALARY), TOPN(1, SALARY) FROM FOO WHERE ID >= 0 GROUP BY (ID) HAVING MAX(SALARY) > 0");
StormSql sql = StormSql.construct();
List<Values> values = new ArrayList<>();
ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
sql.execute(stmt, h);
Assert.assertEquals(4, values.size());
Assert.assertEquals(Collections.singletonList(2), values.get(0).get(2));
Assert.assertEquals(Collections.singletonList(5), values.get(1).get(2));
Assert.assertEquals(Collections.singletonList(8), values.get(2).get(2));
Assert.assertEquals(Collections.singletonList(9), values.get(3).get(2));
}
use of org.apache.storm.tuple.Values in project storm by apache.
the class TestStormSql method testGroupbySameAggregateOnDifferentColumns.
@Test
public void testGroupbySameAggregateOnDifferentColumns() throws Exception {
List<String> stmt = new ArrayList<>();
stmt.add("CREATE EXTERNAL TABLE FOO (ID INT PRIMARY KEY, SALARY INT, PCT DOUBLE, NAME VARCHAR) LOCATION 'mockgroup:///foo'");
stmt.add("SELECT STREAM ID, COUNT(*), AVG(SALARY), AVG(PCT) FROM FOO WHERE ID = 1 GROUP BY (ID)");
StormSql sql = StormSql.construct();
List<Values> values = new ArrayList<>();
ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
sql.execute(stmt, h);
Assert.assertEquals(1, values.size());
Assert.assertEquals(1, values.get(0).get(0));
Assert.assertEquals(3L, values.get(0).get(1));
Assert.assertEquals(4, values.get(0).get(2));
Assert.assertEquals(2.5, values.get(0).get(3));
}
use of org.apache.storm.tuple.Values in project storm by apache.
the class TestStormSql method testExternalDataSourceNested.
@Test
public void testExternalDataSourceNested() throws Exception {
List<String> stmt = new ArrayList<>();
stmt.add("CREATE EXTERNAL TABLE FOO (ID INT, MAPFIELD ANY, NESTEDMAPFIELD ANY, ARRAYFIELD ANY) LOCATION 'mocknested:///foo'");
stmt.add("SELECT STREAM ID, MAPFIELD['c'], NESTEDMAPFIELD, ARRAYFIELD " + "FROM FOO " + "WHERE CAST(MAPFIELD['b'] AS INTEGER) = 2 AND CAST(ARRAYFIELD[2] AS INTEGER) = 200");
StormSql sql = StormSql.construct();
List<Values> values = new ArrayList<>();
ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
sql.execute(stmt, h);
System.out.println(values);
Map<String, Integer> map = ImmutableMap.of("b", 2, "c", 4);
Map<String, Map<String, Integer>> nestedMap = ImmutableMap.of("a", map);
Assert.assertEquals(new Values(2, 4, nestedMap, Arrays.asList(100, 200, 300)), values.get(0));
}
Aggregations