我正在尝试对 PreparedStatement 执行雪花批量更新,但没有成功。我能够执行每个单独的查询(preparedStatement.executeQuery()),以便查询工作。但是一旦我切换到batchUpdate(),我就会得到一个似乎没有设置我的变量的异常(在insertEventStatement.executeBatch() 中抛出)。
net.snowflake.client.jdbc.SnowflakeSQLException: SQL compilation error: error line 1 at position 134
Bind variable ? not set.
at net.snowflake.client.jdbc.SnowflakeUtil.checkErrorAndThrowException(SnowflakeUtil.java:88)
at net.snowflake.client.core.StmtUtil.execute(StmtUtil.java:384)
at net.snowflake.client.core.SFStatement.executeHelper(SFStatement.java:421)
at net.snowflake.client.core.SFStatement.executeQueryInternal(SFStatement.java:240)
at net.snowflake.client.core.SFStatement.executeQuery(SFStatement.java:180)
at net.snowflake.client.core.SFStatement.executeQuery(SFStatement.java:152)
at net.snowflake.client.core.SFStatement.execute(SFStatement.java:637)
at net.snowflake.client.jdbc.SnowflakeStatementV1.executeUpdateInternal(SnowflakeStatementV1.java:135)
at net.snowflake.client.jdbc.SnowflakePreparedStatementV1.executeBatch(SnowflakePreparedStatementV1.java:1045)
有人看到我可能会错过什么吗?
public void insertBotEvents(List<SnowflakeRecord> snowflakeRecords) throws SQLException
{
try
{
PreparedStatement insertEventStatement = connection.prepareStatement(INSERT_EVENT);
PreparedStatement insertMetaEventStatement = connection.prepareStatement(INSERT_META_EVENT);
for (SnowflakeRecord snowflakeRecord : snowflakeRecords)
{
insertEventStatement.setString(1, snowflakeRecord.getUserId());
insertEventStatement.setString(2, snowflakeRecord.getSessionId());
insertEventStatement.setString(3, snowflakeRecord.getEventTime().toString());
insertEventStatement.setString(4, snowflakeRecord.getEventType());
insertEventStatement.setString(5, snowflakeRecord.getEventValue());
insertEventStatement.setString(6, snowflakeRecord.getAdditionalInfo());
insertEventStatement.setString(7, snowflakeRecord.getAffiliateName());
System.out.println("\nINSERTING NEW RECORD: \n" + snowflakeRecord);
insertEventStatement.addBatch();
if (snowflakeRecord.getSmVuiEventDto() != null && snowflakeRecord.getSmVuiEventDto().getParameters() != null)
{
for (ParametersDto parametersDto : snowflakeRecord.getSmVuiEventDto().getParameters())
{
insertMetaEventStatement.setString(1, snowflakeRecord.getSmVuiEventDto().getName());
insertMetaEventStatement.setString(2, parametersDto.getName());
insertMetaEventStatement.setString(3, parametersDto.getValue());
insertMetaEventStatement.setString(4, snowflakeRecord.getUserId());
insertMetaEventStatement.setString(5, snowflakeRecord.getSessionId());
insertMetaEventStatement.setString(6, snowflakeRecord.getEventTime().toString());
insertMetaEventStatement.setString(7, snowflakeRecord.getEventType());
insertMetaEventStatement.setString(8, snowflakeRecord.getEventValue());
insertMetaEventStatement.setString(9, snowflakeRecord.getAdditionalInfo());
insertMetaEventStatement.addBatch();
}
}
}
insertEventStatement.executeBatch();
insertMetaEventStatement.executeBatch();
System.out.println("\nCommitting changes to snowflake.");
connection.commit();
}
catch (Exception e)
{
connection.rollback();
System.err.println("Unable to execute SQL. Rolling back snowflake import");
e.printStackTrace();
throw e;
}
}
以下是查询:
private static final String INSERT_EVENT =
"INSERT INTO EVENT " +
"(USER_ID, " +
" SESSION_ID, " +
" EVENT_DATETIME, " +
" EVENT_TYPE, " +
" EVENT_VALUE, " +
" ADDITIONAL_INFO, " +
" AFFILIATE_ID " +
") " +
"SELECT " +
" ?, " +
" ?, " +
" ?, " +
" ?, " +
" ?, " +
" ?, " +
" AFFILIATE_ID " +
"FROM AFFILIATE_LOOKUP " +
"WHERE AFFILIATE_NAME = ?";
private static final String INSERT_META_EVENT =
"INSERT INTO EVENT_META " +
"(EVENT_ID, " +
" META_TYPE, " +
" META_NAME, " +
" META_VALUE " +
") " +
"SELECT EVENT_ID, ?, ?, ? " +
"FROM SM_VUI_EVENT " +
"WHERE USER_ID=? AND " +
" SESSION_ID=? AND " +
" EVENT_DATETIME=? AND " +
" EVENT_TYPE=? AND " +
" EVENT_VALUE=? AND " +
" ADDITIONAL_INFO=?";
编辑:显然,它在我使用的 JDBC 版本(3.0.18)中不可用,但在最近的版本(3.0.21)中添加。如果它不起作用,我会进一步更新。