snagen
snagen

Reputation: 61

Batch insert in Hive using Hive-JDBC

I am trying to insert data into Hive (NON-ACID) table using hive-jdbc connection. It works if I execute a single SQL query in a 'statement'. If I try to batch the SQL using 'addBatch', I get an error 'method not supported'. I am using hive-jdbc 2.1 and HDP 2.3. Is there a way to batch multiple SQL into a single 'statement' using hive-jdbc?

Upvotes: 6

Views: 2717

Answers (3)

Bugs Buggy
Bugs Buggy

Reputation: 1546

Try this, works for me:

INSERT INTO <table_name> VALUES ("Hello", "World"), ("This", "works"), ("Be", "Awesome")

This will run as one map-reduce job, hence will save time as well. It will create three rows with the mentioned values.

Use StringBuilder to loop over the values and keep appending to the query String, and then execute that String.

Upvotes: 2

phantomsee
phantomsee

Reputation: 31

As Ben mentioned, the addBatch() method is not supported in hive jdbc. You can insert multiple data in one statement, for example:

String batchInsertSql = "insert into name_age values (?,?),(?,?)";
preparedStatement = connection.prepareStatement(batchInsertSql);
preparedStatement.setString(1, "tom");
preparedStatement.setInt(2, 10);
preparedStatement.setString(3, "sam");
preparedStatement.setInt(4, 20);
preparedStatement.execute();

Upvotes: 2

Ben
Ben

Reputation: 187

Unfortunately there is just an interface for method addBatch from Hive-JDBC, there is NO implementation ...

public void addBatch() throws SQLException {
    // TODO Auto-generated method stub
    throw new SQLException("Method not supported");
}

Upvotes: 1

Related Questions