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)?
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.
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...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With