kafka_csv_format.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 Types
  21. from pyflink.datastream import StreamExecutionEnvironment
  22. from pyflink.datastream.connectors.kafka import FlinkKafkaProducer, FlinkKafkaConsumer
  23. from pyflink.datastream.formats.csv import CsvRowSerializationSchema, CsvRowDeserializationSchema
  24. # Make sure that the Kafka cluster is started and the topic 'test_csv_topic' is
  25. # created before executing this job.
  26. def write_to_kafka(env):
  27. type_info = Types.ROW([Types.INT(), Types.STRING()])
  28. ds = env.from_collection([
  29. (1, 'hi'), (2, 'hello'), (3, 'hi'), (4, 'hello'), (5, 'hi'), (6, 'hello'), (6, 'hello')],
  30. type_info=type_info)
  31. serialization_schema = CsvRowSerializationSchema.Builder(type_info).build()
  32. kafka_producer = FlinkKafkaProducer(
  33. topic='test_csv_topic',
  34. serialization_schema=serialization_schema,
  35. producer_config={'bootstrap.servers': 'localhost:9092', 'group.id': 'test_group'}
  36. )
  37. # note that the output type of ds must be RowTypeInfo
  38. ds.add_sink(kafka_producer)
  39. env.execute()
  40. def read_from_kafka(env):
  41. type_info = Types.ROW([Types.INT(), Types.STRING()])
  42. deserialization_schema = CsvRowDeserializationSchema.Builder(type_info).build()
  43. kafka_consumer = FlinkKafkaConsumer(
  44. topics='test_csv_topic',
  45. deserialization_schema=deserialization_schema,
  46. properties={'bootstrap.servers': 'localhost:9092', 'group.id': 'test_group_1'}
  47. )
  48. kafka_consumer.set_start_from_earliest()
  49. env.add_source(kafka_consumer).print()
  50. env.execute()
  51. if __name__ == '__main__':
  52. logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
  53. env = StreamExecutionEnvironment.get_execution_environment()
  54. env.add_jars("file:///path/to/flink-sql-connector-kafka-1.15.0.jar")
  55. print("start writing data to kafka")
  56. write_to_kafka(env)
  57. print("start reading data from kafka")
  58. read_from_kafka(env)