Search in sources :

Example 1 with TridentTopology

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

the class UserPersistanceTridentTopology method getTopology.

@Override
public StormTopology getTopology() {
    TridentTopology topology = new TridentTopology();
    JdbcState.Options options = new JdbcState.Options().withConnectionProvider(connectionProvider).withMapper(this.jdbcMapper).withJdbcLookupMapper(new SimpleJdbcLookupMapper(new Fields("dept_name"), Lists.newArrayList(new Column("user_id", Types.INTEGER)))).withTableName(TABLE_NAME).withSelectQuery(SELECT_QUERY);
    JdbcStateFactory jdbcStateFactory = new JdbcStateFactory(options);
    Stream stream = topology.newStream("userSpout", new UserSpout());
    TridentState state = topology.newStaticState(jdbcStateFactory);
    stream = stream.stateQuery(state, new Fields("user_id", "user_name", "create_date"), new JdbcQuery(), new Fields("dept_name"));
    stream.partitionPersist(jdbcStateFactory, new Fields("user_id", "user_name", "dept_name", "create_date"), new JdbcUpdater(), new Fields());
    return topology.build();
}
Also used : JdbcState(org.apache.storm.jdbc.trident.state.JdbcState) JdbcQuery(org.apache.storm.jdbc.trident.state.JdbcQuery) SimpleJdbcLookupMapper(org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper) UserSpout(org.apache.storm.jdbc.spout.UserSpout) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) Column(org.apache.storm.jdbc.common.Column) TridentState(org.apache.storm.trident.TridentState) JdbcStateFactory(org.apache.storm.jdbc.trident.state.JdbcStateFactory) Stream(org.apache.storm.trident.Stream) JdbcUpdater(org.apache.storm.jdbc.trident.state.JdbcUpdater)

Example 2 with TridentTopology

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

the class TridentKafkaConsumerTopology method newTopology.

/**
     * @param drpc The DRPC stream to be used in querying the word counts. Can be null in distributed mode
     * @return a trident topology that consumes sentences from the kafka topic specified using a
     * {@link TransactionalTridentKafkaSpout} computes the word count and stores it in a {@link MemoryMapState}.
     */
public static StormTopology newTopology(LocalDRPC drpc, ITridentDataSource tridentSpout) {
    final TridentTopology tridentTopology = new TridentTopology();
    addDRPCStream(tridentTopology, addTridentState(tridentTopology, tridentSpout), drpc);
    return tridentTopology.build();
}
Also used : TridentTopology(org.apache.storm.trident.TridentTopology)

Example 3 with TridentTopology

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

the class TridentWordCount method buildTopology.

public static StormTopology buildTopology(LocalDRPC drpc) {
    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", drpc).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)

Example 4 with TridentTopology

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

the class QueryPlanner method compile.

public AbstractTridentProcessor compile(Map<String, ISqlTridentDataSource> sources, String query) throws Exception {
    TridentRel relNode = getPlan(query);
    TridentPlanCreator tridentPlanCreator = new TridentPlanCreator(sources, new RexBuilder(typeFactory));
    relNode.tridentPlan(tridentPlanCreator);
    final TridentTopology topology = tridentPlanCreator.getTopology();
    final IAggregatableStream lastStream = tridentPlanCreator.pop();
    final DataContext dc = tridentPlanCreator.getDataContext();
    final List<CompilingClassLoader> cls = tridentPlanCreator.getClassLoaders();
    return new AbstractTridentProcessor() {

        @Override
        public TridentTopology build() {
            return topology;
        }

        @Override
        public Stream outputStream() {
            return lastStream.toStream();
        }

        @Override
        public DataContext getDataContext() {
            return dc;
        }

        @Override
        public List<CompilingClassLoader> getClassLoaders() {
            return cls;
        }
    };
}
Also used : AbstractTridentProcessor(org.apache.storm.sql.AbstractTridentProcessor) IAggregatableStream(org.apache.storm.trident.fluent.IAggregatableStream) DataContext(org.apache.calcite.DataContext) CompilingClassLoader(org.apache.storm.sql.javac.CompilingClassLoader) TridentTopology(org.apache.storm.trident.TridentTopology) RexBuilder(org.apache.calcite.rex.RexBuilder) TridentRel(org.apache.storm.sql.planner.trident.rel.TridentRel)

Example 5 with TridentTopology

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

the class StormSqlImpl method submit.

@Override
public void submit(String name, Iterable<String> statements, Map<String, ?> stormConf, SubmitOptions opts, StormSubmitter.ProgressListener progressListener, String asUser) throws Exception {
    Map<String, ISqlTridentDataSource> dataSources = new HashMap<>();
    for (String sql : statements) {
        StormParser parser = new StormParser(sql);
        SqlNode node = parser.impl().parseSqlStmtEof();
        if (node instanceof SqlCreateTable) {
            handleCreateTableForTrident((SqlCreateTable) node, dataSources);
        } else if (node instanceof SqlCreateFunction) {
            handleCreateFunction((SqlCreateFunction) node);
        } else {
            QueryPlanner planner = new QueryPlanner(schema);
            AbstractTridentProcessor processor = planner.compile(dataSources, sql);
            TridentTopology topo = processor.build();
            Path jarPath = null;
            try {
                // QueryPlanner on Trident mode configures the topology with compiled classes,
                // so we need to add new classes into topology jar
                // Topology will be serialized and sent to Nimbus, and deserialized and executed in workers.
                jarPath = Files.createTempFile("storm-sql", ".jar");
                System.setProperty("storm.jar", jarPath.toString());
                packageTopology(jarPath, processor);
                StormSubmitter.submitTopologyAs(name, stormConf, topo.build(), opts, progressListener, asUser);
            } finally {
                if (jarPath != null) {
                    Files.delete(jarPath);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) TridentTopology(org.apache.storm.trident.TridentTopology) SqlCreateFunction(org.apache.storm.sql.parser.SqlCreateFunction) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) StormParser(org.apache.storm.sql.parser.StormParser) SqlCreateTable(org.apache.storm.sql.parser.SqlCreateTable) QueryPlanner(org.apache.storm.sql.planner.trident.QueryPlanner) SqlNode(org.apache.calcite.sql.SqlNode)

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