use of com.twitter.heron.streamlet.Runner in project incubator-heron by apache.
the class IntegerProcessingTopology 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 builder = Builder.newBuilder();
Streamlet<Integer> zeroes = builder.newSource(() -> 0);
builder.newSource(() -> ThreadLocalRandom.current().nextInt(1, 11)).setName("random-ints").map(i -> i + 1).setName("add-one").union(zeroes).setName("unify-streams").filter(i -> i != 2).setName("remove-twos").log();
Config config = Config.newBuilder().setNumContainers(NUM_CONTAINERS).setPerContainerRamInGigabytes(GIGABYTES_OF_RAM).setPerContainerCpu(CPU).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, builder);
}
use of com.twitter.heron.streamlet.Runner in project incubator-heron by apache.
the class SmartWatchTopology method main.
public static void main(String[] args) throws Exception {
Builder processingGraphBuilder = Builder.newBuilder();
processingGraphBuilder.newSource(SmartWatchReading::new).setName("incoming-watch-readings").reduceByKeyAndWindow(// Key extractor
reading -> reading.getJoggerId(), // Value extractor
reading -> reading.getFeetRun(), // The time window (1 minute of clock time)
WindowConfig.TumblingTimeWindow(Duration.ofSeconds(10)), // The reduce function (produces a cumulative sum)
(cumulative, incoming) -> cumulative + incoming).setName("reduce-to-total-distance-per-jogger").map(keyWindow -> {
// The per-key result of the previous reduce step
long totalFeetRun = keyWindow.getValue();
// The amount of time elapsed
long startTime = keyWindow.getKey().getWindow().getStartTime();
long endTime = keyWindow.getKey().getWindow().getEndTime();
// Cast to float to use as denominator
long timeLengthMillis = endTime - startTime;
// The feet-per-minute calculation
float feetPerMinute = totalFeetRun / (float) (timeLengthMillis / 1000);
// Reduce to two decimal places
String paceString = new DecimalFormat("#.##").format(feetPerMinute);
// Return a per-jogger average pace
return new KeyValue<>(keyWindow.getKey().getKey(), paceString);
}).setName("calculate-average-speed").consume(kv -> {
String logMessage = String.format("(runner: %s, avgFeetPerMinute: %s)", kv.getKey(), kv.getValue());
LOG.info(logMessage);
});
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);
}
use of com.twitter.heron.streamlet.Runner in project incubator-heron by apache.
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/standalone/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);
}
use of com.twitter.heron.streamlet.Runner in project incubator-heron by apache.
the class WindowedWordCountTopology method main.
public static void main(String[] args) throws Exception {
Builder processingGraphBuilder = Builder.newBuilder();
processingGraphBuilder.newSource(() -> StreamletUtils.randomFromList(SENTENCES)).setName("random-sentences-source").flatMap(sentence -> Arrays.asList(sentence.toLowerCase().split("\\s+"))).setName("flatten-into-individual-words").reduceByKeyAndWindow(// The key extractor (the word is left unchanged)
word -> word, // Value extractor (the value is always 1)
word -> 1, WindowConfig.TumblingCountWindow(50), (x, y) -> x + y).setName("reduce-operation").consume(kv -> {
String logMessage = String.format("(word: %s, count: %d)", kv.getKey().getKey(), kv.getValue());
LOG.info(logMessage);
});
// 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);
}
use of com.twitter.heron.streamlet.Runner in project incubator-heron by apache.
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);
}
Aggregations