Search in sources :

Example 1 with ParserBolt

use of org.apache.metron.parsers.bolt.ParserBolt in project metron by apache.

the class ParserTopologyBuilder method build.

/**
 * Builds a Storm topology that parses telemetry data received from an external sensor.
 *
 * @param zookeeperUrl             Zookeeper URL
 * @param brokerUrl                Kafka Broker URL
 * @param sensorType               Type of sensor
 * @param spoutParallelismSupplier         Supplier for the parallelism hint for the spout
 * @param spoutNumTasksSupplier            Supplier for the number of tasks for the spout
 * @param parserParallelismSupplier        Supplier for the parallelism hint for the parser bolt
 * @param parserNumTasksSupplier           Supplier for the number of tasks for the parser bolt
 * @param errorWriterParallelismSupplier   Supplier for the parallelism hint for the bolt that handles errors
 * @param errorWriterNumTasksSupplier      Supplier for the number of tasks for the bolt that handles errors
 * @param kafkaSpoutConfigSupplier         Supplier for the configuration options for the kafka spout
 * @param securityProtocolSupplier         Supplier for the security protocol
 * @param outputTopic                      The output kafka topic
 * @param stormConfigSupplier              Supplier for the storm config
 * @return A Storm topology that parses telemetry data received from an external sensor
 * @throws Exception
 */
public static ParserTopology build(String zookeeperUrl, Optional<String> brokerUrl, String sensorType, ValueSupplier<Integer> spoutParallelismSupplier, ValueSupplier<Integer> spoutNumTasksSupplier, ValueSupplier<Integer> parserParallelismSupplier, ValueSupplier<Integer> parserNumTasksSupplier, ValueSupplier<Integer> errorWriterParallelismSupplier, ValueSupplier<Integer> errorWriterNumTasksSupplier, ValueSupplier<Map> kafkaSpoutConfigSupplier, ValueSupplier<String> securityProtocolSupplier, Optional<String> outputTopic, ValueSupplier<Config> stormConfigSupplier) throws Exception {
    // fetch configuration from zookeeper
    ParserConfigurations configs = new ParserConfigurations();
    SensorParserConfig parserConfig = getSensorParserConfig(zookeeperUrl, sensorType, configs);
    int spoutParallelism = spoutParallelismSupplier.get(parserConfig, Integer.class);
    int spoutNumTasks = spoutNumTasksSupplier.get(parserConfig, Integer.class);
    int parserParallelism = parserParallelismSupplier.get(parserConfig, Integer.class);
    int parserNumTasks = parserNumTasksSupplier.get(parserConfig, Integer.class);
    int errorWriterParallelism = errorWriterParallelismSupplier.get(parserConfig, Integer.class);
    int errorWriterNumTasks = errorWriterNumTasksSupplier.get(parserConfig, Integer.class);
    Map<String, Object> kafkaSpoutConfig = kafkaSpoutConfigSupplier.get(parserConfig, Map.class);
    Optional<String> securityProtocol = Optional.ofNullable(securityProtocolSupplier.get(parserConfig, String.class));
    // create the spout
    TopologyBuilder builder = new TopologyBuilder();
    KafkaSpout kafkaSpout = createKafkaSpout(zookeeperUrl, sensorType, securityProtocol, Optional.ofNullable(kafkaSpoutConfig), parserConfig);
    builder.setSpout("kafkaSpout", kafkaSpout, spoutParallelism).setNumTasks(spoutNumTasks);
    // create the parser bolt
    ParserBolt parserBolt = createParserBolt(zookeeperUrl, brokerUrl, sensorType, securityProtocol, configs, parserConfig, outputTopic);
    builder.setBolt("parserBolt", parserBolt, parserParallelism).setNumTasks(parserNumTasks).localOrShuffleGrouping("kafkaSpout");
    // create the error bolt, if needed
    if (errorWriterNumTasks > 0) {
        WriterBolt errorBolt = createErrorBolt(zookeeperUrl, brokerUrl, sensorType, securityProtocol, configs, parserConfig);
        builder.setBolt("errorMessageWriter", errorBolt, errorWriterParallelism).setNumTasks(errorWriterNumTasks).localOrShuffleGrouping("parserBolt", Constants.ERROR_STREAM);
    }
    return new ParserTopology(builder, stormConfigSupplier.get(parserConfig, Config.class));
}
Also used : TopologyBuilder(org.apache.storm.topology.TopologyBuilder) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) KafkaSpoutConfig(org.apache.storm.kafka.spout.KafkaSpoutConfig) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Config(org.apache.storm.Config) ParserBolt(org.apache.metron.parsers.bolt.ParserBolt) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) WriterBolt(org.apache.metron.parsers.bolt.WriterBolt) ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) JSONObject(org.json.simple.JSONObject) KafkaSpout(org.apache.storm.kafka.spout.KafkaSpout) StormKafkaSpout(org.apache.metron.storm.kafka.flux.StormKafkaSpout)

Example 2 with ParserBolt

use of org.apache.metron.parsers.bolt.ParserBolt in project metron by apache.

the class ParserTopologyBuilder method createParserBolt.

/**
 * Create a bolt that parses input from a sensor.
 *
 * @param zookeeperUrl Zookeeper URL
 * @param brokerUrl    Kafka Broker URL
 * @param sensorType   Type of sensor that is being consumed.
 * @param configs
 * @param parserConfig
 * @return A Storm bolt that parses input from a sensor
 */
private static ParserBolt createParserBolt(String zookeeperUrl, Optional<String> brokerUrl, String sensorType, Optional<String> securityProtocol, ParserConfigurations configs, SensorParserConfig parserConfig, Optional<String> outputTopic) {
    // create message parser
    MessageParser<JSONObject> parser = ReflectionUtils.createInstance(parserConfig.getParserClassName());
    parser.configure(parserConfig.getParserConfig());
    // create writer - if not configured uses a sensible default
    AbstractWriter writer = parserConfig.getWriterClassName() == null ? createKafkaWriter(brokerUrl, zookeeperUrl, securityProtocol).withTopic(outputTopic.orElse(Constants.ENRICHMENT_TOPIC)) : ReflectionUtils.createInstance(parserConfig.getWriterClassName());
    writer.configure(sensorType, new ParserWriterConfiguration(configs));
    // create a writer handler
    WriterHandler writerHandler = createWriterHandler(writer);
    return new ParserBolt(zookeeperUrl, sensorType, parser, writerHandler);
}
Also used : JSONObject(org.json.simple.JSONObject) WriterHandler(org.apache.metron.parsers.bolt.WriterHandler) ParserBolt(org.apache.metron.parsers.bolt.ParserBolt) ParserWriterConfiguration(org.apache.metron.common.configuration.writer.ParserWriterConfiguration) AbstractWriter(org.apache.metron.writer.AbstractWriter)

Aggregations

ParserBolt (org.apache.metron.parsers.bolt.ParserBolt)2 JSONObject (org.json.simple.JSONObject)2 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)1 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)1 SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)1 ParserWriterConfiguration (org.apache.metron.common.configuration.writer.ParserWriterConfiguration)1 WriterBolt (org.apache.metron.parsers.bolt.WriterBolt)1 WriterHandler (org.apache.metron.parsers.bolt.WriterHandler)1 StormKafkaSpout (org.apache.metron.storm.kafka.flux.StormKafkaSpout)1 AbstractWriter (org.apache.metron.writer.AbstractWriter)1 Config (org.apache.storm.Config)1 KafkaSpout (org.apache.storm.kafka.spout.KafkaSpout)1 KafkaSpoutConfig (org.apache.storm.kafka.spout.KafkaSpoutConfig)1 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)1