1

我正在尝试在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
4

2 回答 2

0

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

从文档中,它说:

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

于 2021-12-16T19:44:45.390 回答
0

我不是专家,但我从您的代码中做了一个工作示例。

我必须使用数据流 api 并格式化日期时间并定义水印。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import datetime

from pyflink.common import Types
from pyflink.datastream import StreamExecutionEnvironment
from pyflink.table import (
    DataTypes,
    Schema,
    StreamTableEnvironment,
)
from pyflink.table.expressions import col
from pyflink.table.window import Tumble

env = StreamExecutionEnvironment.get_execution_environment()
t_env = StreamTableEnvironment.create(env)


def main():
    ds = env.from_collection(
        collection=[
            (datetime(1970, 1, 1, 0, 0, 0).isoformat().replace('T', ' '), 'Hi', 'Hello'),
            (datetime(1970, 1, 1, 1, 0, 0).isoformat().replace('T', ' '), 'Hi', 'hi'),
            (datetime(1970, 1, 1, 2, 0, 0).isoformat().replace('T', ' '), 'Hi2', 'hi'),
            (datetime(1970, 1, 1, 3, 0, 0).isoformat().replace('T', ' '), 'Hi', 'Hello'),
            (datetime(1970, 1, 1, 4, 0, 0).isoformat().replace('T', ' '), 'Hi', 'Hello'),
        ],
        type_info=Types.ROW([Types.STRING(), Types.STRING(), Types.STRING()]),
    )
    schema = Schema.new_builder() \
        .column_by_expression("rowtime", "CAST(f0 AS TIMESTAMP(3))") \
        .column("f1", DataTypes.STRING()) \
        .column("f2", DataTypes.STRING()) \
        .watermark("rowtime", "rowtime - INTERVAL '3' SECOND") \
        .build()
    the_table = t_env.from_data_stream(ds, schema)
    res = the_table.to_pandas()
    print(res)

    res = the_table \
        .window(Tumble.over("1.hour").on("rowtime").alias("w")) \
        .group_by("w") \
        .select(
            col("w").start.alias('start_time'),
            col("w").end.alias('end_time'),
            col("w").proctime.alias('proctime'),
        ) \
        .to_pandas()
    print(res)


if __name__ == '__main__':
    main()
于 2021-12-27T10:35:49.220 回答