flinksql获取系统当前时间搓_如何使用Flink SQL按事件时间对流进行排序

I have an out-of-order DataStream that I want to sort so that the events are ordered by their event time timestamps. I've simplified my use case down to where my Event class has just a single field -- the timestamp field:

public static void main(String[] args) throws Exception {

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);

env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

env.setParallelism(1);

DataStream eventStream = env.addSource(new OutOfOrderEventSource())

.assignTimestampsAndWatermarks(new TimestampsAndWatermarks());

Table events = tableEnv.fromDataStream(eventStream, "timestamp.rowtime");

tableEnv.registerTable("events", events);

Table sorted = tableEnv.sqlQuery("SELECT timestamp FROM events ORDER BY eventTime ASC");

DataStream sortedEventStream = tableEnv.toAppendStream(sorted, Row.class);

sortedEventStream.print();

env.execute();

}

I'm getting this error:

Exception in thread "main"

org.apache.flink.table.api.SqlParserException: SQL parse failed.

Encountered "timestamp FROM" at line 1, column 8.

Seems like I'm not specifying the event time attribute in the correct way, but it's not clear what's wrong.

解决方案

The problem turned out to be using timestamp as a field name in my Event class. Changing it to eventTime was enough to get everything working:

public class Sort {

public static final int OUT_OF_ORDERNESS = 1000;

public static void main(String[] args) throws Exception {

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);

env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

env.setParallelism(1);

DataStream eventStream = env.addSource(new OutOfOrderEventSource())

.assignTimestampsAndWatermarks(new TimestampsAndWatermarks());

Table events = tableEnv.fromDataStream(eventStream, "eventTime.rowtime");

tableEnv.registerTable("events", events);

Table sorted = tableEnv.sqlQuery("SELECT eventTime FROM events ORDER BY eventTime ASC");

DataStream sortedEventStream = tableEnv.toAppendStream(sorted, Row.class);

sortedEventStream.print();

env.execute();

}

public static class Event {

public Long eventTime;

Event() {

this.eventTime = Instant.now().toEpochMilli() + (new Random().nextInt(OUT_OF_ORDERNESS));

}

}

private static class OutOfOrderEventSource implements SourceFunction {

private volatile boolean running = true;

@Override

public void run(SourceContext ctx) throws Exception {

while(running) {

ctx.collect(new Event());

Thread.sleep(1);

}

}

@Override

public void cancel() {

running = false;

}

}

private static class TimestampsAndWatermarks extends BoundedOutOfOrdernessTimestampExtractor {

public TimestampsAndWatermarks() {

super(Time.milliseconds(OUT_OF_ORDERNESS));

}

@Override

public long extractTimestamp(Event event) {

return event.eventTime;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值