Search in sources :

Example 6 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class TestPlanCompiler method testCaseStatement.

@Test
public void testCaseStatement() throws Exception {
    int EXPECTED_VALUE_SIZE = 5;
    String sql = "SELECT CASE WHEN NAME IN ('a', 'abc', 'abcde') THEN UPPER('a') " + "WHEN UPPER(NAME) = 'AB' THEN 'b' ELSE {fn CONCAT(NAME, '#')} END FROM FOO";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverDummyTable(sql);
    final Map<String, ISqlTridentDataSource> data = new HashMap<>();
    data.put("FOO", new TestUtils.MockSqlTridentDataSource());
    QueryPlanner planner = new QueryPlanner(state.schema());
    AbstractTridentProcessor proc = planner.compile(data, sql);
    final TridentTopology topo = proc.build();
    Fields f = proc.outputStream().getOutputFields();
    proc.outputStream().partitionPersist(new TestUtils.MockStateFactory(), f, new TestUtils.MockStateUpdater(), new Fields());
    runTridentTopology(EXPECTED_VALUE_SIZE, proc, topo);
    Assert.assertArrayEquals(new Values[] { new Values("A"), new Values("b"), new Values("A"), new Values("abcd#"), new Values("A") }, getCollectedValues().toArray());
}
Also used : HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values) MockState.getCollectedValues(org.apache.storm.sql.TestUtils.MockState.getCollectedValues) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) QueryPlanner(org.apache.storm.sql.planner.trident.QueryPlanner) TestUtils(org.apache.storm.sql.TestUtils) AbstractTridentProcessor(org.apache.storm.sql.AbstractTridentProcessor) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) Test(org.junit.Test)

Example 7 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class TestPlanCompiler method testCompile.

@Test
public void testCompile() throws Exception {
    final int EXPECTED_VALUE_SIZE = 2;
    String sql = "SELECT ID FROM FOO WHERE ID > 2";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverDummyTable(sql);
    final Map<String, ISqlTridentDataSource> data = new HashMap<>();
    data.put("FOO", new TestUtils.MockSqlTridentDataSource());
    QueryPlanner planner = new QueryPlanner(state.schema());
    AbstractTridentProcessor proc = planner.compile(data, sql);
    final TridentTopology topo = proc.build();
    Fields f = proc.outputStream().getOutputFields();
    proc.outputStream().partitionPersist(new TestUtils.MockStateFactory(), f, new TestUtils.MockStateUpdater(), new Fields());
    runTridentTopology(EXPECTED_VALUE_SIZE, proc, topo);
    Assert.assertArrayEquals(new Values[] { new Values(3), new Values(4) }, getCollectedValues().toArray());
}
Also used : HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values) MockState.getCollectedValues(org.apache.storm.sql.TestUtils.MockState.getCollectedValues) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) QueryPlanner(org.apache.storm.sql.planner.trident.QueryPlanner) TestUtils(org.apache.storm.sql.TestUtils) AbstractTridentProcessor(org.apache.storm.sql.AbstractTridentProcessor) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) Test(org.junit.Test)

Example 8 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class TridentMinMaxOfVehiclesTopology method buildVehiclesTopology.

/**
 * Creates a topology which demonstrates min/max operations on tuples of stream which contain vehicle and driver fields
 * with values {@link TridentMinMaxOfVehiclesTopology.Vehicle} and {@link TridentMinMaxOfVehiclesTopology.Driver} respectively.
 */
public static StormTopology buildVehiclesTopology() {
    Fields driverField = new Fields(Driver.FIELD_NAME);
    Fields vehicleField = new Fields(Vehicle.FIELD_NAME);
    Fields allFields = new Fields(Vehicle.FIELD_NAME, Driver.FIELD_NAME);
    FixedBatchSpout spout = new FixedBatchSpout(allFields, 10, Vehicle.generateVehicles(20));
    spout.setCycle(true);
    TridentTopology topology = new TridentTopology();
    Stream vehiclesStream = topology.newStream("spout1", spout).each(allFields, new Debug("##### vehicles"));
    Stream slowVehiclesStream = vehiclesStream.min(new SpeedComparator()).each(vehicleField, new Debug("#### slowest vehicle"));
    Stream slowDriversStream = slowVehiclesStream.project(driverField).each(driverField, new Debug("##### slowest driver"));
    vehiclesStream.max(new SpeedComparator()).each(vehicleField, new Debug("#### fastest vehicle")).project(driverField).each(driverField, new Debug("##### fastest driver"));
    vehiclesStream.minBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### least efficient vehicle"));
    vehiclesStream.maxBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### most efficient vehicle"));
    return topology.build();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) Stream(org.apache.storm.trident.Stream) Debug(org.apache.storm.trident.operation.builtin.Debug)

Example 9 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class TridentWindowingInmemoryStoreTopology method buildTopology.

public static StormTopology buildTopology(WindowsStoreFactory windowStore, WindowConfig windowConfig) throws Exception {
    FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"));
    spout.setCycle(true);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).window(windowConfig, windowStore, new Fields("word"), new CountAsAggregator(), new Fields("count")).peek(new Consumer() {

        @Override
        public void accept(TridentTuple input) {
            LOG.info("Received tuple: [{}]", input);
        }
    });
    return topology.build();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) Consumer(org.apache.storm.trident.operation.Consumer) TridentTopology(org.apache.storm.trident.TridentTopology) CountAsAggregator(org.apache.storm.trident.testing.CountAsAggregator) Values(org.apache.storm.tuple.Values) Stream(org.apache.storm.trident.Stream) Split(org.apache.storm.trident.testing.Split) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 10 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class TridentWordCount method buildTopology.

public static StormTopology buildTopology() {
    FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"));
    spout.setCycle(true);
    TridentTopology topology = new TridentTopology();
    TridentState wordCounts = topology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    topology.newDRPCStream("words").each(new Fields("args"), new Split(), new Fields("word")).groupBy(new Fields("word")).stateQuery(wordCounts, new Fields("word"), new MapGet(), new Fields("count")).each(new Fields("count"), new FilterNull()).project(new Fields("word", "count"));
    return topology.build();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) FilterNull(org.apache.storm.trident.operation.builtin.FilterNull) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) TridentState(org.apache.storm.trident.TridentState) Values(org.apache.storm.tuple.Values) MapGet(org.apache.storm.trident.operation.builtin.MapGet) Count(org.apache.storm.trident.operation.builtin.Count)

Aggregations

TridentTopology (org.apache.storm.trident.TridentTopology)44 Fields (org.apache.storm.tuple.Fields)38 Stream (org.apache.storm.trident.Stream)25 Values (org.apache.storm.tuple.Values)25 TridentState (org.apache.storm.trident.TridentState)22 FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)19 StateFactory (org.apache.storm.trident.state.StateFactory)12 MapGet (org.apache.storm.trident.operation.builtin.MapGet)10 Sum (org.apache.storm.trident.operation.builtin.Sum)10 HashMap (java.util.HashMap)9 Count (org.apache.storm.trident.operation.builtin.Count)8 AbstractTridentProcessor (org.apache.storm.sql.AbstractTridentProcessor)7 QueryPlanner (org.apache.storm.sql.planner.trident.QueryPlanner)7 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)7 TestUtils (org.apache.storm.sql.TestUtils)6 MockState.getCollectedValues (org.apache.storm.sql.TestUtils.MockState.getCollectedValues)6 Consumer (org.apache.storm.trident.operation.Consumer)6 TridentTuple (org.apache.storm.trident.tuple.TridentTuple)6 Test (org.junit.Test)6 FilterNull (org.apache.storm.trident.operation.builtin.FilterNull)5