Search in sources :

Example 1 with SimpleJdbcLookupMapper

use of org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper in project storm by apache.

the class AbstractUserTopology method execute.

public void execute(String[] args) throws Exception {
    if (args.length != 4 && args.length != 5) {
        System.out.println("Usage: " + this.getClass().getSimpleName() + " <dataSourceClassName> <dataSource.url> " + "<user> <password> [topology name]");
        System.exit(-1);
    }
    Map map = Maps.newHashMap();
    //com.mysql.jdbc.jdbc2.optional.MysqlDataSource
    map.put("dataSourceClassName", args[0]);
    //jdbc:mysql://localhost/test
    map.put("dataSource.url", args[1]);
    //root
    map.put("dataSource.user", args[2]);
    if (args.length == 4) {
        //password
        map.put("dataSource.password", args[3]);
    }
    Config config = new Config();
    config.put(JDBC_CONF, map);
    ConnectionProvider connectionProvider = new HikariCPConnectionProvider(map);
    connectionProvider.prepare();
    int queryTimeoutSecs = 60;
    JdbcClient jdbcClient = new JdbcClient(connectionProvider, queryTimeoutSecs);
    for (String sql : setupSqls) {
        jdbcClient.executeSql(sql);
    }
    this.userSpout = new UserSpout();
    this.jdbcMapper = new SimpleJdbcMapper(TABLE_NAME, connectionProvider);
    connectionProvider.cleanup();
    Fields outputFields = new Fields("user_id", "user_name", "dept_name", "create_date");
    List<Column> queryParamColumns = Lists.newArrayList(new Column("user_id", Types.INTEGER));
    this.jdbcLookupMapper = new SimpleJdbcLookupMapper(outputFields, queryParamColumns);
    this.connectionProvider = new HikariCPConnectionProvider(map);
    if (args.length == 4) {
        try (LocalCluster cluster = new LocalCluster();
            LocalTopology topo = cluster.submitTopology("test", config, getTopology())) {
            Thread.sleep(30000);
        }
        System.exit(0);
    } else {
        StormSubmitter.submitTopology(args[4], config, getTopology());
    }
}
Also used : LocalCluster(org.apache.storm.LocalCluster) UserSpout(org.apache.storm.jdbc.spout.UserSpout) Config(org.apache.storm.Config) JdbcClient(org.apache.storm.jdbc.common.JdbcClient) LocalTopology(org.apache.storm.LocalCluster.LocalTopology) HikariCPConnectionProvider(org.apache.storm.jdbc.common.HikariCPConnectionProvider) ConnectionProvider(org.apache.storm.jdbc.common.ConnectionProvider) HikariCPConnectionProvider(org.apache.storm.jdbc.common.HikariCPConnectionProvider) SimpleJdbcLookupMapper(org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper) Fields(org.apache.storm.tuple.Fields) SimpleJdbcMapper(org.apache.storm.jdbc.mapper.SimpleJdbcMapper) Column(org.apache.storm.jdbc.common.Column) Map(java.util.Map)

Example 2 with SimpleJdbcLookupMapper

use of org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper 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 3 with SimpleJdbcLookupMapper

use of org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper in project storm by apache.

the class JdbcLookupBoltTest method testValidation.

@Test
public void testValidation() {
    ConnectionProvider provider = new HikariCPConnectionProvider(new HashMap<String, Object>());
    JdbcLookupMapper mapper = new SimpleJdbcLookupMapper(new Fields("test"), Lists.newArrayList(new Column("test", 0)));
    String selectQuery = "select * from dual";
    expectIllegaArgs(null, selectQuery, mapper);
    expectIllegaArgs(provider, null, mapper);
    expectIllegaArgs(provider, selectQuery, null);
}
Also used : SimpleJdbcLookupMapper(org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper) Fields(org.apache.storm.tuple.Fields) Column(org.apache.storm.jdbc.common.Column) JdbcLookupMapper(org.apache.storm.jdbc.mapper.JdbcLookupMapper) SimpleJdbcLookupMapper(org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper) HikariCPConnectionProvider(org.apache.storm.jdbc.common.HikariCPConnectionProvider) ConnectionProvider(org.apache.storm.jdbc.common.ConnectionProvider) HikariCPConnectionProvider(org.apache.storm.jdbc.common.HikariCPConnectionProvider) Test(org.junit.Test)

Aggregations

Column (org.apache.storm.jdbc.common.Column)3 SimpleJdbcLookupMapper (org.apache.storm.jdbc.mapper.SimpleJdbcLookupMapper)3 Fields (org.apache.storm.tuple.Fields)3 ConnectionProvider (org.apache.storm.jdbc.common.ConnectionProvider)2 HikariCPConnectionProvider (org.apache.storm.jdbc.common.HikariCPConnectionProvider)2 UserSpout (org.apache.storm.jdbc.spout.UserSpout)2 Map (java.util.Map)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 LocalTopology (org.apache.storm.LocalCluster.LocalTopology)1 JdbcClient (org.apache.storm.jdbc.common.JdbcClient)1 JdbcLookupMapper (org.apache.storm.jdbc.mapper.JdbcLookupMapper)1 SimpleJdbcMapper (org.apache.storm.jdbc.mapper.SimpleJdbcMapper)1 JdbcQuery (org.apache.storm.jdbc.trident.state.JdbcQuery)1 JdbcState (org.apache.storm.jdbc.trident.state.JdbcState)1 JdbcStateFactory (org.apache.storm.jdbc.trident.state.JdbcStateFactory)1 JdbcUpdater (org.apache.storm.jdbc.trident.state.JdbcUpdater)1 Stream (org.apache.storm.trident.Stream)1 TridentState (org.apache.storm.trident.TridentState)1 TridentTopology (org.apache.storm.trident.TridentTopology)1