问题描述
我们正在尝试使用 Jdbc(Simba 驱动程序)将数据加载到 google bigquery。当试图在准备好的语句中插入一个空值时,我们得到了一个异常。这在它不是准备好的语句时有效。例如,代码(隐藏连接Url):
import java.sql.*;
public class Program {
public static void main(String[] args) {
Program program = new Program();
try {
program.doStatement();
} catch (Exception e) {
e.printstacktrace();
}
try {
program.doPreparedStatement();
} catch (Exception e) {
e.printstacktrace();
}
}
public Program() {}
public void doPreparedStatement() throws Exception {
try(Connection connection = getConnection()) {
PreparedStatement statement = connection.prepareStatement("INSERT INTO test1.my_table (CONTACT,COMPANY,ADDRESS,CITY,STATE,ZIP) VALUES (?,?,?)");
statement.setString(1,"MyContact");
statement.setString(2,"MyCompany");
statement.setString(3,"MyAddress");
statement.setString(4,"MyCity");
statement.setString(5,"MyState");
statement.setNull(6,Types.BIGINT);
System.err.println("Executing prepared statement...");
int count = statement.executeUpdate();
}
System.err.println(" done.");
}
public void doStatement() throws Exception {
try(Connection connection = getConnection()) {
String create_command =
"CREATE TABLE test1.my_table ( CONTACT STRING,COMPANY STRING,ADDRESS STRING,CITY STRING,STATE STRING,ZIP STRING )";
Statement createStatement = connection.createStatement( );
System.err.println("Create table.");
createStatement.execute(create_command);
System.err.println("Table created.");
System.err.println("Executing statement...");
Statement statement = connection.createStatement();
int count = statement.executeUpdate("INSERT INTO test1.my_table (CONTACT,ZIP) VALUES ('MyContact','MyCompany','MyAddress','MyCity','MyState',NULL)");
}
System.err.println(" done.");
}
private Connection getConnection() throws Exception
{
Connection connection = null;
connection = DriverManager.getConnection(CONNECTION_URL);
return connection;
}
}
产生以下输出:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (nop) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Create table.
Table created.
Executing statement...
done.
Executing prepared statement...
java.sql.sqlException: [Simba][BigQueryJDBCDriver](100032) Error executing query job. Message: Unparseable query parameter `` in type `TYPE_INT64`,Bad int64 value: null value: 'null'
at com.simba.googlebigquery.googlebigquery.client.BQClient.insertJob(UnkNown Source)
at com.simba.googlebigquery.googlebigquery.client.BQClient.executeQuery(UnkNown Source)
at com.simba.googlebigquery.googlebigquery.dataengine.BQAbstractExecutor.execute(UnkNown Source)
at com.simba.googlebigquery.googlebigquery.dataengine.BQsqlExecutor.execute(UnkNown Source)
at com.simba.googlebigquery.jdbc.common.SPreparedStatement.executeWithParams(UnkNown Source)
at com.simba.googlebigquery.jdbc.common.SPreparedStatement.executeAnyUpdate(UnkNown Source)
at com.simba.googlebigquery.jdbc.common.SPreparedStatement.executeUpdate(UnkNown Source)
at Program.doPreparedStatement(Program.java:35)
Caused by: com.simba.googlebigquery.support.exceptions.GeneralException: [Simba][BigQueryJDBCDriver](100032) Error executing query job. Message: Unparseable query parameter `` in type `TYPE_INT64`,Bad int64 value: null value: 'null'
... 8 more
解决方法
我发现将 BigQuery 支持添加到 jOOQ(参见例如 #11841)时,对于数字类型,此方法有效:
BigDecimal
setString(6,null)
类型是唯一非原始的数字 JDBC 类型。对于其他数据类型,如果支持,请使用它们各自的 setter,或者 NULL
,如果 Simba 仍然不喜欢相应类型的 Boolean
值,包括例如:
Date
Time
BigDecimal
如果对任何转换问题有疑问(例如,因为 Simba 似乎将 STRING
绑定为 CAST(? AS INT64)
,目前,至少在 1.2.13.1016 版本中),请确保您强制转换绑定变量,例如:
msbuild MyProject.vcxproj -p:MultiProcessorCompilation=true;CL_MPCount=8 -p:Platform=x86 -p:Configuration=release -t:Rebuild