Reputation: 2360
I am trying to do some aggregation over a window in PyFlink
. However I get A group window expects a time attribute for grouping in a stream environment.
error for trying it. I have a time attribute both in the window definition and in the select.
What am I doing wrong?
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
Upvotes: 1
Views: 1093
Reputation: 13301
I am not an expert, but I made a working example from your code.
I have to use datastream api and format datetime and define watermark.
#!/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()
Upvotes: 1
Reputation: 11
I think issue with the timestamp format or try with TIMESTAMP_LTZ
column (accepts epoch time i.e 1618989564564
).
From the documentation, it says:
Flink supports defining event time attribute on TIMESTAMP column and TIMESTAMP_LTZ column. If the timestamp data in the source is represented as year-month-day-hour-minute-second, usually a string value without time-zone information, e.g. 2020-04-15 20:13:40.564, it’s recommended to define the event time attribute as a TIMESTAMP column"
Upvotes: 0