Search in sources :

Example 1 with DefaultTopicSelector

use of org.apache.storm.kafka.bolt.selector.DefaultTopicSelector in project storm by apache.

the class KafkaBolt method prepare.

@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    //for backward compatibility.
    if (mapper == null) {
        this.mapper = new FieldNameBasedTupleToKafkaMapper<K, V>();
    }
    //for backward compatibility.
    if (topicSelector == null) {
        if (stormConf.containsKey(TOPIC)) {
            this.topicSelector = new DefaultTopicSelector((String) stormConf.get(TOPIC));
        } else {
            throw new IllegalArgumentException("topic should be specified in bolt's configuration");
        }
    }
    producer = new KafkaProducer<>(boltSpecfiedProperties);
    this.collector = collector;
}
Also used : DefaultTopicSelector(org.apache.storm.kafka.bolt.selector.DefaultTopicSelector)

Example 2 with DefaultTopicSelector

use of org.apache.storm.kafka.bolt.selector.DefaultTopicSelector in project storm by apache.

the class KafkaProducerTopology method newTopology.

/**
     * @param brokerUrl Kafka broker URL
     * @param topicName Topic to which publish sentences
     * @return A Storm topology that produces random sentences using {@link RandomSentenceSpout} and uses a {@link KafkaBolt} to
     * publish the sentences to the kafka topic specified
     */
public static StormTopology newTopology(String brokerUrl, String topicName) {
    final TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("spout", new RandomSentenceSpout.TimeStamped(""), 2);
    /* The output field of the RandomSentenceSpout ("word") is provided as the boltMessageField
          so that this gets written out as the message in the kafka topic. */
    final KafkaBolt<String, String> bolt = new KafkaBolt<String, String>().withProducerProperties(newProps(brokerUrl, topicName)).withTopicSelector(new DefaultTopicSelector(topicName)).withTupleToKafkaMapper(new FieldNameBasedTupleToKafkaMapper<>("key", "word"));
    builder.setBolt("forwardToKafka", bolt, 1).shuffleGrouping("spout");
    return builder.createTopology();
}
Also used : RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) DefaultTopicSelector(org.apache.storm.kafka.bolt.selector.DefaultTopicSelector)

Aggregations

DefaultTopicSelector (org.apache.storm.kafka.bolt.selector.DefaultTopicSelector)2 RandomSentenceSpout (org.apache.storm.starter.spout.RandomSentenceSpout)1 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)1