Skip to content

Commit d1d9116

Browse files
committed
README
1 parent 93a98c7 commit d1d9116

4 files changed

Lines changed: 298 additions & 16 deletions

File tree

JSqlServerBulkInsert/pom.xml

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
<groupId>de.bytefish</groupId>
99
<artifactId>jsqlserverbulkinsert</artifactId>
10-
<version>1.4</version>
10+
<version>1.0</version>
1111
<name>jsqlserverbulkinsert</name>
12-
<description>JSqlServerBulkInsert is a Java library for Bulk Inserts with PostgreSQL.</description>
12+
<description>JSqlServerBulkInsert is a Java library for Bulk Inserts to the SQL Server.</description>
1313
<url>http://www.github.com/bytefish/JSqlServerBulkInsert</url>
1414

1515
<!-- Define the License -->
@@ -56,6 +56,91 @@
5656
<sqlserver.version>6.1.0.jre8</sqlserver.version>
5757
</properties>
5858

59+
60+
<distributionManagement>
61+
<snapshotRepository>
62+
<id>ossrh</id>
63+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
64+
</snapshotRepository>
65+
<repository>
66+
<id>ossrh</id>
67+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
68+
</repository>
69+
</distributionManagement>
70+
71+
<profiles>
72+
<profile>
73+
<id>release</id>
74+
<build>
75+
<plugins>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-gpg-plugin</artifactId>
79+
<version>1.4</version>
80+
<executions>
81+
<execution>
82+
<id>sign-artifacts</id>
83+
<phase>verify</phase>
84+
<goals>
85+
<goal>sign</goal>
86+
</goals>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
</plugins>
91+
<pluginManagement>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-release-plugin</artifactId>
96+
<version>2.1</version>
97+
<configuration>
98+
<mavenExecutorId>forked-path</mavenExecutorId>
99+
<useReleaseProfile>false</useReleaseProfile>
100+
<arguments>${arguments} -Psonatype-oss-release</arguments>
101+
</configuration>
102+
</plugin>
103+
</plugins>
104+
</pluginManagement>
105+
</build>
106+
</profile>
107+
108+
<profile>
109+
<id>docs-and-source</id>
110+
<build>
111+
<plugins>
112+
<plugin>
113+
<groupId>org.apache.maven.plugins</groupId>
114+
<artifactId>maven-source-plugin</artifactId>
115+
<version>2.2.1</version>
116+
<executions>
117+
<execution>
118+
<id>attach-sources</id>
119+
<goals>
120+
<goal>jar-no-fork</goal>
121+
</goals>
122+
</execution>
123+
</executions>
124+
</plugin>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-javadoc-plugin</artifactId>
128+
<version>2.9.1</version>
129+
<executions>
130+
<execution>
131+
<id>attach-javadocs</id>
132+
<goals>
133+
<goal>jar</goal>
134+
</goals>
135+
</execution>
136+
</executions>
137+
</plugin>
138+
</plugins>
139+
</build>
140+
</profile>
141+
142+
</profiles>
143+
59144
<dependencies>
60145

61146
<dependency>
@@ -64,7 +149,6 @@
64149
<version>6.4.0.jre8</version>
65150
</dependency>
66151

67-
68152
<dependency>
69153
<groupId>junit</groupId>
70154
<artifactId>junit</artifactId>

JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/integration/IntegrationTest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@
1919

2020
public class IntegrationTest extends TransactionalTestBase {
2121

22-
private class PersonMapping extends AbstractMapping<Person> {
23-
24-
public PersonMapping() {
25-
super("dbo", "UnitTest");
26-
27-
mapString("FirstName", Person::getFirstName);
28-
mapString("LastName", Person::getLastName);
29-
mapDate("BirthDate", Person::getBirthDate);
30-
}
31-
}
32-
3322
@Override
3423
protected void onSetUpInTransaction() throws Exception {
3524
createTable();
@@ -44,7 +33,7 @@ public void bulkInsertPersonDataTest() throws SQLException {
4433
// Create the Mapping:
4534
PersonMapping mapping = new PersonMapping();
4635
// Create the Bulk Inserter:
47-
SqlServerBulkInsert<Person> bulkInsert = new SqlServerBulkInsert<Person>(mapping);
36+
SqlServerBulkInsert<Person> bulkInsert = new SqlServerBulkInsert<>(mapping);
4837
// Now save all entities of a given stream:
4938
bulkInsert.saveAll(connection, persons.stream());
5039
// And assert all have been written to the database:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Philipp Wagner. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package de.bytefish.jsqlserverbulkinsert.test.integration;
5+
6+
import de.bytefish.jsqlserverbulkinsert.mapping.AbstractMapping;
7+
import de.bytefish.jsqlserverbulkinsert.test.model.Person;
8+
9+
public class PersonMapping extends AbstractMapping<Person> {
10+
11+
public PersonMapping() {
12+
super("dbo", "UnitTest");
13+
14+
mapString("FirstName", Person::getFirstName);
15+
mapString("LastName", Person::getLastName);
16+
mapDate("BirthDate", Person::getBirthDate);
17+
}
18+
}

README.md

Lines changed: 192 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,195 @@
22

33
[MIT License]: https://opensource.org/licenses/MIT
44

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

Comments
 (0)