首页 > 解决方案 > Pyflink 表 API 流组窗口

问题描述

我正在尝试在PyFlink. 但是我A group window expects a time attribute for grouping in a stream environment.尝试它时出错。我在窗口定义和选择中都有一个时间属性。

我究竟做错了什么?

from pyflink.common import Row
from pyflink.table import EnvironmentSettings, TableEnvironment, DataTypes
from pyflink.table.expressions import col 
from pyflink.table.window import Tumble

from datetime import datetime, date, time

    
# create a blink streaming TableEnvironment
env_settings = EnvironmentSettings.new_instance().in_streaming_mode().use_blink_planner().build()
table_env = TableEnvironment.create(env_settings)

schema = DataTypes.ROW([
    DataTypes.FIELD("rowtime", DataTypes.TIMESTAMP(3)), 
    DataTypes.FIELD("b", DataTypes.STRING()), 
    DataTypes.FIELD("c", DataTypes.STRING()), 
    ])
    

if __name__ == "__main__":

    t = table_env.from_elements([(datetime(1970, 1, 1, 0, 0, 0), 'Hi', 'Hello'),
                                 (datetime(1970, 1, 1, 1, 0, 0), 'Hi', 'hi'),
                                 (datetime(1970, 1, 1, 2, 0, 0), 'Hi2', 'hi'),
                                 (datetime(1970, 1, 1, 3, 0, 0), 'Hi', 'Hello'),
                                 (datetime(1970, 1, 1, 4, 0, 0), 'Hi', 'Hello')], schema=schema)

    print(
            t.window(Tumble.over("1.hour").on("rowtime").alias("w")) \
              .group_by("w")\
              .select(col("w").start, col("w").end, col("w").rowtime)
    )

Traceback (most recent call last):
  File "test2.py", line 31, in <module>
    .select(col("w").start, col("w").end, col("w").rowtime)
  File "/home/user/venv/lib/python3.6/site-packages/pyflink/table/table.py", line 1290, in select
    return Table(self._j_table.select(to_expression_jarray(fields)), self._t_env)
  File "/home/user/venv/lib/python3.6/site-packages/py4j/java_gateway.py", line 1286, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "/home/user/venv/lib/python3.6/site-packages/pyflink/util/exceptions.py", line 147, in deco
    return f(*a, **kw)
  File "/home/user/venv/lib/python3.6/site-packages/py4j/protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o46.select.
: org.apache.flink.table.api.ValidationException: A group window expects a time attribute for grouping in a stream environment.
    at org.apache.flink.table.operations.utils.AggregateOperationFactory.validateStreamTimeAttribute(AggregateOperationFactory.java:329)
    at org.apache.flink.table.operations.utils.AggregateOperationFactory.validateTimeAttributeType(AggregateOperationFactory.java:309)
    at org.apache.flink.table.operations.utils.AggregateOperationFactory.getValidatedTimeAttribute(AggregateOperationFactory.java:302)
    at org.apache.flink.table.operations.utils.AggregateOperationFactory.createResolvedWindow(AggregateOperationFactory.java:267)
    at org.apache.flink.table.operations.utils.OperationTreeBuilder.windowAggregate(OperationTreeBuilder.java:257)
    at org.apache.flink.table.api.internal.TableImpl$WindowGroupedTableImpl.select(TableImpl.java:783)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.flink.api.python.shaded.py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at org.apache.flink.api.python.shaded.py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at org.apache.flink.api.python.shaded.py4j.Gateway.invoke(Gateway.java:282)
    at org.apache.flink.api.python.shaded.py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at org.apache.flink.api.python.shaded.py4j.commands.CallCommand.execute(CallCommand.java:79)
    at org.apache.flink.api.python.shaded.py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748

标签: pythonapache-flinkpyflink

解决方案


我认为时间戳格式有问题或尝试使用TIMESTAMP_LTZ列(接受纪元时间,即1618989564564)。

从文档中,它说:

Flink 支持在 TIMESTAMP 列和 TIMESTAMP_LTZ 列上定义事件时间属性。如果源中的时间戳数据表示为年-月-日-时-分-秒,通常是没有时区信息的字符串值,例如2020-04-15 20:13:40.564,建议定义事件时间属性作为 TIMESTAMP 列"


推荐阅读