|
| 1 | +# |
| 2 | +# Copyright IBM Corporation 2021 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import ray |
| 18 | +import rayvens |
| 19 | +import sys |
| 20 | +import time |
| 21 | + |
| 22 | +# An artificial example of using Kafka sources and sink. |
| 23 | +# Typically the user application will interact with an external Kafka |
| 24 | +# service to either subscribe or publish data to other services: |
| 25 | +# |
| 26 | +# EXT. SERVICE => KAFKA => RAYVENS KAFKA SOURCE |
| 27 | +# or |
| 28 | +# RAYVENS KAFKA SINK => KAFKA => EXT. SERVICE |
| 29 | +# |
| 30 | +# In this example we put together an artificial example where, to |
| 31 | +# demonstrate both Kafka sources and sinks at the same time we |
| 32 | +# set a Kafka sink to publish to a test topic then have a Kafka |
| 33 | +# source read from that test topic: |
| 34 | +# |
| 35 | +# RAYVENS KAFKA SINK => KAFKA => RAYVENS KAFKA SOURCE |
| 36 | +# |
| 37 | + |
| 38 | +# Command line arguments and validation: |
| 39 | +if len(sys.argv) < 2: |
| 40 | + print(f'usage: {sys.argv[0]} <brokers> <password> <run_mode> OR' |
| 41 | + f' {sys.argv[0]} <run_mode>') |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | +# Brokers and run mode: |
| 45 | +brokers = None |
| 46 | +password = None |
| 47 | +run_mode = sys.argv[1] |
| 48 | +if len(sys.argv) == 4: |
| 49 | + brokers = sys.argv[1] |
| 50 | + password = sys.argv[2] |
| 51 | + run_mode = sys.argv[3] |
| 52 | + |
| 53 | +if run_mode not in ['local', 'mixed', 'operator']: |
| 54 | + raise RuntimeError(f'Invalid run mode provided: {run_mode}') |
| 55 | + |
| 56 | +# The Kafka topic used for communication. |
| 57 | +topic = "externalTopic" |
| 58 | + |
| 59 | +# If using the Kafka broker started by Rayvens the following brokers |
| 60 | +# are possible: |
| 61 | +# - from inside the cluster: kafka:9092 |
| 62 | +# - from outside the cluster: localhost:31093 |
| 63 | +# If using a different Kafka service please provide the brokers in the |
| 64 | +# form of host:port,host1:port1, ... . |
| 65 | +if brokers is None: |
| 66 | + brokers = 'localhost:31093' |
| 67 | + if run_mode == 'operator': |
| 68 | + brokers = "kafka:9092" |
| 69 | + |
| 70 | +# Initialize ray either on the cluster or locally otherwise. |
| 71 | +if run_mode == 'operator': |
| 72 | + ray.init(address='auto') |
| 73 | +else: |
| 74 | + ray.init() |
| 75 | + |
| 76 | +# Start rayvens in operator mode. |
| 77 | +rayvens.init(mode=run_mode) |
| 78 | + |
| 79 | +# Create source stream and configuration. |
| 80 | +source_stream = rayvens.Stream('kafka-source-stream') |
| 81 | + |
| 82 | +source_config = dict(kind='kafka-source', |
| 83 | + route='/fromkafka', |
| 84 | + topic=topic, |
| 85 | + brokers=brokers, |
| 86 | + partitions=3) |
| 87 | +if password is not None: |
| 88 | + source_config['SASL_password'] = password |
| 89 | +source = source_stream.add_source(source_config) |
| 90 | +# Log all events from stream-attached sources. |
| 91 | +source_stream >> (lambda event: print('KAFKA SOURCE:', event)) |
| 92 | + |
| 93 | +# Create sink stream and configuration. |
| 94 | +sink_stream = rayvens.Stream('kafka-sink-stream') |
| 95 | +sink_config = dict(kind='kafka-sink', |
| 96 | + route='/tokafka', |
| 97 | + topic=topic, |
| 98 | + brokers=brokers, |
| 99 | + partitions=3) |
| 100 | +if password is not None: |
| 101 | + sink_config['SASL_password'] = password |
| 102 | +sink = sink_stream.add_sink(sink_config) |
| 103 | + |
| 104 | +time.sleep(10) |
| 105 | + |
| 106 | +# Sends message to all sinks attached to this stream. |
| 107 | +sink_stream << f'Sending message to Kafka sink in run mode {run_mode}.' |
| 108 | + |
| 109 | +# Give a grace period to the message to propagate then disconnect source |
| 110 | +# and sink. |
| 111 | +time.sleep(30) |
| 112 | +source_stream.disconnect_all() |
| 113 | +sink_stream.disconnect_all() |
0 commit comments