Skip to content

Commit 7a578a1

Browse files
committed
Added source code
1 parent f838e53 commit 7a578a1

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.amrut.prabhu.bulkdatainsert;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import javax.persistence.*;
9+
10+
@Data
11+
@Builder
12+
@Entity
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
public class Book {
16+
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGen")
19+
@SequenceGenerator(name = "seqGen", sequenceName = "seq", initialValue = 1)
20+
private Long id;
21+
private String name;
22+
private Integer Price;
23+
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.amrut.prabhu.bulkdatainsert;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface BookRepository extends JpaRepository<Book, String> {
8+
9+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.amrut.prabhu.bulkdatainsert;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.boot.context.event.ApplicationReadyEvent;
8+
import org.springframework.context.event.EventListener;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.IntStream;
14+
15+
@SpringBootApplication
16+
public class BulkDataInsertApplication {
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(BulkDataInsertApplication.class, args);
20+
}
21+
22+
@Autowired
23+
private BookRepository repository;
24+
25+
@Value("${spring.jpa.properties.hibernate.jdbc.batch_size}")
26+
private int batchSize;
27+
28+
@EventListener(ApplicationReadyEvent.class)
29+
public void doSomethingAfterStartup() {
30+
31+
int totalObjects = 10000;
32+
33+
long start = System.currentTimeMillis();
34+
List<Book> books = IntStream.range(0, totalObjects)
35+
.mapToObj(val -> Book.builder()
36+
.name("books" + val)
37+
.Price(val)
38+
.build())
39+
.collect(Collectors.toList());
40+
41+
System.out.println("Finished creating "+totalObjects+" objects in memory in:" + (System.currentTimeMillis() - start)/1000);
42+
43+
start = System.currentTimeMillis();
44+
System.out.println("Inserting ..........");
45+
46+
for (int i = 0; i < totalObjects; i += batchSize) {
47+
if( i+ batchSize > totalObjects){
48+
List<Book> books1 = books.subList(i, totalObjects - 1);
49+
repository.saveAll(books1);
50+
break;
51+
}
52+
List<Book> books1 = books.subList(i, i + batchSize);
53+
repository.saveAll(books1);
54+
}
55+
56+
System.out.println("Finished inserting "+totalObjects+" objects in :" + (System.currentTimeMillis() - start));
57+
}
58+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spring:
2+
datasource:
3+
url: jdbc:mysql://localhost:13306/BOOK_DB?serverTimezone=UTC&cachePrepStmts=true&useServerPrepStmts=true&rewriteBatchedStatements=true
4+
username: root
5+
password: zZijbfa64LnL2quYCFyH5jcRn2f3iUNLSrfRwiX3
6+
jpa:
7+
# show-sql: true
8+
generate-ddl: true
9+
properties:
10+
hibernate:
11+
jdbc:
12+
batch_size: 1000
13+
# cache:
14+
# use_second_level_cache: true
15+
# order_updates: true
16+
# order_inserts: true
17+
# generate_statistics: true

0 commit comments

Comments
 (0)