basic_operations.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.common import Types
  22. from pyflink.datastream import StreamExecutionEnvironment
  23. def show(ds, env):
  24. ds.print()
  25. env.execute()
  26. def basic_operations():
  27. env = StreamExecutionEnvironment.get_execution_environment()
  28. env.set_parallelism(1)
  29. # define the source
  30. ds = env.from_collection(
  31. collection=[
  32. (1, '{"name": "Flink", "tel": 123, "addr": {"country": "Germany", "city": "Berlin"}}'),
  33. (2, '{"name": "hello", "tel": 135, "addr": {"country": "China", "city": "Shanghai"}}'),
  34. (3, '{"name": "world", "tel": 124, "addr": {"country": "USA", "city": "NewYork"}}'),
  35. (4, '{"name": "PyFlink", "tel": 32, "addr": {"country": "China", "city": "Hangzhou"}}')
  36. ],
  37. type_info=Types.ROW_NAMED(["id", "info"], [Types.INT(), Types.STRING()])
  38. )
  39. # map
  40. def update_tel(data):
  41. # parse the json
  42. json_data = json.loads(data.info)
  43. json_data['tel'] += 1
  44. return data.id, json.dumps(json_data)
  45. show(ds.map(update_tel), env)
  46. # (1, '{"name": "Flink", "tel": 124, "addr": {"country": "Germany", "city": "Berlin"}}')
  47. # (2, '{"name": "hello", "tel": 136, "addr": {"country": "China", "city": "Shanghai"}}')
  48. # (3, '{"name": "world", "tel": 125, "addr": {"country": "USA", "city": "NewYork"}}')
  49. # (4, '{"name": "PyFlink", "tel": 33, "addr": {"country": "China", "city": "Hangzhou"}}')
  50. # filter
  51. show(ds.filter(lambda data: data.id == 1).map(update_tel), env)
  52. # (1, '{"name": "Flink", "tel": 124, "addr": {"country": "Germany", "city": "Berlin"}}')
  53. # key by
  54. show(ds.map(lambda data: (json.loads(data.info)['addr']['country'],
  55. json.loads(data.info)['tel']))
  56. .key_by(lambda data: data[0]).sum(1), env)
  57. # ('Germany', 123)
  58. # ('China', 135)
  59. # ('USA', 124)
  60. # ('China', 167)
  61. if __name__ == '__main__':
  62. logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
  63. basic_operations()