Project: Apache Heron (App Definition and Development / Streaming & Messaging)
File: topology-development-topology-api-python.md

id: version-0.20.2-incubating-topology-development-topology-api-python title: The Heron Topology API for Python sidebar_label: The Heron Topology API for Python original_id: topology-development-topology-api-python <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> The current version of `heronpy` is . Support for developing Heron topologies in Python is provided by a Python library called . #### Python API docs You can find API docs for the `heronpy` library . First, you need to install the `heronpy` library using , , or an analogous tool: ```shell $ pip install heronpy $ easy_install heronpy ``` Then you can include `heronpy` in your project files. Here's an example: ```python from heronpy.api.bolt.bolt import Bolt from heronpy.api.spout.spout import Spout from heronpy.api.topology import Topology ``` Heron are networks of that pull data into a topology and that process that ingested data. You can see how to create Python spouts in the guide and how to create Python bolts in the guide. Once you've defined spouts and bolts for a topology, you can then compose the topology in one of two ways: You can use the class inside of a main function. Here's an example: ```python from heronpy.api.topology import TopologyBuilder if name == "main": builder = TopologyBuilder("MyTopology") builder.buildandsubmit() ``` You can subclass the class. Here's an example: ```python from heronpy.api.stream import Grouping from heronpy.api.topology import Topology class MyTopology(Topology): my_spout = WordSpout.spec(par=2) my_bolt = CountBolt.spec(par=3, inputs={spout: Grouping.fields("word")}) ``` If you create a Python topology using a , you need to instantiate a `TopologyBuilder` inside of a standard Python main function, like this: ```python from heronpy.api.topology import TopologyBuilder if name == "main": builder = TopologyBuilder("MyTopology") ``` Once you've created a `TopologyBuilder` object, you can add using the method and using the method. Here's an example: ```python builder = TopologyBuilder("MyTopology") builder.addbolt("mybolt", CountBolt, par=3) builder.addspout("myspout", WordSpout, par=2) ``` Both the `addbolt` and `addspout` methods return the corresponding

The `add_bolt` method takes four arguments and an optional `config` parameter: Argument | Data type | Description | Default :--|:-|:|:- `name` | `str` | The unique identifier assigned to this bolt | | `bolt_cls` | class | The subclass of that defines this bolt | | `par` | `int` | The number of instances of this bolt in the topology | | `config` | `dict` | Specifies the configuration for this spout | `None` The `add_spout` method takes three arguments and an optional `config` parameter: Argument | Data type | Description | Default :--|:-|:|:- `name` | `str` | The unique identifier assigned to this spout | | `spout_cls` | class | The subclass of that defines this spout | | `par` | `int` | The number of instances of this spout in the topology | | `inputs` | `dict` or `list` | Either a `dict` mapping from to or a list of s, in which case the grouping is used `config` | `dict` | Specifies the configuration for this spout | `None` The following is an example implementation of a word count topology in Python that subclasses . ```python from your_spout import WordSpout from your_bolt import CountBolt from heronpy.api.stream import Grouping from heronpy.api.topology import TopologyBuilder if name == "main": builder = TopologyBuilder("WordCountTopology") wordspout = builder.addspout("word_spout", WordSpout, par=2) countbolt = builder.addbolt("countbolt", CountBolt, par=2, inputs={wordspout: Grouping.fields("word")}) builder.buildandsubmit() ``` Note that arguments to the main method can be passed by providing them in the `heron submit` command. If you're building a Python topology using a `TopologyBuilder`, you can specify configuration for the topology using the method. A topology's config is a `dict` in which the keys are a series constants from the module and values are configuration values for those parameters. Here's an example: ```python from heronpy.api import api_constants from heronpy.api.topology import TopologyBuilder if name == "main": topology_config = { apiconstants.TOPOLOGYENABLEMESSAGETIMEOUTS: True } builder = TopologyBuilder("MyTopology") builder.setconfig(topologyconfig) ``` If you want to Python topologies to a Heron cluster, they need to be packaged as a file. In order to produce PEX files, we recommend using a build tool like or . If you defined your topology by subclassing the class and built a `word_count.pex` file for that topology in the `~/topology` folder. You can submit the topology to a cluster called `local` like this: ```bash $ heron submit local \ ~/topology/word_count.pex \ # No class specified ``` Note the `-` in this submission command. If you define a topology by subclassing `TopologyBuilder` you do not need to instruct Heron where your main method is located. #### Example topologies buildable as PEXs TODO If you create a Python topology by subclassing the class, you need to create a new topology class, like this: ```python from my_spout import WordSpout from my_bolt import CountBolt from heronpy.api.stream import Grouping from heronpy.api.topology import Topology class MyTopology(Topology): my_spout = WordSpout.spec(par=2) myboltinputs = {my_spout: Grouping.fields("word")} mybolt = CountBolt.spec(par=3, inputs=mybolt_inputs) ``` All you need to do is place s as the class attributes of your topology class, which are returned by the `spec()` method of your spout or bolt class. You do not need to run a `build` method or anything like that; the `Topology` class will automatically detect which spouts and bolts are included in the topology. If you use this method to define a new Python topology, you do not need to have a main

For bolts, the method for spouts takes three optional arguments:: Argument | Data type | Description | Default :--|:-|:|:- `name` | `str` | The unique identifier assigned to this bolt or `None` if you want to use the variable name of the return `HeronComponentSpec` as the unique identifier for this bolt | | `par` | `int` | The number of instances of this bolt in the topology | | `config` | `dict` | Specifies the configuration for this bolt | `None` For spouts, the method takes four optional arguments: Argument | Data type | Description | Default :--|:-|:|:- `name` | `str` | The unique identifier assigned to this spout or `None` if you want to use the variable name of the return `HeronComponentSpec` as the unique identifier for this spout | `None` | `inputs` | `dict` or `list` | Either a `dict` mapping from to or a list of s, in which case the grouping is used `par` | `int` | The number of instances of this spout in the topology | `1` | `config` | `dict` | Specifies the configuration for this spout | `None` Here's an example topology definition with one spout and one bolt: ```python from my_spout import WordSpout from my_bolt import CountBolt from heronpy.api.stream import Grouping from heronpy.api.topology import Topology class WordCount(Topology): word_spout = WordSpout.spec(par=2) countbolt = CountBolt.spec(par=2, inputs={wordspout: Grouping.fields("word")}) ``` If you defined your topology by subclassing the class, your main Python file should not contain a main method. You will, however, need to instruct Heron which class contains your topology definition. Let's say that you've defined a topology by subclassing `Topology` and built a PEX stored in `~/topology/dist/wordcount.pex`. The class containing your topology definition is `topology.wordcount.WordCount`. You can submit the topology to a cluster called `local` like this: ```bash $ heron submit local \ ~/topology/dist/word_count.pex \ topology.word_count.WordCount \ # Specifies the topology class definition WordCountTopology ``` If you're building a Python topology by subclassing `Topology`, you can specify configuration for the topology using the method. A topology's config is a `dict` in which the keys are a series constants from the module and values are configuration values for those parameters. Here's an example: ```python from heronpy.api.topology import Topology from heronpy.api import api_constants class MyTopology(Topology): config = { apiconstants.TOPOLOGYENABLEMESSAGETIMEOUTS: True } ``` To specify that a component has multiple output streams, instead of using a list of strings for `outputs`, you can specify a list of `Stream` objects, in the following manner. ```python class MultiStreamSpout(Spout): outputs = [ Stream(fields=["normal", "fields"], name="default"), Stream(fields=["errormessage"], name="errorstream"), ] ``` To select one of these streams as the input for your bolt, you can simply use `[]` to specify the stream you want. Without any stream specified, the `default` stream will be used. ```python class MultiStreamTopology(Topology): spout = MultiStreamSpout.spec() errorbolt = ErrorBolt.spec(inputs={spout["errorstream"]: Grouping.LOWEST}) consume_bolt = ConsumeBolt.spec(inputs={spout: Grouping.SHUFFLE}) ``` In Python topologies, the output fields of your spouts and bolts need to be declared by placing `outputs` class attributes, as there is no `declareOutputFields()` method. `heronpy` enables you to dynamically declare output fields as a list using the `optional_outputs` argument in the `spec()` method. This is useful in a situation like below. ```python class IdentityBolt(Bolt): class process(self, tup): emit([tup.values]) class DynamicOutputField(Topology): spout = WordSpout.spec() bolt = IdentityBolt.spec(inputs={spout: Grouping.ALL}, optional_outputs=["word"]) ``` You can also declare outputs in the `addspout()` and the `addbolt()` method for the `TopologyBuilder` in the same way. Bolts must implement the `Bolt` interface, which has the following methods. ```python class MyBolt(Bolt): def initialize(self, config, context): pass def process(self, tup): pass ``` The `initialize()` method is called when the bolt is first initialized and provides the bolt with the executing environment. It is equivalent to `prepare()` method of the interface in

Note that you should not override `init()` constructor of `Bolt` class for initialization of custom variables, since it is used internally by HeronInstance; instead, `initialize()` should be used to initialize any custom variables or connections to databases. The `process()` method is called to process a single input `tup` of `HeronTuple` type. This method is equivalent to `execute()` method of `IBolt` interface in Java. You can use `self.emit()` method to emit the result, as described below. In addition, `BaseBolt` class provides you with the following methods. ```python class BaseBolt(BaseComponent): def emit(self, tup, stream="default", anchors=None, directtask=None, needtask_ids=False): ... def ack(self, tup): ... def fail(self, tup): ... def log(self, message, level=None): ... @staticmethod def is_tick(tup) @classmethod def spec(cls, name=None, inputs=None, par=1, config=None): ... ``` The `emit()` method is used to emit a given `tup`, which can be a `list` or `tuple` of any python objects. Unlike the Java implementation, `OutputCollector` doesn't exist in the Python implementation. The `ack()` method is used to indicate that processing of a tuple has succeeded. The `fail()` method is used to indicate that processing of a tuple has failed. The `is_tick()` method returns whether a given `tup` of `HeronTuple` type is a tick tuple. The `log()` method is used to log an arbitrary message, and its outputs are redirected to the log file of the component. It accepts an optional argument which specifies the logging level. By default, its logging level is `info`. Warning: due to internal issue, you should NOT output anything to `sys.stdout` or `sys.stderr`; instead, you should use this method to log anything you want. In order to declare the output fields of this bolt, you need to place a class attribute `outputs` as a list of `str` or `Stream`. Note that unlike Java, `declareOutputFields` does not exist in the Python implementation. Moreover, you can optionally specify the output fields from the `spec()` method from the `optional_outputs`. You will use the `spec()` method to define a topology and specify the location of this bolt within the topology, as well as to give component-specific configurations. The following is an example implementation of a bolt in Python. ```python from collections import Counter from heronpy.api.bolt.bolt import Bolt class CountBolt(Bolt): outputs = ["word", "count"] def initialize(self, config, context): self.counter = Counter() def process(self, tup): word = tup.values[0] self.counter[word] += 1 self.emit([word, self.counter[word]]) ``` To create a spout for a Heron topology, you need to subclass the class, which has the following methods. ```python class MySpout(Spout): def initialize(self, config, context): pass def next_tuple(self): pass def ack(self, tup_id): pass def fail(self, tup_id): pass def activate(self): pass def deactivate(self): pass def close(self): pass ``` The class provides a number of methods that you should implement when subclassing. The `initialize()` method is called when the spout is first initialized and provides the spout with the executing environment. It is equivalent to `open()` method of . Note that you should not override `init()` constructor of `Spout` class for initialization of custom variables, since it is used internally by HeronInstance; instead, `initialize()` should be used to initialize any custom variables or connections to databases. The `next_tuple()` method is used to fetch tuples from input source. You can emit fetched tuples by calling `self.emit()`, as described below. The `ack()` method is called when the `HeronTuple` with the `tup_id` emitted by this spout is successfully

The `fail()` method is called when the `HeronTuple` with the `tup_id` emitted by this spout is not processed successfully. The `activate()` method is called when the spout is asked to back into active state. The `deactivate()` method is called when the spout is asked to enter deactive state. The `close()` method is called when when the spout is shutdown. There is no guarantee that this method is called due to how the instance is killed. The `Spout` class inherits from the class, which also provides you methods you can use in your spouts. ```python class BaseSpout(BaseComponent): def log(self, message, level=None): ... def emit(self, tup, tupid=None, stream="default", directtask=None, needtaskids=False): ... @classmethod def spec(cls, name=None, par=1, config=None): ... ``` The `emit()` method is used to emit a given tuple, which can be a `list` or `tuple` of any Python objects. Unlike in the Java implementation, there is no `OutputCollector` in the Python implementation. The `log()` method is used to log an arbitrary message, and its outputs are redirected to the log file of the component. It accepts an optional argument which specifies the logging level. By default, its logging level is `info`. Warning: due to internal issue, you should NOT output anything to `sys.stdout` or `sys.stderr`; instead, you should use this method to log anything you want. In order to declare the output fields of this spout, you need to place a class attribute `outputs` as a list of `str` or `Stream`. Note that unlike Java, `declareOutputFields` does not exist in the Python implementation. Moreover, you can optionally specify the output fields from the `spec()` method from the `optional_outputs`. . You will use the `spec()` method to define a topology and specify the location of this spout within the topology, as well as to give component-specific configurations. The following is an example implementation of a spout in Python. ```python from itertools import cycle from heronpy.api.spout.spout import Spout class WordSpout(Spout): outputs = ['word'] def initialize(self, config, context): self.words = cycle(["hello", "world", "heron", "storm"]) self.log("Initializing WordSpout...") def next_tuple(self): word = next(self.words) self.emit([word]) ``` By default, the `submit` command also activates topologies. To disable this behavior, set the `--deploy-deactivated` flag. There are a number of example topologies that you can peruse in the directory of the : Topology | File | Description :--|:--|:-- Word count | | The spout emits random words from a list, while the bolt counts the number of words that have been emitted. Multiple streams | | The emits multiple streams to downstream bolts. Half acking | | The acks only half of all received tuples. Custom grouping | | The class provides a custom field grouping. You can build the respective PEXs for these topologies using the following commands: ```shell $ bazel build examples/src/python:word_count $ bazel build examples/src/python:multi_stream $ bazel build examples/src/python:half_acking $ bazel build examples/src/python:custom_grouping ``` All built PEXs will be stored in `bazel-bin/examples/src/python`. You can submit them to Heron like so: ```shell $ heron submit local \ bazel-bin/examples/src/python/word_count.pex - \ WordCount $ heron submit local \ bazel-bin/examples/src/python/multi_stream.pex \ heron.examples.src.python.multistreamtopology.MultiStream $ heron submit local \ bazel-bin/examples/src/python/half_acking.pex - \ HalfAcking $ heron submit local \ bazel-bin/examples/src/python/custom_grouping.pex \ heron.examples.src.python.customgroupingtopology.CustomGrouping ``` By default, the `submit` command also activates topologies. To disable this behavior, set the `--deploy-deactivated` flag.