使用 JOOQ 将 NaN 值插入到 postgresql 数据库中
Inserting NaN values into postgresql database with JOOQ
如何使用 JOOQ 将 Double.NaN 插入到 PostgreSQL 'double precision' 字段中?
我正在使用 PostgreSQL 9.5 和 JOOQ 3.7.3。我正在将传感器值批量插入 table。有时值是 NaN,当 JOOQ 尝试插入它时,它会抛出异常。
org.springframework.jdbc.BadSqlGrammarException: jOOQ; bad SQL grammar [];
nested exception is java.sql.BatchUpdateException: Batch entry 6
insert into "sensordata" ("measure_time", "location_id", "sensor_id", "value")
values (1462551000, 12, 15, NaN)
on conflict ("measure_time", "location_id", "sensor_id")
do update set "value" = NaN
was aborted.
Call getNextException to see the cause.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:99) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
值字段在 SQL 中定义如下:"value" DOUBLE PRECISION NOT NULL DEFAULT 0.0
我的 Java 代码的相关部分:
final List<InsertOnDuplicateSetMoreStep<SensordataRecord>> inserts =
seq(list)
.map(this::process)
.flatMap(t -> t)
.map(d ->
sql.insertInto(SENSORDATA, SENSORDATA.MEASURE_TIME, SENSORDATA.LOCATION_ID, SENSORDATA.SENSOR_ID, SENSORDATA.VALUE)
.values(d.getMeasureTime(), d.getLocationId(), d.getSensorId(), d.getValue())
.onDuplicateKeyUpdate()
.set(SENSORDATA.VALUE, d.getValue())
)
.toList();
sql.transaction(cfg -> DSL.using(cfg).batch(inserts).execute());
这是 jOOQ 中的错误:https://github.com/jOOQ/jOOQ/issues/5249
jOOQ 应该显式地将 NaN
转换为 double precision
,如下所示:'NaN'::double precision
。有两种可能的解决方法:
- 通过实施明确的
NaN
检查 实施将自己投射到客户端
- 通过使用此处记录的单语句(多绑定变量)批处理来避免对多个语句进行批处理:http://www.jooq.org/doc/latest/manual/sql-execution/batch-execution
- 使用 jOOQ 的加载器 API 进行数据插入,如下所述:http://www.jooq.org/doc/latest/manual/sql-execution/importing
在这种特殊情况下,后者可能更可取,因为您可以微调批量、批量和提交大小,从而获得更好的性能。
如何使用 JOOQ 将 Double.NaN 插入到 PostgreSQL 'double precision' 字段中?
我正在使用 PostgreSQL 9.5 和 JOOQ 3.7.3。我正在将传感器值批量插入 table。有时值是 NaN,当 JOOQ 尝试插入它时,它会抛出异常。
org.springframework.jdbc.BadSqlGrammarException: jOOQ; bad SQL grammar [];
nested exception is java.sql.BatchUpdateException: Batch entry 6
insert into "sensordata" ("measure_time", "location_id", "sensor_id", "value")
values (1462551000, 12, 15, NaN)
on conflict ("measure_time", "location_id", "sensor_id")
do update set "value" = NaN
was aborted.
Call getNextException to see the cause.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:99) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
值字段在 SQL 中定义如下:"value" DOUBLE PRECISION NOT NULL DEFAULT 0.0
我的 Java 代码的相关部分:
final List<InsertOnDuplicateSetMoreStep<SensordataRecord>> inserts =
seq(list)
.map(this::process)
.flatMap(t -> t)
.map(d ->
sql.insertInto(SENSORDATA, SENSORDATA.MEASURE_TIME, SENSORDATA.LOCATION_ID, SENSORDATA.SENSOR_ID, SENSORDATA.VALUE)
.values(d.getMeasureTime(), d.getLocationId(), d.getSensorId(), d.getValue())
.onDuplicateKeyUpdate()
.set(SENSORDATA.VALUE, d.getValue())
)
.toList();
sql.transaction(cfg -> DSL.using(cfg).batch(inserts).execute());
这是 jOOQ 中的错误:https://github.com/jOOQ/jOOQ/issues/5249
jOOQ 应该显式地将 NaN
转换为 double precision
,如下所示:'NaN'::double precision
。有两种可能的解决方法:
- 通过实施明确的
NaN
检查 实施将自己投射到客户端
- 通过使用此处记录的单语句(多绑定变量)批处理来避免对多个语句进行批处理:http://www.jooq.org/doc/latest/manual/sql-execution/batch-execution
- 使用 jOOQ 的加载器 API 进行数据插入,如下所述:http://www.jooq.org/doc/latest/manual/sql-execution/importing
在这种特殊情况下,后者可能更可取,因为您可以微调批量、批量和提交大小,从而获得更好的性能。