Search in sources :

Example 1 with EventsGeneratorSource

use of org.apache.flink.streaming.examples.statemachine.generator.EventsGeneratorSource in project flink by apache.

the class StateMachineExample method main.

/**
 * Main entry point for the program.
 *
 * @param args The command line arguments.
 */
public static void main(String[] args) throws Exception {
    // ---- print some usage help ----
    System.out.println("Usage with built-in data generator: StateMachineExample [--error-rate <probability-of-invalid-transition>] [--sleep <sleep-per-record-in-ms>]");
    System.out.println("Usage with Kafka: StateMachineExample --kafka-topic <topic> [--brokers <brokers>]");
    System.out.println("Options for both the above setups: ");
    System.out.println("\t[--backend <hashmap|rocks>]");
    System.out.println("\t[--checkpoint-dir <filepath>]");
    System.out.println("\t[--incremental-checkpoints <true|false>]");
    System.out.println("\t[--output <filepath> OR null for stdout]");
    System.out.println();
    // ---- determine whether to use the built-in source, or read from Kafka ----
    final DataStream<Event> events;
    final ParameterTool params = ParameterTool.fromArgs(args);
    // create the environment to create streams and configure execution
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.enableCheckpointing(2000L);
    final String stateBackend = params.get("backend", "memory");
    if ("hashmap".equals(stateBackend)) {
        final String checkpointDir = params.get("checkpoint-dir");
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage(checkpointDir);
    } else if ("rocks".equals(stateBackend)) {
        final String checkpointDir = params.get("checkpoint-dir");
        boolean incrementalCheckpoints = params.getBoolean("incremental-checkpoints", false);
        env.setStateBackend(new EmbeddedRocksDBStateBackend(incrementalCheckpoints));
        env.getCheckpointConfig().setCheckpointStorage(checkpointDir);
    }
    if (params.has("kafka-topic")) {
        // set up the Kafka reader
        String kafkaTopic = params.get("kafka-topic");
        String brokers = params.get("brokers", "localhost:9092");
        System.out.printf("Reading from kafka topic %s @ %s\n", kafkaTopic, brokers);
        System.out.println();
        KafkaSource<Event> source = KafkaSource.<Event>builder().setBootstrapServers(brokers).setGroupId("stateMachineExample").setTopics(kafkaTopic).setDeserializer(KafkaRecordDeserializationSchema.valueOnly(new EventDeSerializationSchema())).setStartingOffsets(OffsetsInitializer.latest()).build();
        events = env.fromSource(source, WatermarkStrategy.noWatermarks(), "StateMachineExampleSource");
    } else {
        double errorRate = params.getDouble("error-rate", 0.0);
        int sleep = params.getInt("sleep", 1);
        System.out.printf("Using standalone source with error rate %f and sleep delay %s millis\n", errorRate, sleep);
        System.out.println();
        events = env.addSource(new EventsGeneratorSource(errorRate, sleep));
    }
    // ---- main program ----
    final String outputFile = params.get("output");
    // make parameters available in the web interface
    env.getConfig().setGlobalJobParameters(params);
    DataStream<Alert> alerts = events.keyBy(Event::sourceAddress).flatMap(new StateMachineMapper());
    // output the alerts to std-out
    if (outputFile == null) {
        alerts.print();
    } else {
        alerts.sinkTo(FileSink.<Alert>forRowFormat(new Path(outputFile), new SimpleStringEncoder<>()).withRollingPolicy(DefaultRollingPolicy.builder().withMaxPartSize(MemorySize.ofMebiBytes(1)).withRolloverInterval(Duration.ofSeconds(10)).build()).build()).setParallelism(1).name("output");
    }
    // trigger program execution
    env.execute("State machine job");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) Path(org.apache.flink.core.fs.Path) EmbeddedRocksDBStateBackend(org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend) EventsGeneratorSource(org.apache.flink.streaming.examples.statemachine.generator.EventsGeneratorSource) Event(org.apache.flink.streaming.examples.statemachine.event.Event) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) HashMapStateBackend(org.apache.flink.runtime.state.hashmap.HashMapStateBackend) Alert(org.apache.flink.streaming.examples.statemachine.event.Alert) EventDeSerializationSchema(org.apache.flink.streaming.examples.statemachine.kafka.EventDeSerializationSchema) SimpleStringEncoder(org.apache.flink.api.common.serialization.SimpleStringEncoder)

Example 2 with EventsGeneratorSource

use of org.apache.flink.streaming.examples.statemachine.generator.EventsGeneratorSource in project flink by apache.

the class KafkaEventsGeneratorJob method main.

public static void main(String[] args) throws Exception {
    final ParameterTool params = ParameterTool.fromArgs(args);
    double errorRate = params.getDouble("error-rate", 0.0);
    int sleep = params.getInt("sleep", 1);
    String kafkaTopic = params.get("kafka-topic");
    String brokers = params.get("brokers", "localhost:9092");
    System.out.printf("Generating events to Kafka with standalone source with error rate %f and sleep delay %s millis\n", errorRate, sleep);
    System.out.println();
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.addSource(new EventsGeneratorSource(errorRate, sleep)).sinkTo(KafkaSink.<Event>builder().setBootstrapServers(brokers).setRecordSerializer(KafkaRecordSerializationSchema.builder().setValueSerializationSchema(new EventDeSerializationSchema()).setTopic(kafkaTopic).build()).build());
    // trigger program execution
    env.execute("State machine example Kafka events generator job");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) EventsGeneratorSource(org.apache.flink.streaming.examples.statemachine.generator.EventsGeneratorSource) Event(org.apache.flink.streaming.examples.statemachine.event.Event) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) EventDeSerializationSchema(org.apache.flink.streaming.examples.statemachine.kafka.EventDeSerializationSchema)

Aggregations

ParameterTool (org.apache.flink.api.java.utils.ParameterTool)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 Event (org.apache.flink.streaming.examples.statemachine.event.Event)2 EventsGeneratorSource (org.apache.flink.streaming.examples.statemachine.generator.EventsGeneratorSource)2 EventDeSerializationSchema (org.apache.flink.streaming.examples.statemachine.kafka.EventDeSerializationSchema)2 SimpleStringEncoder (org.apache.flink.api.common.serialization.SimpleStringEncoder)1 EmbeddedRocksDBStateBackend (org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend)1 Path (org.apache.flink.core.fs.Path)1 HashMapStateBackend (org.apache.flink.runtime.state.hashmap.HashMapStateBackend)1 Alert (org.apache.flink.streaming.examples.statemachine.event.Alert)1