Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert batch of 1000 records into db?

I'm reading a huge file and inserting records in mysql using statement.executeBatch() of prepared statement. Below is the code:

for(int i = 0; i < file.length; i++) {

      count += 1
      statement.setString(1, record.id)
      statement.setString(2, record.date)
      statement.setString(3, record.msg)
      statement.addBatch()

      if (count % 1000 == 0) 
          statement.executeBatch()
}
statement.executeBatch() 

How can Slick help here (perhaps mimicking the code above)?

like image 626
Pramod R Avatar asked Jun 30 '14 10:06

Pramod R


People also ask

How do I insert more than 1000 records in SQL?

A table can store upto 1000 rows in one insert statement. If a user want to insert multiple rows at a time, the following syntax has to written. If a user wants to insert more than 1000 rows, multiple insert statements, bulk insert or derived table must be used.

How do I insert 1500 records in SQL?

First query USE CustomerDB; IF OBJECT_ID('Customer', 'U') IS NOT NULL DROP TABLE Customer; CREATE TABLE Customer ( CustomerID int PRIMARY KEY IDENTITY, CustomerName nvarchar(16), ...about 130 more columns... ); INSERT INTO Customer VALUES ('FirstCustomerName', ...), ... 1500 more rows...

How can insert 1000 records at a time in MySQL?

MySQL INSERT multiple rows statement In this syntax: First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause.


1 Answers

Using Slick 2.0.2

Once you have a case class modeling your data, your Table class and your TableQuery object defined, just read the file into a Seq, and add it to the TableQuery object using the ++= function.

case class MyDataClass(id: String, date: String, msg: String)

class MyData(tag: Tag) extends Table[MyDataClass](tag,"mydatatableInDB") {
   def id = column[String]("id")
   def date = column[String]("date")
   def msg = column[String]("msg")

   def * = (id, date, msg) <> (MyDataClass.tupled)(MyDataClass.unapply)
}

val myDataTableQuery = TableQuery[MyData]

// read your data into a Seq[MyDataClass] (for example) and...

myDataTableQuery ++= listWithData

If you really need to do it with a batch, you could group the elements using grouped, and iterate on them, adding data to the table query on each iteration. Something like this:

// With your data already in a Seq
def insertIntoTableQuery(data: List[MyDataClass) = {
    myDataTableQuery ++= data
}

// make groups of 1000 and iterate    
listWithData.grouped(1000) foreach insertInToTableQuery

Unless I'm missing something, I think this is pretty much covered in the documentation:

Slick 2.0.2 Documentation

Some weeks ago I had a similar task. The file I was working with had 350,000+ lines, and from each line I had to extract data and create a couple of objects, each of one was to be inserted in a different table. Mind you, I didn't need to do it in a batch and it was really fast.

like image 194
mmedina Avatar answered Nov 01 '22 12:11

mmedina