Hi, Could someone help? I have the following code...
# troubleshooting
a
Hi, Could someone help? I have the following code:
Copy code
RowTypeInfo rowTypeInfo1 = new RowTypeInfo(
                    new TypeInformation[]{
                            BasicTypeInfo.LONG_TYPE_INFO,   // userId
                            BasicTypeInfo.LONG_TYPE_INFO,
                            Types.LOCAL_DATE_TIME(),
                    }, // eventTime
                    new String[]{"id", "version", "eventTimeS"});

            // Example changelog stream: Tuple3<RowKind, String, Long>
            DataStream<Row> stream = env.fromData(
                            Row.ofKind(RowKind.INSERT, 1L, 1L, LocalDateTime.now(ZoneOffset.UTC)),
                            Row.ofKind(RowKind.INSERT, 2L, 2L, LocalDateTime.now(ZoneOffset.UTC)))
                    .returns(rowTypeInfo1);

            /* Define a table schema with a primary key */
            Table dataStream = tableEnv.fromDataStream(
                    stream,
                    Schema.newBuilder()
                            .column("id", DataTypes.BIGINT().notNull())
                            .column("version", DataTypes.BIGINT())
                            .column("eventTimeS", DataTypes.TIMESTAMP(3))
                            .primaryKey("id") // Define primary key
                            .watermark("eventTimeS", "eventTimeS - INTERVAL '5' SECONDS")
                            .build()
            );
            tableEnv.createTemporaryView("DStream", dataStream);
            
            DynamoDbStreamsSource<UserState> dynamoDbStreamsSource =
                    DynamoDbStreamsSource.<UserState>builder()
                            .setStreamArn("-")
                            .setSourceConfig(conf)
                            .setDeserializationSchema(new StaticStateDeserialization())
                            .build();

            RowTypeInfo rowTypeInfo = new RowTypeInfo(
                    new TypeInformation[]{
                            BasicTypeInfo.LONG_TYPE_INFO,   // userId
                            BasicTypeInfo.BOOLEAN_TYPE_INFO,// paid
                            Types.LOCAL_DATE_TIME()}, // eventTime
                    new String[]{"userId", "paid", "eventTime"});

            DataStream<Row> userStateDataStream = env.fromSource(dynamoDbStreamsSource,
                            WatermarkStrategy.<UserState>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                                    .withTimestampAssigner((user, time) -> LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC).toEpochMilli())
                                    .withIdleness(Duration.ofSeconds(3)),
                            "DynamoDB Streams source")
                    .returns(UserState.class)
                    .map(userState -> Row.ofKind(RowKind.INSERT, userState.getUserId(), userState.getPaid(),
                            LocalDateTime.now(ZoneOffset.UTC).minusMinutes(10)))
                    .returns(rowTypeInfo);

            Table userStateStream = tableEnv.fromDataStream(userStateDataStream, Schema.newBuilder()
                    .column("userId", DataTypes.BIGINT().notNull())
                    .column("paid", "BOOLEAN")
                    .column("eventTime", DataTypes.TIMESTAMP(3))
                    .primaryKey("userId")
                    .watermark("eventTime", "eventTime - INTERVAL '1' SECONDS") // Watermark generation
                    .build());

            tableEnv.createTemporaryView("userStateStream", userStateStream);

            Table result = tableEnv.sqlQuery(
                    "SELECT userId, paid, eventTime\n" +
                            "FROM (\n" +
                            "    SELECT *,\n" +
                            "           ROW_NUMBER() OVER (PARTITION BY userId ORDER BY eventTime DESC) AS rownum\n" +
                            "    FROM userStateStream\n" +
                            ")\n" +
                            "WHERE rownum = 1;"
            );
            
            tableEnv.createTemporaryView("userStateStatic", result);

            tableEnv.sqlQuery(
                    "SELECT id, version, eventTimeS, userId, eventTime, paid FROM DStream JOIN userStateStatic FOR SYSTEM_TIME AS OF eventTimeS ON id = userId"
            ).execute().print();
It returns nothing but if i remove
FOR SYSTEM_TIME AS OF eventTimeS
it starts producing message I need exactly join dstream with user state on the moment of time event Can someone tell what I am doing wrong ?
r
not 100% sure, but this looks wrong to me
Copy code
DataStream<Row> userStateDataStream = env.fromSource(dynamoDbStreamsSource,
                            WatermarkStrategy.<UserState>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                                    .withTimestampAssigner((user, time) -> LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC).toEpochMilli())
This is taking the current machine time, not event time as the watermark? Isn't there a field / value you could use from dynamodb? Otherwise if the event time < machine time - 1s: pretty much ALL events will be late arriving
a
thank you! My idea of the issue was that the loading of data from Dynamodb took time, so I got messages before Flink loaded data from Dynamodb. How I checked: When I replace datastream with an event from Kafka that I send after flink loaded data from dynamodb, everything works correctly
r
Glad you found it!
πŸ™ 1