如何在HBase状态/映射器中使用Storm Trident内置Min

时间:2018-08-28 06:40:10

标签: apache-storm trident

我在开发中使用内置了hbase状态/映射器的Storm三叉戟Min。在开发之前,我尝试过这样的单词计数示例,但它不起作用。我猜HbaseMapper withCounterFields不支持Min?

Mapper级别的Min内置了其他数据库吗?

下面是我的代码:

公共类HbaseWordCountTridentTopolopgyMin {

    private static final Random RANDOM = new Random();

    public static class RandomCount extends BaseFunction {

        public RandomCount() {
        }

        public void execute(TridentTuple tuple, TridentCollector collector) {
            String word = tuple.getString(0);
            collector.emit(new Values(word, RANDOM.nextInt(1000)));

        }
    }

    public static StormTopology buildTopology() {
        String hbaseRoot =  "/opt/cloudera/parcels/CDH/lib/hbase";
        Fields fields = new Fields("word", "count");

        FixedBatchSpout spout = //new FixedBatchSpout(fields, 4,
                new FixedBatchSpout(new Fields("word"), 4,
                new Values("storm"),
                new Values("trident"),
                new Values("needs"),
                new Values("javadoc")
        );
        spout.setCycle(true);

        TridentHBaseMapper tridentHBaseMapper = new SimpleTridentHBaseMapper()
                .withColumnFamily("INFO")
                //.withColumnFamily("info")
                .withColumnFields(new Fields("word"))
                .withCounterFields(new Fields("count"))
                .withRowKeyField("word");

        HBaseValueMapper rowToStormValueMapper = new WordCountValueMapper();

        HBaseProjectionCriteria projectionCriteria = new HBaseProjectionCriteria();
        projectionCriteria.addColumn(new HBaseProjectionCriteria.ColumnMetaData("INFO", "count"));

        HBaseState.Options options = new HBaseState.Options()
                //.withConfigKey(hbaseRoot)
                .withConfigKey("hbase.conf")
                .withDurability(Durability.SYNC_WAL)
                .withMapper(tridentHBaseMapper)
                .withProjectionCriteria(projectionCriteria)
                .withRowToStormValueMapper(rowToStormValueMapper)
                //.withTableName("WordCount");
                .withTableName("test_HbaseWordCountTridentTopolopgy");

        StateFactory factory = new HBaseStateFactory(options);

        TridentTopology topology = new TridentTopology();
        Stream stream =
                topology.newStream("spout", spout)
                        .each(new Fields("word"), new RandomCount(), new Fields("tocheck"))
                        .groupBy(new Fields("word"))
                        .aggregate(new Min("tocheck"), new Fields("count"))
                ;

        stream.partitionPersist(factory, fields,  new HBaseUpdater(), new Fields());

        TridentState state = topology.newStaticState(factory);
        stream = stream.stateQuery(state, new Fields("word"), new HBaseQuery(), new Fields("columnName","columnValue"));
        stream.each(new Fields("word","columnValue"), new PrintFunction(), new Fields());
        return topology.build();
    }

    public static void main(String[] args) throws Exception{
        Map<String, Object> hbConf = new HashMap<String, Object>();
        hbConf.put("hbase.rootdir", "/opt/cloudera/parcels/CDH/lib/hbase");
        hbConf.put("hbase.zookeeper.quorum", "beta-hbase02:2181,beta-hbase03:2181,beta-hbase04:2181");
        Config conf = new Config();
        //conf.put("hbase.conf", new HashMap());
        conf.put("hbase.conf", hbConf);
        conf.setMaxSpoutPending(5);
        boolean checkLocal = Arrays.stream(args).map(arg -> arg.equals("local")).reduce((arg1,arg2) -> arg1 | arg2 ).orElse(false);
        if (checkLocal) {
            LocalCluster cluster = new LocalCluster();
            cluster.submitTopology("HbaseWordCountTridentTopolopgy", conf, buildTopology());
            Thread.sleep(60 * 1000);
        }else {
            conf.setNumWorkers(3);
            StormSubmitter.submitTopology("hbase-word-count-trident", conf, buildTopology());
        }
    }

}

0 个答案:

没有答案