微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google BigQuery,在准备好的语句中为空值抛出异常

如何解决Google BigQuery,在准备好的语句中为空值抛出异常

我们正在尝试使用 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。