java.lang.IllegalArgumentException: SQL 数组不能为空

java.lang.IllegalArgumentException: SQL array must not be empty

我有下面的 DBImporter class,它工作正常并且还在数据库 table 中正确插入数据。我正在尝试从 .CSV 文件中获取数据并插入到 Oracle table.

到目前为止,我只处理了我的目录中的一个文件,并且工作正常。现在我想处理多个文件。因此,在 运行 第一个文件正确处理并插入数据期间,在第二个文件中它开始读取数据并抛出错误:

java.lang.IllegalArgumentException: SQL array must not be empty

下面是我的 DBimporter class。我认为错误是在此处某处的最终提交批处理期间发生的,但不确定

jdbcTemplate.batchUpdate(sqlBatch.toArray(新字符串[sqlBatch.size()]));

@Service
public class DBImporter {

    private final static Logger log = LoggerFactory.getLogger(DBImporter.class);
    private static final List<String> NULL_VALUES = Arrays.asList("", "N.A", "N.A", "UNKNOWN");
    private static final List<String> COL_HEADERS = Arrays.asList("ID", "NM", "TYE", "SA");
    private static final int BATCH_SIZE = 50;

    private boolean eof = false;
    private String tableName;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    @Transactional(rollbackFor = IOException.class)
    public void processFile(BufferedReader reader, String tableName) {
        this.tableName = tableName;
        List<String> sqlBatch = new ArrayList<String>(BATCH_SIZE);

        log.info("Starte auslesen der Daten");
        long t1 = System.currentTimeMillis();
        log.info("Start time: " + t1);

        jdbcTemplate.execute("DELETE FROM " + tableName);

        while (!eof) {
            try {
                Map<String, ColumnData> dbColumns = getDBColumns();

                // Get a list of db column data related to the column headers.
                List<ColumnData> columnData = COL_HEADERS.stream().map(dbColumns::get).collect(toList());

                // Get the next valid data row if its starts from "FRO" or "BO".
                List<String> dataRow = findNextLineStartingWith(reader, "R", "T");

                String query = createSql(columnData, dataRow);
                sqlBatch.add(query);

                // Process batch.
                if (sqlBatch.size() >= BATCH_SIZE) {
                    jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
                    sqlBatch.clear();
                }
            } catch (IllegalStateException e) {
                break;
            } catch (IOException e) {
                log.error(e.getLocalizedMessage());
            }
        }

        // Commit the final batch.
        jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
        sqlBatch.clear();

        long delta = System.currentTimeMillis() - t1;
        log.info("Total runtime : " + delta / 1000 + " seconds");
    }

    /**
     * Create a SQL insert query using the data row.
     * 
     * @param tableName  Name of the table.
     * @param columnData Column data list.
     * @param dataRow    Data row to be inserted.
     * @return Generated SQL query string.
     */
    private String createSql(List<ColumnData> columnData, List<String> dataRow) {
        List<String> values = new ArrayList<>(columnData.size());

        for (int i = 0; i < columnData.size(); i++) {
            if (NULL_VALUES.contains(dataRow.get(i))) {
                values.add("NULL");
            } else if (columnData.get(i).getType() >= Types.NUMERIC && columnData.get(i).getType() <= Types.DOUBLE) {
                values.add(dataRow.get(i));
            } else {
                values.add("'" + dataRow.get(i).replace("'", "''") + "'");
            }
        }

        return "INSERT INTO " + tableName + " (" +
        columnData.stream().filter(Objects::nonNull).map(ColumnData::getName).collect(joining(", ")) +
        ", SYSTEM_INSERTED_AT) VALUES (" +
        values.stream().collect(joining(", ")) +
        ", CURRENT_TIMESTAMP)";
    }

    /**
     * Find the next line starting with the given string and split it into columns.
     * 
     * @param reader   BufferedReader object to be used.
     * @param prefixes A list of prefixes to look for in the string.
     * @return List of data objects.
     * @throws IOException
     */
    private List<String> findNextLineStartingWith(BufferedReader reader, String... prefixes) throws IOException {
        while (true) {
            String line = readLineOrThrow(reader);
            for (String prefix : prefixes)
                if (line.startsWith(prefix)) {
                    ArrayList<String> data = new ArrayList<>();
                    // Split the line using the delimiter.
                    data.addAll(Arrays.asList(line.split(";")));

                    // Build the row to be inserted.
                    List<String> row = Arrays.asList(data.get(1), data.get(2).trim(), "", "");                      

                    return row;
                }
        }
    }

    /**
     * Read a single line in the file.
     * 
     * @param reader BufferedReader object to be used.
     * @return
     * @throws IOException
     */
    private String readLineOrThrow(BufferedReader reader) throws IOException {
        String line = reader.readLine();
        if (line == null) {
            this.eof = true;
            throw new IllegalStateException("Unexpected EOF");
        }

        return line.trim();
    }

    /**
     * Read database column metadata.
     * 
     * @param tableName Name of the table to process.
     * @return A map containing column information.
     */
    private Map<String, ColumnData> getDBColumns() {
        Map<String, ColumnData> result = new HashMap<>();
        try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {
            ResultSet rs = connection.getMetaData().getColumns(null, null, tableName, null);
            while (rs.next()) {
                String columnName = rs.getString(4).toUpperCase();
                int type = rs.getInt(5);
                result.put(columnName, new ColumnData(columnName, type));
            }
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

您的最后一批可能是空的。

如果您刚刚提交了 BATCH_SIZE 个条目并清除了 sqlBatch,这是可能的。如果此时您的 while 循环退出, 没有要提交的元素。

您需要通过添加尺寸检查来解决这个问题,例如:

// Commit the final batch (only if there is something left)
if (sqlBatch.isEmpty() == false) {
    jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
    sqlBatch.clear();
}

编辑:

正如 @Vasif 指出的那样,您需要在方法的不同调用之间重置 eof

一个简单的解决方案(尽管有点 hacky)是

boolean eof = false
while (!eof) {
    try {

    } catch (IllegalStateException e) {
        eof = true;
        break;
    } catch (IOException e) {
        log.error(e.getLocalizedMessage());
    }
}

一个合适的解决方案是重构您的代码,使其不依赖于抛出的这些异常。

一些提示:

  • 去掉readLineOrThrow.
  • 如果下一行是 null.
  • ,则删除 findNextLineStartingWith 中的 while(true) 并改为 return 一个空列表
  • 调整外部循环以适当地处理这个 return 值。
  • (注意:如果得到 IOException,您可能还需要打破循环)。

请尝试以下更改:

        // Commit the final batch.
if (sqlBatch.size() > 0){
   jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
   sqlBatch.clear();
}

    @Transactional(rollbackFor = IOException.class)
    public void processFile(BufferedReader reader, String tableName) {
        eof = false;
        ...

但是如果您想要一个更清晰和安全的解决方案,请按以下方式更改您的代码:

public class DBImporter {

    private final static Logger log = LoggerFactory.getLogger(DBImporter.class);
    private static final List<String> NULL_VALUES = Arrays.asList("", "N.A", "N.A", "UNKNOWN");
    private static final List<String> COL_HEADERS = Arrays.asList("USER_ID", "NAME", "TYPE", "SRC_DATA");
    private static final int BATCH_SIZE = 50;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = IOException.class)
    public void processFile(BufferedReader reader, String tableName) {
        AtomicBoolean eof = new AtomicBoolean(false);
        List<String> sqlBatch = new ArrayList<String>(BATCH_SIZE);

        log.info("Starte auslesen der Daten");
        long t1 = System.currentTimeMillis();
        log.info("Start time: " + t1);

        jdbcTemplate.execute("DELETE FROM " + tableName);

        while (!eof.get()) {
            try {
                Map<String, ColumnData> dbColumns = getDBColumns(tableName);

                // Get a list of db column data related to the column headers.
                List<ColumnData> columnData = COL_HEADERS.stream().map(dbColumns::get).collect(toList());

                // Get the next valid data row if its starts from "R" or "T".
                List<String> dataRow = findNextLineStartingWith(reader, eof, "R", "T");

                String query = createSql(tableName, columnData, dataRow);
                sqlBatch.add(query);

                // Process batch.
                if (sqlBatch.size() >= BATCH_SIZE) {
                    jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
                    sqlBatch.clear();
                }
            } catch (IllegalStateException e) {
                break;
            } catch (IOException e) {
                log.error(e.getLocalizedMessage());
            }
        }

        // Commit the final batch.
        jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
        sqlBatch.clear();

        long delta = System.currentTimeMillis() - t1;
        log.info("Total runtime : " + delta / 1000 + " seconds");
    }

    /**
     * Create a SQL insert query using the data row.
     *
     * @param tableName  Name of the table.
     * @param columnData Column data list.
     * @param dataRow    Data row to be inserted.
     * @return Generated SQL query string.
     */
    private String createSql(String tableName, List<ColumnData> columnData, List<String> dataRow) {
        List<String> values = new ArrayList<>(columnData.size());

        for (int i = 0; i < columnData.size(); i++) {
            if (NULL_VALUES.contains(dataRow.get(i))) {
                values.add("NULL");
            } else if (columnData.get(i).getType() >= Types.NUMERIC && columnData.get(i).getType() <= Types.DOUBLE) {
                values.add(dataRow.get(i));
            } else {
                values.add("'" + dataRow.get(i).replace("'", "''") + "'");
            }
        }

        return "INSERT INTO " + tableName + " (" +
                columnData.stream().filter(Objects::nonNull).map(ColumnData::getName).collect(joining(", ")) +
                ", SYSTEM_INSERTED_AT) VALUES (" +
                values.stream().collect(joining(", ")) +
                ", CURRENT_TIMESTAMP)";
    }

    /**
     * Find the next line starting with the given string and split it into columns.
     *
     * @param reader   BufferedReader object to be used.
     * @param prefixes A list of prefixes to look for in the string.
     * @return List of data objects.
     * @throws IOException
     */
    private List<String> findNextLineStartingWith(BufferedReader reader, AtomicBoolean eof, String... prefixes) throws IOException {
        while (true) {
            String line = readLineOrThrow(reader, eof);
            for (String prefix : prefixes)
                if (line.startsWith(prefix)) {
                    ArrayList<String> data = new ArrayList<>();
                    // Split the line using the delimiter.
                    data.addAll(Arrays.asList(line.split(";")));

                    // Build the row to be inserted.
                    List<String> row = Arrays.asList(data.get(1), data.get(2).trim(), "", "");

                    // Insert type depending on the prefix.
                    if (prefix.equals("R"))
                        row.set(2, "USER");
                    else if (prefix.equals("T"))
                        row.set(2, "PERM");

                    row.set(3, String.join(";", row.subList(0, 3)));

                    return row;
                }
        }
    }

    /**
     * Read a single line in the file.
     *
     * @param reader BufferedReader object to be used.
     * @return
     * @throws IOException
     */
    private String readLineOrThrow(BufferedReader reader, AtomicBoolean eof) throws IOException {
        String line = reader.readLine();
        if (line == null) {
            eof.set(true);
            throw new IllegalStateException("Unexpected EOF");
        }

        return line.trim();
    }

    /**
     * Read database column metadata.
     *
     * @param tableName Name of the table to process.
     * @return A map containing column information.
     */
    private Map<String, ColumnData> getDBColumns(String tableName) {
        Map<String, ColumnData> result = new HashMap<>();
        try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {
            ResultSet rs = connection.getMetaData().getColumns(null, null, tableName, null);
            while (rs.next()) {
                String columnName = rs.getString(4).toUpperCase();
                int type = rs.getInt(5);
                result.put(columnName, new ColumnData(columnName, type));
            }
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}