Is it possible bulk insert in HANA?

回眸只為那壹抹淺笑 提交于 2019-12-12 01:23:39

问题


I want to insert into hana by bulk.Currently I am using Java to insert line by line from the result set.Is there a way to insert multiple rows at a time? Is it possible to do? (I do not want import only bulk insert) I searched all over and could not find any good answer.Any help is appreciated?


回答1:


For JAVA/JDBC code, there exists the so-called batch interface. Here's an old example that I used for testing:

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();

All that is documented in the JDBC docu so it shouldn't be a problem to follow this.

Remark: ARRAY data types are not supported (neither for single prepared statements nor for batches) - just in case that is what you wanted to do...



来源:https://stackoverflow.com/questions/41677436/is-it-possible-bulk-insert-in-hana

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!