QQ: What is the non-deprecated way to convert a da...
# troubleshooting
p
QQ: What is the non-deprecated way to convert a dataStream to a table, where the rowTime should come from the record eventtime? This is the deprecated way, for reference:
Copy code
tEnv.fromDataStream(fsFiles, $("location"), $("ts").rowtime())
I was not able to come up with the correct
Schema
definition for using the record eventtime for the rowtime. Thanks, Peter
t
Hey Peter, Great question! Transitioning from the deprecated methods to the newer ones can be a bit tricky, but I’ve got you covered. To convert a DataStream to a Table and specify a field as the event time (rowtime) attribute in the non-deprecated way, you’ll want to use the Schema builder provided in the Table API.
Here’s how you can do it: First, make sure your stream has timestamps and watermarks defined. Assuming fsFiles is your DataStream, and it has a field for timestamps, you’ll do something like this: import org.apache.flink.table.api.*; DataStream<MyEvent> fsFiles = // your data stream TableEnvironment tEnv = // your table environment Schema schema = Schema.newBuilder() .column("location", DataTypes.STRING()) .column("ts", DataTypes.TIMESTAMP(3)) .watermark("ts", "ts - INTERVAL '5' SECOND") // Adjust the watermark expression as needed .build(); Table table = tEnv.fromDataStream(fsFiles, schema); In this snippet, MyEvent is your event class, and it must have location and ts fields. The ts field is used for event time. The watermark definition here is an example; you’ll need to adjust it based on your event time characteristics and how you want to handle late events. This approach lets you precisely define how the rowtime and watermarks should be derived from your data stream, ensuring that the event time semantics are correctly captured in the resulting table.
p
Thanks @Tim Wolfe for the detailed answer! In the original example I have used the record timestamp as a
ts
, and I did not have the timestamp in the records itself. Also I wanted to generate the watermarks in my stream based on my specific requirements. So in a previous operator, I did something like this:
Copy code
output.collect(record, timestamp);
output.emitWatermark(new Watermark(watermark));
The original stream creation used the
timestamp
for the record timestamp, and did not generate a new watermark. Based on your suggestion, I could add the
timestamp
to the record, and use it as a
ts
column. Also, we could generate additional watermarks, which are not needed, but the solution could work. Thanks, Peter