Search in sources :

Example 6 with Builder

use of org.apache.heron.streamlet.Builder in project heron by twitter.

the class SimplePulsarSourceTopology method main.

/**
 * All Heron topologies require a main function that defines the topology's behavior
 * at runtime
 */
public static void main(String[] args) throws Exception {
    Builder processingGraphBuilder = Builder.newBuilder();
    /**
     * A Pulsar source is constructed for a specific Pulsar installation, topic, and
     * subsecription.
     */
    Source<String> pulsarSource = new PulsarSource(// Pulsar connection URL
    "pulsar://localhost:6650", // Pulsar topic
    "persistent://sample/nomad/ns1/heron-pulsar-test-topic", // Subscription name for the Pulsar topic
    "subscription-1");
    /**
     * In this processing graph, the source streamlet consists of messages on a
     * Pulsar topic. Those messages are simply logged without any processing logic
     * applied to them.
     */
    processingGraphBuilder.newSource(pulsarSource).setName("incoming-pulsar-messages").consume(s -> LOG.info(String.format("Message received from Pulsar: \"%s\"", s)));
    Config config = Config.defaultConfig();
    // Fetches the topology name from the first command-line argument
    String topologyName = StreamletUtils.getTopologyName(args);
    // Finally, the processing graph and configuration are passed to the Runner, which converts
    // the graph into a Heron topology that can be run in a Heron cluster.
    new Runner().run(topologyName, config, processingGraphBuilder);
}
Also used : Runner(org.apache.heron.streamlet.Runner) Config(org.apache.heron.streamlet.Config) Builder(org.apache.heron.streamlet.Builder)

Example 7 with Builder

use of org.apache.heron.streamlet.Builder in project heron by twitter.

the class FilesystemSinkTopology method main.

/**
 * All Heron topologies require a main function that defines the topology's behavior
 * at runtime
 */
public static void main(String[] args) throws Exception {
    Builder processingGraphBuilder = Builder.newBuilder();
    // Creates a temporary file to write output into.
    File file = File.createTempFile("filesystem-sink-example", ".tmp");
    LOG.info(String.format("Ready to write to file %s", file.getAbsolutePath()));
    processingGraphBuilder.newSource(() -> {
        // This applies a "brake" that makes the processing graph write
        // to the temporary file at a reasonable, readable pace.
        StreamletUtils.sleep(500);
        return ThreadLocalRandom.current().nextInt(100);
    }).setName("incoming-integers").toSink(new FilesystemSink<>(file));
    // The topology's parallelism (the number of containers across which the topology's
    // processing instance will be split) can be defined via the second command-line
    // argument (or else the default of 2 will be used).
    int topologyParallelism = StreamletUtils.getParallelism(args, 2);
    Config config = Config.newBuilder().setNumContainers(topologyParallelism).build();
    // Fetches the topology name from the first command-line argument
    String topologyName = StreamletUtils.getTopologyName(args);
    // Finally, the processing graph and configuration are passed to the Runner, which converts
    // the graph into a Heron topology that can be run in a Heron cluster.
    new Runner().run(topologyName, config, processingGraphBuilder);
}
Also used : Runner(org.apache.heron.streamlet.Runner) Config(org.apache.heron.streamlet.Config) Builder(org.apache.heron.streamlet.Builder) File(java.io.File)

Example 8 with Builder

use of org.apache.heron.streamlet.Builder in project heron by twitter.

the class FormattedOutputTopology method main.

public static void main(String[] args) throws Exception {
    Builder processingGraphBuilder = Builder.newBuilder();
    processingGraphBuilder.newSource(SensorReading::new).filter(reading -> reading.getHumidity() < .9 && reading.getTemperature() < 90).consume(reading -> LOG.info(String.format("Reading from device %s: (temp: %f, humidity: %f)", reading.getDeviceId(), reading.getTemperature(), reading.getHumidity())));
    // Fetches the topology name from the first command-line argument
    String topologyName = StreamletUtils.getTopologyName(args);
    Config config = Config.defaultConfig();
    // Finally, the processing graph and configuration are passed to the Runner, which converts
    // the graph into a Heron topology that can be run in a Heron cluster.
    new Runner().run(topologyName, config, processingGraphBuilder);
}
Also used : IntStream(java.util.stream.IntStream) List(java.util.List) Runner(org.apache.heron.streamlet.Runner) StreamletUtils(org.apache.heron.examples.streamlet.utils.StreamletUtils) Random(java.util.Random) Builder(org.apache.heron.streamlet.Builder) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Config(org.apache.heron.streamlet.Config) Runner(org.apache.heron.streamlet.Runner) Config(org.apache.heron.streamlet.Config) Builder(org.apache.heron.streamlet.Builder)

Example 9 with Builder

use of org.apache.heron.streamlet.Builder in project heron by twitter.

the class ImpressionsAndClicksTopology method main.

/**
 * All Heron topologies require a main function that defines the topology's behavior
 * at runtime
 */
public static void main(String[] args) throws Exception {
    Builder processingGraphBuilder = Builder.newBuilder();
    // A KVStreamlet is produced. Each element is a KeyValue object where the key
    // is the impression ID and the user ID is the value.
    Streamlet<AdImpression> impressions = processingGraphBuilder.newSource(AdImpression::new);
    // A KVStreamlet is produced. Each element is a KeyValue object where the key
    // is the ad ID and the user ID is the value.
    Streamlet<AdClick> clicks = processingGraphBuilder.newSource(AdClick::new);
    /**
     * Here, the impressions KVStreamlet is joined to the clicks KVStreamlet.
     */
    impressions.join(// The other streamlet that's being joined to
    clicks, // Key extractor for the impressions streamlet
    impression -> impression.getUserId(), // Key extractor for the clicks streamlet
    click -> click.getUserId(), // Window configuration for the join operation
    WindowConfig.TumblingCountWindow(25), // Join type (inner join means that all elements from both streams will be included)
    JoinType.INNER, // if the ad IDs match between the elements (or a value of 0 if they don't).
    (user1, user2) -> (user1.getAdId().equals(user2.getAdId())) ? 1 : 0).reduceByKeyAndWindow(// Key extractor for the reduce operation
    kv -> String.format("user-%s", kv.getKey().getKey()), // Value extractor for the reduce operation
    kv -> kv.getValue(), // Window configuration for the reduce operation
    WindowConfig.TumblingCountWindow(50), // A running cumulative total is calculated for each key
    (cumulative, incoming) -> cumulative + incoming).consume(kw -> {
        LOG.info(String.format("(user: %s, clicks: %d)", kw.getKey().getKey(), kw.getValue()));
    });
    Config config = Config.defaultConfig();
    // Fetches the topology name from the first command-line argument
    String topologyName = StreamletUtils.getTopologyName(args);
    // Finally, the processing graph and configuration are passed to the Runner, which converts
    // the graph into a Heron topology that can be run in a Heron cluster.
    new Runner().run(topologyName, config, processingGraphBuilder);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Runner(org.apache.heron.streamlet.Runner) WindowConfig(org.apache.heron.streamlet.WindowConfig) JoinType(org.apache.heron.streamlet.JoinType) StreamletUtils(org.apache.heron.examples.streamlet.utils.StreamletUtils) Builder(org.apache.heron.streamlet.Builder) UUID(java.util.UUID) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Config(org.apache.heron.streamlet.Config) Streamlet(org.apache.heron.streamlet.Streamlet) List(java.util.List) Runner(org.apache.heron.streamlet.Runner) WindowConfig(org.apache.heron.streamlet.WindowConfig) Config(org.apache.heron.streamlet.Config) Builder(org.apache.heron.streamlet.Builder)

Example 10 with Builder

use of org.apache.heron.streamlet.Builder in project heron by twitter.

the class RepartitionTopology method main.

/**
 * All Heron topologies require a main function that defines the topology's behavior
 * at runtime
 */
public static void main(String[] args) throws Exception {
    Builder processingGraphBuilder = Builder.newBuilder();
    Streamlet<Integer> randomIntegers = processingGraphBuilder.newSource(() -> {
        // Random integers are emitted every 50 milliseconds
        StreamletUtils.sleep(50);
        return ThreadLocalRandom.current().nextInt(100);
    }).setNumPartitions(2).setName("random-integer-source");
    randomIntegers.repartition(8, RepartitionTopology::repartitionStreamlet).setName("repartition-incoming-values").repartition(2).setName("reduce-partitions-for-logging-operation").log();
    // Fetches the topology name from the first command-line argument
    String topologyName = StreamletUtils.getTopologyName(args);
    Config config = Config.defaultConfig();
    // Finally, the processing graph and configuration are passed to the Runner, which converts
    // the graph into a Heron topology that can be run in a Heron cluster.
    new Runner().run(topologyName, config, processingGraphBuilder);
}
Also used : Runner(org.apache.heron.streamlet.Runner) Config(org.apache.heron.streamlet.Config) Builder(org.apache.heron.streamlet.Builder)

Aggregations

Builder (org.apache.heron.streamlet.Builder)16 Config (org.apache.heron.streamlet.Config)12 Runner (org.apache.heron.streamlet.Runner)12 Streamlet (org.apache.heron.streamlet.Streamlet)5 MalformedURLException (java.net.MalformedURLException)4 Arrays (java.util.Arrays)4 List (java.util.List)4 Config (org.apache.heron.api.Config)4 AbstractTestTopology (org.apache.heron.integration_test.common.AbstractTestTopology)4 TestTopologyBuilder (org.apache.heron.integration_test.core.TestTopologyBuilder)4 BuilderImpl (org.apache.heron.streamlet.impl.BuilderImpl)4 Logger (java.util.logging.Logger)3 StreamletUtils (org.apache.heron.examples.streamlet.utils.StreamletUtils)3 WindowConfig (org.apache.heron.streamlet.WindowConfig)3 Serializable (java.io.Serializable)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2