pulsar.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ################################################################################
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. ################################################################################
  18. import logging
  19. import sys
  20. from pyflink.common import SimpleStringSchema, WatermarkStrategy
  21. from pyflink.datastream import StreamExecutionEnvironment
  22. from pyflink.datastream.connectors.pulsar import PulsarSource, PulsarSink, StartCursor, \
  23. StopCursor, DeliveryGuarantee, TopicRoutingMode
  24. if __name__ == '__main__':
  25. logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
  26. PULSAR_SQL_CONNECTOR_PATH = 'file:///path/to/flink-sql-connector-pulsar-1.16.0.jar'
  27. SERVICE_URL = 'pulsar://localhost:6650'
  28. ADMIN_URL = 'http://localhost:8080'
  29. env = StreamExecutionEnvironment.get_execution_environment()
  30. env.set_parallelism(1)
  31. env.add_jars(PULSAR_SQL_CONNECTOR_PATH)
  32. pulsar_source = PulsarSource.builder() \
  33. .set_service_url(SERVICE_URL) \
  34. .set_admin_url(ADMIN_URL) \
  35. .set_topics('ada') \
  36. .set_start_cursor(StartCursor.latest()) \
  37. .set_unbounded_stop_cursor(StopCursor.never()) \
  38. .set_subscription_name('pyflink_subscription') \
  39. .set_deserialization_schema(SimpleStringSchema()) \
  40. .set_config('pulsar.source.enableAutoAcknowledgeMessage', True) \
  41. .set_properties({'pulsar.source.autoCommitCursorInterval': '1000'}) \
  42. .build()
  43. ds = env.from_source(source=pulsar_source,
  44. watermark_strategy=WatermarkStrategy.for_monotonous_timestamps(),
  45. source_name="pulsar source")
  46. pulsar_sink = PulsarSink.builder() \
  47. .set_service_url(SERVICE_URL) \
  48. .set_admin_url(ADMIN_URL) \
  49. .set_producer_name('pyflink_producer') \
  50. .set_topics('beta') \
  51. .set_serialization_schema(SimpleStringSchema()) \
  52. .set_delivery_guarantee(DeliveryGuarantee.AT_LEAST_ONCE) \
  53. .set_topic_routing_mode(TopicRoutingMode.ROUND_ROBIN) \
  54. .set_config('pulsar.producer.maxPendingMessages', 1000) \
  55. .set_properties({'pulsar.producer.batchingMaxMessages': '100'}) \
  56. .build()
  57. ds.sink_to(pulsar_sink).name('pulsar sink')
  58. env.execute()