Search in sources :

Example 1 with MongoUpdateMapper

use of org.apache.storm.mongodb.common.mapper.MongoUpdateMapper in project storm by apache.

the class UpdateWordCount method main.

public static void main(String[] args) throws Exception {
    Config config = new Config();
    String url = TEST_MONGODB_URL;
    String collectionName = TEST_MONGODB_COLLECTION_NAME;
    if (args.length >= 2) {
        url = args[0];
        collectionName = args[1];
    }
    WordSpout spout = new WordSpout();
    WordCounter bolt = new WordCounter();
    MongoUpdateMapper mapper = new SimpleMongoUpdateMapper().withFields("word", "count");
    QueryFilterCreator filterCreator = new SimpleQueryFilterCreator().withField("word");
    MongoUpdateBolt updateBolt = new MongoUpdateBolt(url, collectionName, filterCreator, mapper);
    //if a new document should be inserted if there are no matches to the query filter
    //updateBolt.withUpsert(true);
    //whether find all documents according to the query filter
    //updateBolt.withMany(true);
    // wordSpout ==> countBolt ==> MongoUpdateBolt
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout(WORD_SPOUT, spout, 1);
    builder.setBolt(COUNT_BOLT, bolt, 1).shuffleGrouping(WORD_SPOUT);
    builder.setBolt(UPDATE_BOLT, updateBolt, 1).fieldsGrouping(COUNT_BOLT, new Fields("word"));
    if (args.length == 2) {
        try (LocalCluster cluster = new LocalCluster();
            LocalTopology topo = cluster.submitTopology("test", config, builder.createTopology())) {
            Thread.sleep(30000);
        }
        System.exit(0);
    } else if (args.length == 3) {
        StormSubmitter.submitTopology(args[2], config, builder.createTopology());
    } else {
        System.out.println("Usage: UpdateWordCount <mongodb url> <mongodb collection> [topology name]");
    }
}
Also used : SimpleMongoUpdateMapper(org.apache.storm.mongodb.common.mapper.SimpleMongoUpdateMapper) LocalCluster(org.apache.storm.LocalCluster) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) Config(org.apache.storm.Config) SimpleQueryFilterCreator(org.apache.storm.mongodb.common.SimpleQueryFilterCreator) SimpleQueryFilterCreator(org.apache.storm.mongodb.common.SimpleQueryFilterCreator) QueryFilterCreator(org.apache.storm.mongodb.common.QueryFilterCreator) MongoUpdateBolt(org.apache.storm.mongodb.bolt.MongoUpdateBolt) LocalTopology(org.apache.storm.LocalCluster.LocalTopology) Fields(org.apache.storm.tuple.Fields) MongoUpdateMapper(org.apache.storm.mongodb.common.mapper.MongoUpdateMapper) SimpleMongoUpdateMapper(org.apache.storm.mongodb.common.mapper.SimpleMongoUpdateMapper)

Aggregations

Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 LocalTopology (org.apache.storm.LocalCluster.LocalTopology)1 MongoUpdateBolt (org.apache.storm.mongodb.bolt.MongoUpdateBolt)1 QueryFilterCreator (org.apache.storm.mongodb.common.QueryFilterCreator)1 SimpleQueryFilterCreator (org.apache.storm.mongodb.common.SimpleQueryFilterCreator)1 MongoUpdateMapper (org.apache.storm.mongodb.common.mapper.MongoUpdateMapper)1 SimpleMongoUpdateMapper (org.apache.storm.mongodb.common.mapper.SimpleMongoUpdateMapper)1 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)1 Fields (org.apache.storm.tuple.Fields)1