davidcavazos commented on a change in pull request #12188: URL: https://github.com/apache/beam/pull/12188#discussion_r451686296
########## File path: sdks/python/apache_beam/examples/kafkataxi/kafka_taxi.py ########## @@ -0,0 +1,87 @@ +# +# 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. +# + +"""An example that writes to and reads from Kafka. + + This example reads from the PubSub NYC Taxi stream described in + https://github.com/googlecodelabs/cloud-dataflow-nyc-taxi-tycoon, writes to a + given Kafka topic and reads back from the same Kafka topic. + """ + +# pytype: skip-file + +from __future__ import absolute_import + +import argparse +import logging +import typing + +import apache_beam as beam +from apache_beam.io.kafka import ReadFromKafka +from apache_beam.io.kafka import WriteToKafka +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions + + +def run(argv=None): + """Main entry point; defines and runs the wordcount pipeline.""" + parser = argparse.ArgumentParser() + parser.add_argument( + '--bootstrap_servers', + dest='bootstrap_servers', + required=True, + help='Bootstrap servers for the Kafka cluster. Should be accessible by ' + 'the runner') + parser.add_argument( + '--topic', + dest='topic', + default='kafka_taxirides_realtime', + help='Kafka topic to write to and read from') + known_args, pipeline_args = parser.parse_known_args(argv) + + pipeline_options = PipelineOptions(pipeline_args) + pipeline_options.view_as(SetupOptions).save_main_session = True + pipeline_options.view_as(StandardOptions).streaming = True + + pipeline = beam.Pipeline(options=pipeline_options) + bootstrap_servers = known_args.bootstrap_servers + _ = ( + pipeline + | beam.io.ReadFromPubSub( + topic='projects/pubsub-public-data/topics/taxirides-realtime'). + with_output_types(bytes) + | beam.Map(lambda x: (b'', x)).with_output_types( + typing.Tuple[bytes, bytes]) + | beam.WindowInto(beam.window.FixedWindows(15)) Review comment: Can we make this into either a constant or another command line argument with a default value? It's also a good idea to mention the units, are these seconds or minutes? ########## File path: sdks/python/apache_beam/examples/kafkataxi/kafka_taxi.py ########## @@ -0,0 +1,87 @@ +# +# 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. +# + +"""An example that writes to and reads from Kafka. + + This example reads from the PubSub NYC Taxi stream described in + https://github.com/googlecodelabs/cloud-dataflow-nyc-taxi-tycoon, writes to a + given Kafka topic and reads back from the same Kafka topic. + """ + +# pytype: skip-file + +from __future__ import absolute_import + +import argparse +import logging +import typing + +import apache_beam as beam +from apache_beam.io.kafka import ReadFromKafka +from apache_beam.io.kafka import WriteToKafka +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions + + +def run(argv=None): + """Main entry point; defines and runs the wordcount pipeline.""" + parser = argparse.ArgumentParser() + parser.add_argument( + '--bootstrap_servers', + dest='bootstrap_servers', + required=True, + help='Bootstrap servers for the Kafka cluster. Should be accessible by ' + 'the runner') + parser.add_argument( + '--topic', + dest='topic', + default='kafka_taxirides_realtime', + help='Kafka topic to write to and read from') + known_args, pipeline_args = parser.parse_known_args(argv) + + pipeline_options = PipelineOptions(pipeline_args) + pipeline_options.view_as(SetupOptions).save_main_session = True + pipeline_options.view_as(StandardOptions).streaming = True + + pipeline = beam.Pipeline(options=pipeline_options) Review comment: Why not use `with` statement? ```py with beam.Pipeline(options=pipeline_options) as pipeline: _ = ( pipeline | beam.io.ReadFromPubsub(... ... ) _ = ( pipeline | ReadFromKafka(... ... ) ``` ########## File path: sdks/python/apache_beam/examples/kafkataxi/kafka_taxi.py ########## @@ -0,0 +1,87 @@ +# +# 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. +# + +"""An example that writes to and reads from Kafka. + + This example reads from the PubSub NYC Taxi stream described in + https://github.com/googlecodelabs/cloud-dataflow-nyc-taxi-tycoon, writes to a + given Kafka topic and reads back from the same Kafka topic. + """ + +# pytype: skip-file + +from __future__ import absolute_import + +import argparse +import logging +import typing + +import apache_beam as beam +from apache_beam.io.kafka import ReadFromKafka +from apache_beam.io.kafka import WriteToKafka +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions + + +def run(argv=None): Review comment: In general, for Python samples, we usually have the `run` function accept the actual values the function needs to run instead of `argv`, and we let `__main__` use `argparse` to parse the arguments. This allows users to more easily import the sample "as a library" and run the sample from within a script. It also makes it easier to test. It's also a good idea to show a commented example of how those arguments would look like. Ideally, if they copy-paste the contents of the `run` function and uncomment those examples, it should run, although some inputs might need tweaking like a project ID or something like that. ```py def run(bootstrap_servers, topic, pipeline_args): # bootstrap_servers = '...' # topic = 'kafka_taxirides_realtime' # pipeline_args = ['--project', 'my-project', ...] ... if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() ... args, pipeline_args = parser.parse_known_args() run(args.bootstrap_servers, args.topic, pipeline_args) ``` ########## File path: sdks/python/apache_beam/examples/kafkataxi/kafka_taxi.py ########## @@ -0,0 +1,87 @@ +# +# 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. +# + +"""An example that writes to and reads from Kafka. + + This example reads from the PubSub NYC Taxi stream described in + https://github.com/googlecodelabs/cloud-dataflow-nyc-taxi-tycoon, writes to a + given Kafka topic and reads back from the same Kafka topic. + """ + +# pytype: skip-file + +from __future__ import absolute_import + +import argparse +import logging +import typing + +import apache_beam as beam +from apache_beam.io.kafka import ReadFromKafka +from apache_beam.io.kafka import WriteToKafka +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions + + +def run(argv=None): + """Main entry point; defines and runs the wordcount pipeline.""" + parser = argparse.ArgumentParser() + parser.add_argument( + '--bootstrap_servers', + dest='bootstrap_servers', + required=True, + help='Bootstrap servers for the Kafka cluster. Should be accessible by ' + 'the runner') + parser.add_argument( + '--topic', + dest='topic', + default='kafka_taxirides_realtime', + help='Kafka topic to write to and read from') + known_args, pipeline_args = parser.parse_known_args(argv) + + pipeline_options = PipelineOptions(pipeline_args) + pipeline_options.view_as(SetupOptions).save_main_session = True + pipeline_options.view_as(StandardOptions).streaming = True + + pipeline = beam.Pipeline(options=pipeline_options) + bootstrap_servers = known_args.bootstrap_servers + _ = ( + pipeline + | beam.io.ReadFromPubSub( + topic='projects/pubsub-public-data/topics/taxirides-realtime'). + with_output_types(bytes) + | beam.Map(lambda x: (b'', x)).with_output_types( Review comment: Why are we adding an empty bytes key? Is it required by another transform? A comment would be nice. ########## File path: sdks/python/apache_beam/examples/kafkataxi/kafka_taxi.py ########## @@ -0,0 +1,87 @@ +# +# 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. +# + +"""An example that writes to and reads from Kafka. + + This example reads from the PubSub NYC Taxi stream described in + https://github.com/googlecodelabs/cloud-dataflow-nyc-taxi-tycoon, writes to a + given Kafka topic and reads back from the same Kafka topic. + """ + +# pytype: skip-file + +from __future__ import absolute_import + +import argparse +import logging +import typing + +import apache_beam as beam +from apache_beam.io.kafka import ReadFromKafka +from apache_beam.io.kafka import WriteToKafka +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions + + +def run(argv=None): + """Main entry point; defines and runs the wordcount pipeline.""" + parser = argparse.ArgumentParser() + parser.add_argument( + '--bootstrap_servers', + dest='bootstrap_servers', + required=True, + help='Bootstrap servers for the Kafka cluster. Should be accessible by ' + 'the runner') + parser.add_argument( + '--topic', + dest='topic', + default='kafka_taxirides_realtime', + help='Kafka topic to write to and read from') + known_args, pipeline_args = parser.parse_known_args(argv) + + pipeline_options = PipelineOptions(pipeline_args) Review comment: Is there any reason not to use the shorter version? ```py pipeline_options = PipelineOptions( pipeline_args, save_main_session=True, streaming=True) ``` ########## File path: sdks/python/apache_beam/examples/kafkataxi/kafka_taxi.py ########## @@ -0,0 +1,87 @@ +# +# 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. +# + +"""An example that writes to and reads from Kafka. + + This example reads from the PubSub NYC Taxi stream described in + https://github.com/googlecodelabs/cloud-dataflow-nyc-taxi-tycoon, writes to a + given Kafka topic and reads back from the same Kafka topic. + """ + +# pytype: skip-file + +from __future__ import absolute_import + +import argparse +import logging +import typing + +import apache_beam as beam +from apache_beam.io.kafka import ReadFromKafka +from apache_beam.io.kafka import WriteToKafka +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions + + +def run(argv=None): + """Main entry point; defines and runs the wordcount pipeline.""" + parser = argparse.ArgumentParser() + parser.add_argument( + '--bootstrap_servers', + dest='bootstrap_servers', + required=True, + help='Bootstrap servers for the Kafka cluster. Should be accessible by ' + 'the runner') + parser.add_argument( + '--topic', + dest='topic', + default='kafka_taxirides_realtime', + help='Kafka topic to write to and read from') + known_args, pipeline_args = parser.parse_known_args(argv) + + pipeline_options = PipelineOptions(pipeline_args) + pipeline_options.view_as(SetupOptions).save_main_session = True + pipeline_options.view_as(StandardOptions).streaming = True + + pipeline = beam.Pipeline(options=pipeline_options) + bootstrap_servers = known_args.bootstrap_servers + _ = ( + pipeline + | beam.io.ReadFromPubSub( + topic='projects/pubsub-public-data/topics/taxirides-realtime'). + with_output_types(bytes) + | beam.Map(lambda x: (b'', x)).with_output_types( + typing.Tuple[bytes, bytes]) + | beam.WindowInto(beam.window.FixedWindows(15)) + | WriteToKafka( + producer_config={'bootstrap.servers': bootstrap_servers}, + topic=known_args.topic)) + + _ = ( + pipeline + | ReadFromKafka( + consumer_config={'bootstrap.servers': bootstrap_servers}, + topics=[known_args.topic]) + | beam.FlatMap(lambda x: [])) Review comment: Why are we getting rid of all the elements? By running this, do we see the elements via the Kafka transform or should we print the elements? ########## File path: sdks/python/apache_beam/examples/kafkataxi/kafka_taxi.py ########## @@ -0,0 +1,87 @@ +# +# 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. +# + +"""An example that writes to and reads from Kafka. + + This example reads from the PubSub NYC Taxi stream described in + https://github.com/googlecodelabs/cloud-dataflow-nyc-taxi-tycoon, writes to a + given Kafka topic and reads back from the same Kafka topic. + """ + +# pytype: skip-file + +from __future__ import absolute_import + +import argparse +import logging +import typing + +import apache_beam as beam +from apache_beam.io.kafka import ReadFromKafka +from apache_beam.io.kafka import WriteToKafka +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions + + +def run(argv=None): + """Main entry point; defines and runs the wordcount pipeline.""" + parser = argparse.ArgumentParser() + parser.add_argument( + '--bootstrap_servers', Review comment: I know this is how Kafka calls them, but reading this name is not very intuitive of what it means. Maybe rename to something like `--kafka_servers`? What do you think? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
