是否可以在HANA中批量插入?

时间:2017-01-16 13:23:42

标签: database sap hana

我想通过批量插入hana。目前我使用Java从结果集中逐行插入。有没有办法一次插入多行?有可能吗? (我不想只导入批量插入)我搜遍了所有并且找不到任何好的答案。感谢任何帮助?

1 个答案:

答案 0 :(得分:1)

对于JAVA / JDBC代码,存在所谓的批处理接口。 这是我用于测试的一个老例子:

myDBconn.setAutoCommit(false);

PreparedStatement insStmt = myDBconn
        .prepareStatement("INSERT INTO EFASHION.SHOP_FACTS_INS_DEMO VALUES"
                + " (?, ?, ?, ?, ?, ?, ?, ?  )");

for (int i = 1; i <= LOOPCNT; i++) {
    myfacts.createNewFact();  // create a JAVA object with new data

    // prepare the new data for the batch 
    // note that this is a typed assignment. 
    insStmt.setInt(1, i);
    insStmt.setInt(2, myfacts.article_id);
    insStmt.setInt(3, myfacts.color_code);
    insStmt.setInt(4, myfacts.week_id);
    insStmt.setInt(5, myfacts.shop_id);
    insStmt.setDouble(6, myfacts.margin);
    insStmt.setDouble(7, myfacts.amount_sold);
    insStmt.setInt(8, myfacts.quantity_sold);

    // add the new data to the batch
    insStmt.addBatch();

    // limit the batch size, to  prevent client side out of memory errors.
    // but DON'T commit yet!
    // Remember the data in the current batch is kept in client
    // memory as long as we don't send it to the HANA server
    if (i % BATCHSIZE == 0) {
        // executeBatch returns the number of affected rows.
        // if we want to use this in the application we just keep adding this up
        affectedRows += insStmt.executeBatch();
    }
}
// the final batch execution for whatever remained in the
// last batch
affectedRows += insStmt.executeBatch();

// finally commit
myDBconn.commit();

所有这些都记录在JDBC文档中,所以不应该有这个问题。

备注:不支持ARRAY数据类型(既不支持单个预处理语句也不支持批处理) - 以防万一您想要这样做...