sliding_window.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.time import Instant
  21. from pyflink.common import Types
  22. from pyflink.datastream import StreamExecutionEnvironment
  23. from pyflink.table import (DataTypes, TableDescriptor, Schema, StreamTableEnvironment)
  24. from pyflink.table.expressions import lit, col
  25. from pyflink.table.window import Slide
  26. def sliding_window_demo():
  27. env = StreamExecutionEnvironment.get_execution_environment()
  28. env.set_parallelism(1)
  29. t_env = StreamTableEnvironment.create(stream_execution_environment=env)
  30. # define the source with watermark definition
  31. ds = env.from_collection(
  32. collection=[
  33. (Instant.of_epoch_milli(1000), 'Alice', 110.1),
  34. (Instant.of_epoch_milli(4000), 'Bob', 30.2),
  35. (Instant.of_epoch_milli(3000), 'Alice', 20.0),
  36. (Instant.of_epoch_milli(2000), 'Bob', 53.1),
  37. (Instant.of_epoch_milli(5000), 'Alice', 13.1),
  38. (Instant.of_epoch_milli(3000), 'Bob', 3.1),
  39. (Instant.of_epoch_milli(7000), 'Bob', 16.1),
  40. (Instant.of_epoch_milli(10000), 'Alice', 20.1)
  41. ],
  42. type_info=Types.ROW([Types.INSTANT(), Types.STRING(), Types.FLOAT()]))
  43. table = t_env.from_data_stream(
  44. ds,
  45. Schema.new_builder()
  46. .column_by_expression("ts", "CAST(f0 AS TIMESTAMP(3))")
  47. .column("f1", DataTypes.STRING())
  48. .column("f2", DataTypes.FLOAT())
  49. .watermark("ts", "ts - INTERVAL '3' SECOND")
  50. .build()
  51. ).alias("ts", "name", "price")
  52. # define the sink
  53. t_env.create_temporary_table(
  54. 'sink',
  55. TableDescriptor.for_connector('print')
  56. .schema(Schema.new_builder()
  57. .column('name', DataTypes.STRING())
  58. .column('total_price', DataTypes.FLOAT())
  59. .column('w_start', DataTypes.TIMESTAMP_LTZ())
  60. .column('w_end', DataTypes.TIMESTAMP_LTZ())
  61. .build())
  62. .build())
  63. # define the sliding window operation
  64. table = table.window(Slide.over(lit(5).seconds).every(lit(2).seconds).on(col("ts")).alias("w"))\
  65. .group_by(col('name'), col('w')) \
  66. .select(col('name'), col('price').sum, col("w").start, col("w").end)
  67. # submit for execution
  68. table.execute_insert('sink') \
  69. .wait()
  70. # remove .wait if submitting to a remote cluster, refer to
  71. # https://nightlies.apache.org/flink/flink-docs-stable/docs/dev/python/faq/#wait-for-jobs-to-finish-when-executing-jobs-in-mini-cluster
  72. # for more details
  73. if __name__ == '__main__':
  74. logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
  75. sliding_window_demo()