process_json_data_with_udf.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 json
  19. import logging
  20. import sys
  21. from pyflink.table import (EnvironmentSettings, TableEnvironment, DataTypes, TableDescriptor,
  22. Schema)
  23. from pyflink.table.expressions import col
  24. from pyflink.table.udf import udf
  25. def process_json_data_with_udf():
  26. t_env = TableEnvironment.create(EnvironmentSettings.in_streaming_mode())
  27. # define the source
  28. table = t_env.from_elements(
  29. elements=[
  30. (1, '{"name": "Flink", "tel": 123, "addr": {"country": "Germany", "city": "Berlin"}}'),
  31. (2, '{"name": "hello", "tel": 135, "addr": {"country": "China", "city": "Shanghai"}}'),
  32. (3, '{"name": "world", "tel": 124, "addr": {"country": "USA", "city": "NewYork"}}'),
  33. (4, '{"name": "PyFlink", "tel": 32, "addr": {"country": "China", "city": "Hangzhou"}}')
  34. ],
  35. schema=['id', 'data'])
  36. # define the sink
  37. t_env.create_temporary_table(
  38. 'sink',
  39. TableDescriptor.for_connector('print')
  40. .schema(Schema.new_builder()
  41. .column('id', DataTypes.BIGINT())
  42. .column('data', DataTypes.STRING())
  43. .build())
  44. .build())
  45. # update json columns
  46. @udf(result_type=DataTypes.STRING())
  47. def update_tel(data):
  48. json_data = json.loads(data)
  49. json_data['tel'] += 1
  50. return json.dumps(json_data)
  51. table = table.select(col('id'), update_tel(col('data')))
  52. # execute
  53. table.execute_insert('sink') \
  54. .wait()
  55. # remove .wait if submitting to a remote cluster, refer to
  56. # https://nightlies.apache.org/flink/flink-docs-stable/docs/dev/python/faq/#wait-for-jobs-to-finish-when-executing-jobs-in-mini-cluster
  57. # for more details
  58. if __name__ == '__main__':
  59. logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
  60. process_json_data_with_udf()