Hey everyone, I am writing a flink (v1.17) sql usi...
# troubleshooting
a
Hey everyone, I am writing a flink (v1.17) sql using kafka connectors running on flink k8s operator It essentially contains 4 queries. • The first query contains a table according to the event I am consuming from kafka. And I am watermarking on a timestamp field
event_time
. • The second query creates time aggregating HOP windows on
event_time
every minute aggregating data for last 60 mins. The problem comes on at the next step. • The third query uses the
window_end
time from the 2nd query to create another HOP window, this is where an error is thrown.
Copy code
Exception in thread "main" org.apache.flink.table.api.TableException: Window aggregate can only be defined over a time attribute column, but TIMESTAMP(3) encountered.
	at org.apache.flink.table.planner.plan.rules.logical.StreamLogicalWindowAggregateRule.getInAggregateGroupExpression(StreamLogicalWindowAggregateRule.scala:49)
	at org.apache.flink.table.planner.plan.rules.logical.LogicalWindowAggregateRuleBase.onMatch(LogicalWindowAggregateRuleBase.scala:79)
Can anyone help me with this? I have also tried using a single nested query for this purpose and still it throws the same error. I understand that the
window_end
field in the third query isn't a time attribute, but how do I achieve that in my case? Also is this a good practice? Sample queries: query 1:
Copy code
CREATE TABLE events (
`col1` STRING,
`EventTime` STRING, 
`event_time` as to_timestamp(`EventTime`, 'yyyy-MM-dd''T''HH:mm:ss''Z'''),
WATERMARK FOR `event_time` as `event_time` - INTERVAL '60' SECOND   ) WITH (
 'connector' = 'kafka',
 'topic' = 'sample_topic',
 'properties.bootstrap.servers' = 'brokers',
 'properties.group.id' = 'cg')
query 2:
Copy code
select col1, 
HOP_END(event_time, INTERVAL '1' MINUTE, INTERVAL '60' MINUTE) AS window_end,
get_count(concat(col1, "suffix")) AS items
FROM events
GROUP BY HOP_END(event_time, INTERVAL '1' MINUTE, INTERVAL '60' MINUTE),
col1
And register a temporary table on this ☝️ named table1.
get_count
is a simle udf to dedup number of events. query3:
Copy code
select col1, 
max(items)
HOP_START(window_end, INTERVAL '1' MINUTE, INTERVAL '15' MINUTE) as start_time,
HOP_END(window_end, INTERVAL '1' MINUTE, INTERVAL '15' MINUTE) as start_end
FROM table1
GROUP BY HOP_END(window_end, INTERVAL '1' MINUTE, INTERVAL '15' MINUTE),
col1
and the last query is a simple sink query. Can anyone help me with this??
t
I guess your windows are supposed to be in event time. Your watermark is defined on
event_time
which is therefore the event time of the row. The second window needs to be defined on
event_time
, too. I would suggest including it in in the select of query 2. Doing this, you should be able to rewrite query 3 to
HOP_START(event_time, INTERVAL '1' MINUTE, INTERVAL '15' MINUTE)
. The column
event_time
of query 2 should contain the last event timestamp that is possible in the window, i.e.,
window_end - 1
.
a
Hey, but then I will have to group by the second query with
event_time
which I don't want to do because its a time aggregation query. Apologies, I missed the fact that both query 2 and query 3 are also grouped by on the time window and rest of the columns.
t
Could you update your code snippets to show the grouping?
a
done @Tilman Krokotsch
bumping this up. Can anyone help me regarding this?
t
I had another look at the documentation. Have you considered using
HOP
directly instead of
HOP_END
and
HOP_START
?
HOP
should give you an additional column
window_time
that serves as the event time of the new windows. Using
window_time
in the last query should enable you to window again.
a
@Tilman Krokotsch HOP function can only be used in GROUP BY clauses.
Copy code
Exception in thread "main" org.apache.flink.table.api.ValidationException: SQL validation failed. From line 5, column 8 to line 5, column 65: Group function '$HOP' can only appear in GROUP BY clause
	at org.apache.flink.table.planner.calcite.FlinkPlannerImpl.org$apache$flink$table$planner$calcite$FlinkPlannerImpl$$validate(FlinkPlannerImpl.scala:187)
	at org.apache.flink.table.planner.calcite.FlinkPlannerImpl.validate(FlinkPlannerImpl.scala:113)
t
Sorry, I'm out of ideas here 🫤