Class MongoItemReader<T>
- All Implemented Interfaces:
ItemReader<T>
,ItemStream
,ItemStreamReader<T>
,org.springframework.beans.factory.InitializingBean
Restartable ItemReader
that reads documents from MongoDB via a paging
technique.
If you set JSON String query setQuery(String)
then it executes the JSON to
retrieve the requested documents.
If you set Query object setQuery(Query)
then it executes the Query to retrieve
the requested documents.
The query is executed using paged requests specified in the AbstractPaginatedDataItemReader.setPageSize(int)
.
Additional pages are requested as needed to provide data when the AbstractItemCountingItemStreamItemReader.read()
method is called.
The JSON String query provided supports parameter substitution via ?<index> placeholders where the <index> indicates the index of the parameterValue to substitute.
The implementation is thread-safe between calls to AbstractItemCountingItemStreamItemReader.open(ExecutionContext)
, but
remember to use saveState=false
if used in a multi-threaded client (no
restart available).
- Author:
- Michael Minella, Takaaki Iida, Mahmoud Ben Hassine, Parikshit Dutta
-
Field Summary
Fields inherited from class org.springframework.batch.item.data.AbstractPaginatedDataItemReader
page, pageSize, results
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Checks mandatory propertiesMethod thisItemStreamReader
delegates to for the actual work of reading a page.void
setCollection
(String collection) void
JSON defining the fields to be returned from the matching documents by MongoDB.void
JSON String telling MongoDB what index to use.void
setParameterValues
(List<Object> parameterValues) List
of values to be substituted in for each of the parameters in the query.void
A JSON formatted MongoDB query.void
setQuery
(org.springframework.data.mongodb.core.query.Query query) A Mongo Query to be used.void
Map
of property names/Sort.Direction
values to sort the input by.void
setTargetType
(Class<? extends T> type) The type of object to be returned for eachAbstractItemCountingItemStreamItemReader.read()
call.void
setTemplate
(org.springframework.data.mongodb.core.MongoOperations template) Used to perform operations against the MongoDB instance.Methods inherited from class org.springframework.batch.item.data.AbstractPaginatedDataItemReader
doClose, doOpen, doRead, jumpToItem, setPageSize
Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, update
Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, setExecutionContextName, setName
-
Constructor Details
-
MongoItemReader
public MongoItemReader()
-
-
Method Details
-
setQuery
public void setQuery(org.springframework.data.mongodb.core.query.Query query) A Mongo Query to be used.- Parameters:
query
- Mongo Query to be used.
-
setTemplate
public void setTemplate(org.springframework.data.mongodb.core.MongoOperations template) Used to perform operations against the MongoDB instance. Also handles the mapping of documents to objects.- Parameters:
template
- the MongoOperations instance to use- See Also:
-
MongoOperations
-
setQuery
A JSON formatted MongoDB query. Parameterization of the provided query is allowed via ?<index> placeholders where the <index> indicates the index of the parameterValue to substitute.- Parameters:
queryString
- JSON formatted Mongo query
-
setTargetType
The type of object to be returned for eachAbstractItemCountingItemStreamItemReader.read()
call.- Parameters:
type
- the type of object to return
-
setParameterValues
List
of values to be substituted in for each of the parameters in the query.- Parameters:
parameterValues
- values
-
setFields
JSON defining the fields to be returned from the matching documents by MongoDB.- Parameters:
fields
- JSON string that identifies the fields to sort by.
-
setSort
Map
of property names/Sort.Direction
values to sort the input by.- Parameters:
sorts
- map of properties and direction to sort each.
-
setCollection
- Parameters:
collection
- Mongo collection to be queried.
-
setHint
JSON String telling MongoDB what index to use.- Parameters:
hint
- string indicating what index to use.
-
doPageRead
Description copied from class:AbstractPaginatedDataItemReader
Method thisItemStreamReader
delegates to for the actual work of reading a page. Each time this method is called, the resultingIterator
should contain the items read within the next page.
If theIterator
is empty or null when it is returned, thisItemReader
will assume that the input has been exhausted.- Specified by:
doPageRead
in classAbstractPaginatedDataItemReader<T>
- Returns:
- an
Iterator
containing the items within a page.
-
afterPropertiesSet
Checks mandatory properties- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
Exception
- See Also:
-
InitializingBean.afterPropertiesSet()
-