mixing_use_of_datastream_and_table.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.table import (DataTypes, TableDescriptor, Schema, StreamTableEnvironment)
  23. from pyflink.table.expressions import col
  24. from pyflink.table.udf import udf
  25. def mixing_use_of_datastream_and_table():
  26. # use StreamTableEnvironment instead of TableEnvironment when mixing use of table & datastream
  27. env = StreamExecutionEnvironment.get_execution_environment()
  28. t_env = StreamTableEnvironment.create(stream_execution_environment=env)
  29. # define the source
  30. t_env.create_temporary_table(
  31. 'source',
  32. TableDescriptor.for_connector('datagen')
  33. .schema(Schema.new_builder()
  34. .column('id', DataTypes.BIGINT())
  35. .column('data', DataTypes.STRING())
  36. .build())
  37. .option("number-of-rows", "10")
  38. .build())
  39. # define the sink
  40. t_env.create_temporary_table(
  41. 'sink',
  42. TableDescriptor.for_connector('print')
  43. .schema(Schema.new_builder()
  44. .column('a', DataTypes.BIGINT())
  45. .build())
  46. .build())
  47. @udf(result_type=DataTypes.BIGINT())
  48. def length(data):
  49. return len(data)
  50. # perform table api operations
  51. table = t_env.from_path("source")
  52. table = table.select(col('id'), length(col('data')))
  53. # convert table to datastream and perform datastream api operations
  54. ds = t_env.to_data_stream(table)
  55. ds = ds.map(lambda i: i[0] + i[1], output_type=Types.LONG())
  56. # convert datastream to table and perform table api operations as you want
  57. table = t_env.from_data_stream(
  58. ds,
  59. Schema.new_builder().column("f0", DataTypes.BIGINT()).build())
  60. # execute
  61. table.execute_insert('sink') \
  62. .wait()
  63. # remove .wait if submitting to a remote cluster, refer to
  64. # https://nightlies.apache.org/flink/flink-docs-stable/docs/dev/python/faq/#wait-for-jobs-to-finish-when-executing-jobs-in-mini-cluster
  65. # for more details
  66. if __name__ == '__main__':
  67. logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
  68. mixing_use_of_datastream_and_table()