Skip to content

Commit 5b9c3a9

Browse files
committed
first draft
1 parent da7d89f commit 5b9c3a9

File tree

3 files changed

+330
-0
lines changed

3 files changed

+330
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ toc_landing_pages = [
1919
"/api-documentation",
2020
"/security",
2121
"/security/auth",
22+
"/integrations"
2223
]
2324

2425
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

source/integrations.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Third-Party Integrations
1818
:depth: 2
1919
:class: singlecol
2020

21+
.. toctree::
22+
23+
Tutorial: Spring Data Framework </integrations/spring-data-integration>
24+
2125
Overview
2226
--------
2327

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
.. _java-flask-celery:
2+
.. original URL: https://www.mongodb.com/developer/products/mongodb/python-flask-celery-newsletter/
3+
4+
===========================================
5+
Tutorial: Spring Data Framework Integration
6+
===========================================
7+
8+
.. contents:: On this page
9+
:local:
10+
:backlinks: none
11+
:depth: 3
12+
:class: singlecol
13+
14+
.. facet::
15+
:name: genre
16+
:values: tutorial
17+
18+
.. meta::
19+
:description: Build an application using the Spring Data Framework and the MongoDB Java driver.
20+
:keywords: spring, tutorial, bulk insert, code example
21+
22+
Overview
23+
--------
24+
25+
In this tutorial, you can learn how to use `Spring Data
26+
MongoDB <https://spring.io/projects/spring-data-mongodb>`__ with the
27+
{+driver-short+} to build a bulk write application.
28+
29+
Spring Data MongoDB
30+
~~~~~~~~~~~~~~~~~~~
31+
32+
Spring Data MongoDB is a third-party Java ORM for MongoDB. The Spring Data
33+
project provides a familiar and consistent Spring-based programming model which
34+
is enhanced by MongoDB-specific features and capabilities. Spring Data MongoDB
35+
uses a POJO-centric model for interacting with collections and writing
36+
repository-style data access layers.
37+
38+
The Spring Data BulkOperations Interface
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
41+
BulkOperations is an Spring Framework interface that contains a list of write
42+
operations that can be applied to your database. They can be any combination of
43+
the following {+driver-short+} operations:
44+
45+
- ``insert``
46+
- ``updateOne``
47+
- ``updateMany``
48+
- ``replaceOne``
49+
- ``deleteOne``
50+
- ``deleteMany``
51+
- ``upsert``
52+
53+
A BulkOperation can be ordered or unordered. Ordered operations will be applied
54+
sequentially and if an error is detected, will return with an error code.
55+
Unordered operations will be applied in parallel, which means they can be
56+
faster. However, you must check if there were errors during the operations.
57+
58+
For more information, see the `BulkOperations
59+
<https://www.mongodb.com/docs/manual/core/bulk-write-operations/>`__ page of the
60+
Spring Framework API documentation and the :manual:`Bulk Write Operations
61+
</core/bulk-write-operations/>` page of the MongoDB server manual.
62+
63+
Tutorial
64+
--------
65+
66+
You can find the completed sample app for this tutorial in the
67+
:github:`SpringDataBulkInsert sample project
68+
</jorge-imperial/SpringDataMongoBulkInsert>` GitHub repository.
69+
70+
.. note:: Imports Not specified
71+
72+
The import statements required for the classes in the tutorial have not been included. See the GitHub repository for complete files.
73+
74+
Prerequisites
75+
~~~~~~~~~~~~~
76+
77+
Ensure you have the following components installed and set up before you start
78+
this tutorial:
79+
80+
- `Java 8 or later <https://www.oracle.com/java/technologies/downloads/>`__
81+
- A MongoDB Atlas cluster
82+
To learn how to set up a cluster, see the :ref:`Getting Started
83+
<java-get-started>`__ guide for more information.
84+
- `Spring Boot application setup <https://spring.io/guides/gs/spring-boot>`__
85+
- Maven for dependency management
86+
87+
Add Dependencies
88+
~~~~~~~~~~~~~~~~
89+
90+
Add the following dependencies to your ``pom.xml``:
91+
92+
.. code-block:: xml
93+
94+
<dependencies>
95+
<dependency>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
98+
<version>4.4.0</version>
99+
</dependency>
100+
101+
Ensure that you are using a Spring Data version that is compatible with the
102+
{+driver-long+} and Java versions you are using. For compatibility specifications, see
103+
the `Requirements
104+
<https://docs.spring.io/spring-data/mongodb/reference/preface.html>`__ page
105+
of the Spring Data MongoDB documentation, and the :ref:`Compatibility
106+
<java-compatibility-tables>` page of this guide.
107+
108+
MongoClient Configuration
109+
~~~~~~~~~~~~~~~~~~~~~~~~~
110+
111+
The ``MongodConfig`` class contains the configuration for the ``MongoClient``
112+
that will allow the Spring Data framework to interact with the MongoDB server
113+
and sets the template for data operations.
114+
115+
This application uses ``@Configuration`` annotations for classes, ``@Bean``
116+
annotations for methods, and ``@Value`` annotations for parameter conversion.
117+
These annotations allow the Spring IoC container to manage objects. For a
118+
detailed explanation of these annotations, see the following sections of the
119+
Spring Data framework guide:
120+
121+
- ``@Configuration`` and ``@Bean`` annotations: `Java-based Container
122+
Configuration
123+
<https://docs.spring.io/spring-framework/docs/5.3.22/reference/html/core.html#beans-java>`__
124+
- ``@Value`` annotations: `Using @Value
125+
<https://docs.spring.io/spring-framework/docs/5.3.22/reference/html/core.html#beans-value-annotations>`__
126+
127+
Create configuration and template classes to set up your MongoDB connection by adding the
128+
following code to your application:
129+
130+
.. code-block:: java
131+
132+
@Configuration
133+
public class MongoConfig {
134+
@Value("${mongodb.uri}")
135+
private String uri;
136+
137+
@Value("${mongodb.database}")
138+
private String databaseName;
139+
140+
// Indicates whether you deployment is managed by Atlas
141+
@Value("${mongodb.atlas}")
142+
private boolean atlas;
143+
144+
// Details for accessing credentials in your JVM trust store
145+
// for non-Atlas deployments
146+
@Value("${truststore.path}")
147+
private String trustStorePath;
148+
@Value("${truststore.pwd}")
149+
private String trustStorePwd;
150+
151+
@Bean
152+
public MongoClient mongo() {
153+
ConnectionString connectionString = new ConnectionString(uri);
154+
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
155+
.applyConnectionString(connectionString)
156+
.applyToSslSettings(builder -> {
157+
if (!atlas) {
158+
// Use SSLContext if a trustStore has been provided
159+
if (!trustStorePath.isEmpty()) {
160+
SSLFactory sslFactory = SSLFactory.builder()
161+
.withTrustMaterial(Paths.get(trustStorePath), trustStorePwd.toCharArray())
162+
.build();
163+
SSLContext sslContext = sslFactory.getSslContext();
164+
builder.context(sslContext);
165+
builder.invalidHostNameAllowed(true);
166+
}
167+
}
168+
builder.enabled(true);
169+
})
170+
.build();
171+
return MongoClients.create(mongoClientSettings);
172+
}
173+
174+
@Bean
175+
public MongoTemplate mongoTemplate() throws Exception {
176+
return new MongoTemplate(mongo(), databaseName);
177+
}
178+
}
179+
180+
Ensure that your connection string (``mongodb.database``) and
181+
database name (``mongodb.database``) are set in your environment variables. For
182+
more information, see the :ref:`<java-get-started-connection-string>` section of
183+
this guide.
184+
185+
For non-Atlas deployments, ensure your JVM trust store information is set in
186+
your environment variables. For more information, see the :ref:`Configure the
187+
JVM Trust Store <tls-configure-jvm-truststore>` of this guide.
188+
189+
Object to Document Mapping
190+
~~~~~~~~~~~~~~~~~~~~~~~~~~
191+
192+
Mapping a class to a collection allows the Spring IoC container to store objects
193+
as MongoDB documents. You can use the ``@Document`` annotation to specify which
194+
collection a class maps to. For more information about mapping objects to MongoDB
195+
documents, see the `Mapping Annotation Overview
196+
<https://docs.spring.io/spring-data/mongodb/docs/current-SNAPSHOT/reference/html/#mapping-usage-annotations>`__
197+
section of the Spring Data MongoDB documentation.
198+
199+
Create your ``Product`` class and map it to your ``products`` collection by
200+
adding the following code to your application:
201+
202+
.. code-block:: java
203+
204+
@Document("products")
205+
public class Products {
206+
207+
private static final Logger LOG = LoggerFactory
208+
.getLogger(Products.class);
209+
210+
// Indicates our default index
211+
@Id
212+
private String id;
213+
214+
private String name;
215+
private int qty;
216+
private double price;
217+
private Date available;
218+
private Date unavailable;
219+
private String skuId;
220+
221+
// Getters and setters
222+
}
223+
224+
You can add getters and setters for each field.
225+
226+
Implement a Bulk Write Method
227+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
228+
229+
The repository that stores your Products is implemented by two classes: an
230+
interface, and an interface implementation. This will allow you to create
231+
multiple product repositories for storing different products, while maintaining
232+
a basic interface.
233+
234+
Create a generic repository interface with a bulk inserts method by adding the
235+
following code to your application:
236+
237+
.. code-block:: java
238+
239+
public interface CustomProductsRepository {
240+
void updateProductQuantity(String name, int newQty);
241+
int bulkInsertProducts(int count);
242+
}
243+
244+
Create a class that implements and customizes the repository by adding the
245+
following code to your application:
246+
247+
.. code-block:: java
248+
249+
@Component
250+
public class CustomProductsRepositoryImpl implements CustomProductsRepository {
251+
252+
private static final Logger LOG = LoggerFactory
253+
.getLogger(CustomProductsRepository.class);
254+
255+
@Autowired
256+
MongoTemplate mongoTemplate;
257+
258+
public int bulkInsertProducts(int count) {
259+
260+
// For testing purposes, clear any existing data in your product collection
261+
LOG.info("Dropping collection...");
262+
mongoTemplate.dropCollection(Products.class);
263+
LOG.info("Dropped!");
264+
265+
Instant start = Instant.now();
266+
mongoTemplate.setWriteConcern(WriteConcern.W1.withJournal(true));
267+
268+
Products [] productList = Products.RandomProducts(count);
269+
BulkOperations bulkInsertion = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Products.class);
270+
271+
for (int i=0; i<productList.length; ++i)
272+
bulkInsertion.insert(productList[i]);
273+
274+
BulkWriteResult bulkWriteResult = bulkInsertion.execute();
275+
276+
LOG.info("Bulk insert of "+bulkWriteResult.getInsertedCount()+" documents completed in "+ Duration.between(start, Instant.now()).toMillis() + " milliseconds");
277+
return bulkWriteResult.getInsertedCount();
278+
}
279+
}
280+
281+
This method uses unordered bulk inserts, which can improve performance by not guaranteeing the order of operations.
282+
283+
Perform a Bulk Operation
284+
~~~~~~~~~~~~~~~~~~~~~~~~
285+
286+
Create a main application class, trigger the bulk insert on startup:
287+
288+
.. code-block:: java
289+
290+
@SpringBootApplication
291+
public class Application implements CommandLineRunner {
292+
293+
@Autowired
294+
private ProductRepository productRepository;
295+
296+
@Override
297+
public void run(String... args) throws Exception {
298+
List<Product> products = Arrays.asList(
299+
new Product("Product1", 10, 100.0),
300+
new Product("Product2", 20, 200.0)
301+
);
302+
productRepository.bulkInsert(products);
303+
}
304+
305+
public static void main(String[] args) {
306+
SpringApplication.run(Application.class, args);
307+
}
308+
}
309+
310+
Conclusion
311+
----------
312+
313+
Implementing bulk writes in Spring Boot with MongoDB significantly improves performance by minimizing database round trips. By following the steps in this tutorial, you can efficiently manage batch data operations in your applications.
314+
315+
More Resources
316+
--------------
317+
318+
For more information about the Spring Data Framework, see the
319+
following resources:
320+
321+
- `Spring Framework Documentation <https://docs.spring.io/spring-framework/reference/>`__
322+
- `Spring Data for MongoDB reference <https://docs.spring.io/spring-data/mongodb/reference/>`__
323+
- `Spring Data for MongoDB API documentation <https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/package-summary.html>`__
324+
325+
For support or to contribute to the MongoDB Community, see the `MongoDB Developer Community <https://community.mongodb.com/>`__.

0 commit comments

Comments
 (0)