1

I am trying to implement a streaming application with a source connection of JDBC postgresql db. As a beginning, I had tried a basic query, however I could not execute it due to casting exception.

Here is the exception

Caused by: org.apache.flink.streaming.runtime.tasks.ExceptionInChainedOperatorException: Could not forward element to next operator
at org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.pushToOperator(OperatorChain.java:651)
at org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect(OperatorChain.java:612)
at org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect(OperatorChain.java:592)
at org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect(AbstractStreamOperator.java:727)
at org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect(AbstractStreamOperator.java:705)
at org.apache.flink.streaming.api.operators.StreamSourceContexts$ManualWatermarkContext.processAndCollect(StreamSourceContexts.java:305)
at org.apache.flink.streaming.api.operators.StreamSourceContexts$WatermarkContext.collect(StreamSourceContexts.java:394)
at org.apache.flink.streaming.api.functions.source.InputFormatSourceFunction.run(InputFormatSourceFunction.java:93)
at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:100)
at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:63)
at org.apache.flink.streaming.runtime.tasks.SourceStreamTask$LegacySourceFunctionThread.run(SourceStreamTask.java:202)
Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.time.LocalDate
at org.apache.flink.api.common.typeutils.base.LocalDateSerializer.copy(LocalDateSerializer.java:30)
at org.apache.flink.api.java.typeutils.runtime.RowSerializer.copy(RowSerializer.java:93)
at org.apache.flink.api.java.typeutils.runtime.RowSerializer.copy(RowSerializer.java:44)
at org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.pushToOperator(OperatorChain.java:635)

Here is the code snippet

        StreamExecutionEnvironment streamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
        streamExecutionEnvironment.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(streamExecutionEnvironment);

        JDBCOptions jdbcOptions = JDBCOptions.builder()
                .setDriverName("org.postgresql.Driver")
                .setDBUrl("jdbc:postgresql://192.168.4.69:5432/sessiondb")
                .setUsername("postgres")
                .setPassword("postgres")
                .setTableName("sessions")
                .build();

        TableSchema tableSchema = TableSchema.builder()
                .field("id", DataTypes.STRING())
                .field("ip", DataTypes.STRING())
                .field("port", DataTypes.INT())
                .field("alias", DataTypes.STRING())
                .field("connect_time", DataTypes.DATE())
                .field("disconnect_time", DataTypes.DATE())
                .build();

        JDBCTableSource jdbcTableSource = JDBCTableSource.builder().setOptions(jdbcOptions).setSchema(tableSchema).build();
        tableEnvironment.registerTableSource("sessions", jdbcTableSource);
        Table table = tableEnvironment.sqlQuery("SELECT * FROM sessions");
        tableEnvironment.toAppendStream(table, Row.class).print();
        streamExecutionEnvironment.execute();

When I change DataType to TIMESTAMP_WITH_TIME_ZONE (this is originally the true DataType on postgresql), I get this error which does not let me run the app at all

org.apache.flink.table.api.TableException: Unsupported conversion from data type
'TIMESTAMP(6) WITH TIME ZONE' (conversion class: java.time.OffsetDateTime) to type information. 
Only data types that originated from type information fully support a reverse conversion.

Any ideas how to deal with this problem? (Apache Flink API 1.9.0)

misterbaykal
  • 522
  • 5
  • 15

1 Answers1

0

First of all, the exception around the data type TIMESTAMP WITH TIME ZONE is because both planners currently do not support this data type. I recommend to check the compatibility table.

Did you try to use DataTypes.TIMESTAMP(3)?

twalthr
  • 2,584
  • 16
  • 15