|
2 | 2 |
|
3 | 3 | [MIT License]: https://opensource.org/licenses/MIT |
4 | 4 |
|
5 | | -Work In Progress |
| 5 | +[JSqlServerBulkInsert] is a library to simplify Bulk Inserts to the SQL Server. It wraps the ``SQLServerBulkCopy`` behind a nice API. |
| 6 | + |
| 7 | +## Installing ## |
| 8 | + |
| 9 | +You can obtain [JSqlServerBulkInsert] from Maven by adding the following: |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependency> |
| 13 | + <groupId>de.bytefish</groupId> |
| 14 | + <artifactId>jsqlserverbulkinsert</artifactId> |
| 15 | + <version>1.0</version> |
| 16 | +</dependency> |
| 17 | +``` |
| 18 | + |
| 19 | +## Getting Started ## |
| 20 | + |
| 21 | +Imagine ``1,000,000`` Persons should be inserted into an SQL Server database. |
| 22 | + |
| 23 | +### Domain Model ### |
| 24 | + |
| 25 | +The domain model could be the ``Person`` class with a First Name, Last Name and a birth date. |
| 26 | + |
| 27 | +```java |
| 28 | +// Copyright (c) Philipp Wagner. All rights reserved. |
| 29 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 30 | + |
| 31 | +package de.bytefish.jsqlserverbulkinsert.test.model; |
| 32 | + |
| 33 | +import java.time.LocalDate; |
| 34 | + |
| 35 | +public class Person { |
| 36 | + |
| 37 | + private String firstName; |
| 38 | + |
| 39 | + private String lastName; |
| 40 | + |
| 41 | + private LocalDate birthDate; |
| 42 | + |
| 43 | + public Person() { |
| 44 | + } |
| 45 | + |
| 46 | + public String getFirstName() { |
| 47 | + return firstName; |
| 48 | + } |
| 49 | + |
| 50 | + public void setFirstName(String firstName) { |
| 51 | + this.firstName = firstName; |
| 52 | + } |
| 53 | + |
| 54 | + public String getLastName() { |
| 55 | + return lastName; |
| 56 | + } |
| 57 | + |
| 58 | + public void setLastName(String lastName) { |
| 59 | + this.lastName = lastName; |
| 60 | + } |
| 61 | + |
| 62 | + public LocalDate getBirthDate() { |
| 63 | + return birthDate; |
| 64 | + } |
| 65 | + |
| 66 | + public void setBirthDate(LocalDate birthDate) { |
| 67 | + this.birthDate = birthDate; |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Mapping ### |
| 73 | + |
| 74 | +To bulk insert the ``Person`` data to a SQL Server database it is important to know how to map |
| 75 | +between the Java Object and the Database Columns: |
| 76 | + |
| 77 | +```java |
| 78 | +// Copyright (c) Philipp Wagner. All rights reserved. |
| 79 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 80 | + |
| 81 | +package de.bytefish.jsqlserverbulkinsert.test.integration; |
| 82 | + |
| 83 | +import de.bytefish.jsqlserverbulkinsert.mapping.AbstractMapping; |
| 84 | +import de.bytefish.jsqlserverbulkinsert.test.model.Person; |
| 85 | + |
| 86 | +public class PersonMapping extends AbstractMapping<Person> { |
| 87 | + |
| 88 | + public PersonMapping() { |
| 89 | + super("dbo", "UnitTest"); |
| 90 | + |
| 91 | + mapString("FirstName", Person::getFirstName); |
| 92 | + mapString("LastName", Person::getLastName); |
| 93 | + mapDate("BirthDate", Person::getBirthDate); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### Construct and Use the SqlServerBulkInsert ### |
| 99 | + |
| 100 | +The ``AbstractMapping`` is used to instantiate a ``SqlServerBulkInsert``, which provides a ``saveAll`` method to store a given stream of data. |
| 101 | + |
| 102 | +```java |
| 103 | +// Instantiate the SqlServerBulkInsert class: |
| 104 | +SqlServerBulkInsert<Person> bulkInsert = new SqlServerBulkInsert<>(mapping); |
| 105 | +// Now save all entities of a given stream: |
| 106 | +bulkInsert.saveAll(connection, persons.stream()); |
| 107 | +``` |
| 108 | + |
| 109 | +And the full Integration Test: |
| 110 | + |
| 111 | +```java |
| 112 | +// Copyright (c) Philipp Wagner. All rights reserved. |
| 113 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 114 | + |
| 115 | +package de.bytefish.jsqlserverbulkinsert.test.integration; |
| 116 | + |
| 117 | +import de.bytefish.jsqlserverbulkinsert.mapping.AbstractMapping; |
| 118 | +import de.bytefish.jsqlserverbulkinsert.test.model.Person; |
| 119 | +import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert; |
| 120 | +import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase; |
| 121 | +import org.junit.Assert; |
| 122 | +import org.junit.Test; |
| 123 | + |
| 124 | +import java.sql.ResultSet; |
| 125 | +import java.sql.SQLException; |
| 126 | +import java.sql.Statement; |
| 127 | +import java.time.LocalDate; |
| 128 | +import java.util.ArrayList; |
| 129 | +import java.util.List; |
| 130 | + |
| 131 | +public class IntegrationTest extends TransactionalTestBase { |
| 132 | + |
| 133 | + @Override |
| 134 | + protected void onSetUpInTransaction() throws Exception { |
| 135 | + createTable(); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void bulkInsertPersonDataTest() throws SQLException { |
| 140 | + // The Number of Entities to insert: |
| 141 | + int numEntities = 1000000; |
| 142 | + // Create a large list of Persons: |
| 143 | + List<Person> persons = getPersonList(numEntities); |
| 144 | + // Create the Mapping: |
| 145 | + PersonMapping mapping = new PersonMapping(); |
| 146 | + // Create the Bulk Inserter: |
| 147 | + SqlServerBulkInsert<Person> bulkInsert = new SqlServerBulkInsert<>(mapping); |
| 148 | + // Now save all entities of a given stream: |
| 149 | + bulkInsert.saveAll(connection, persons.stream()); |
| 150 | + // And assert all have been written to the database: |
| 151 | + Assert.assertEquals(numEntities, getRowCount()); |
| 152 | + } |
| 153 | + |
| 154 | + private List<Person> getPersonList(int numPersons) { |
| 155 | + List<Person> persons = new ArrayList<>(); |
| 156 | + |
| 157 | + for (int pos = 0; pos < numPersons; pos++) { |
| 158 | + Person p = new Person(); |
| 159 | + |
| 160 | + p.setFirstName("Philipp"); |
| 161 | + p.setLastName("Wagner"); |
| 162 | + p.setBirthDate(LocalDate.of(1986, 5, 12)); |
| 163 | + |
| 164 | + persons.add(p); |
| 165 | + } |
| 166 | + |
| 167 | + return persons; |
| 168 | + } |
| 169 | + |
| 170 | + private boolean createTable() throws SQLException { |
| 171 | + |
| 172 | + String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" + |
| 173 | + " (\n" + |
| 174 | + " FirstName NVARCHAR(255),\n" + |
| 175 | + " LastName NVARCHAR(255),\n" + |
| 176 | + " BirthDate DATE\n" + |
| 177 | + " );"; |
| 178 | + |
| 179 | + Statement statement = connection.createStatement(); |
| 180 | + |
| 181 | + return statement.execute(sqlStatement); |
| 182 | + } |
| 183 | + |
| 184 | + private int getRowCount() throws SQLException { |
| 185 | + |
| 186 | + Statement s = connection.createStatement(); |
| 187 | + |
| 188 | + ResultSet r = s.executeQuery("SELECT COUNT(*) AS total FROM [dbo].[UnitTest];"); |
| 189 | + r.next(); |
| 190 | + int count = r.getInt("total"); |
| 191 | + r.close(); |
| 192 | + |
| 193 | + return count; |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
0 commit comments