flink 1.9.1异常:JDBCUpsertTableSink 使用Postgresql连接时报错

文章目录

  • 异常
  • 问题原因
  • 解决
  • 测试

异常

Caused by: java.sql.BatchUpdateException:
 Batch entry 0 INSERT INTO "action_log"("id", "cnt") VALUES ('1', 1) ON CONFLICT ("id" DO UPDATE SET "id"=EXCLUDED."id", "cnt"=EXCLUDED."cnt" 
 was aborted: 
 ERROR: syntax error at or near "DO"

使用JDBCUpsertTableSink写数据到Postgresql时报错,代码如下:

    val options = JDBCOptions.builder()
      .setDBUrl("jdbc:postgresql://192.168.10.10:5432/postgres")
      .setDriverName("org.postgresql.Driver")
      .setUsername("postgres")
      .setPassword("xxx")
      .setTableName("action_log")
      .build

    val tableSche: TableSchema = TableSchema.builder()
      .field("id", DataTypes.STRING().notNull())
      .field("cnt", DataTypes.BIGINT())
      .primaryKey("id").build()

    val sink = JDBCUpsertTableSink.builder()
      .setOptions(options)
      .setMaxRetryTimes(5)
      .setFlushMaxSize(1000)
      .setFlushIntervalMills(1000)
      .setTableSchema(tableSche)
      .build()

    tblEnv.registerTableSink("postgresOutputTable", sink)

问题原因

flink1.9.1在定义postgresql upsert时sql拼的有点问题,如下:

https://github.com/apache/flink/blob/release-1.9.1/flink-connectors/flink-jdbc/src/main/java/org/apache/flink/api/java/io/jdbc/dialect/JDBCDialects.java
flink 1.9.1异常:JDBCUpsertTableSink 使用Postgresql连接时报错_第1张图片

return Optional.of(getInsertIntoStatement(tableName, fieldNames) +
							" ON CONFLICT (" + uniqueColumns +
							" DO UPDATE SET " + updateClause

解决

修改为:

return Optional.of(getInsertIntoStatement(tableName, fieldNames) +
							" ON CONFLICT (" + uniqueColumns + ")" +
							" DO UPDATE SET " + updateClause

重新编译打包flink-jdbc模块:

mvn package -Dmaven.test.skip=true -Dcheckstyle.skip=true

flink 1.9.1异常:JDBCUpsertTableSink 使用Postgresql连接时报错_第2张图片

测试

flink 1.9.1异常:JDBCUpsertTableSink 使用Postgresql连接时报错_第3张图片
测试通过!

你可能感兴趣的:(Flink)