public class ExportListener extends Object implements QueryBatchListener
Reads document contents (and optionally metadata) for each batch, then sends each document to any listeners registered with onDocumentReady for further processing or writing to any target supported by Java. Supports reading partial documents via transforms. Supports exporting all documents at a consistent point-in-time using withConsistentSnapshot.
For example:
QueryBatcher exportBatcher = moveMgr.newQueryBatcher(query)
.withConsistentSnapshot()
.onUrisReady(
new ExportListener()
.withConsistentSnapshot()
.onDocumentReady(doc -> {
logger.debug("Contents=[{}]", doc.getContentAs(String.class));
})
)
.onQueryFailure(exception -> exception.printStackTrace());
JobTicket ticket = moveMgr.startJob(exportBatcher);
exportBatcher.awaitCompletion();
moveMgr.stopJob(ticket);
By default only document contents are retrieved. If you would also like metadata, make sure to call withMetadataCategory to configure which categories of metadata you desire.
As with all the provided listeners, this listener will not meet the needs of all applications but the source code for it should serve as helpful sample code so you can write your own custom listeners.
| Constructor and Description |
|---|
ExportListener() |
| Modifier and Type | Method and Description |
|---|---|
ExportListener |
onBatchFailure(BatchFailureListener<Batch<String>> listener)
When a batch fails or a callback throws an Exception, run this listener code.
|
ExportListener |
onDocumentReady(Consumer<DocumentRecord> listener)
Adds a listener to process each retrieved document, which is the way users of ExportListener can provide custom code to export documents.
|
void |
processEvent(QueryBatch batch)
This is the method QueryBatcher calls for ExportListener to do its thing.
|
ExportListener |
withConsistentSnapshot()
Specifies that documents should be retrieved as they were when this QueryBatcher job started.
|
ExportListener |
withMetadataCategory(DocumentManager.Metadata category)
Adds a metadata category to retrieve and make available from
DocumentRecord.getMetadata to listeners registered with onDocumentReady. |
ExportListener |
withNonDocumentFormat(Format nonDocumentFormat)
Sets the format for metadata available from
DocumentRecord.getMetadata to listeners registered with onDocumentReady (assuming withMetadataCategory has been called to request specific metdata). |
ExportListener |
withTransform(ServerTransform transform)
Sets the server tranform to modify the document contents.
|
public void processEvent(QueryBatch batch)
This is the method QueryBatcher calls for ExportListener to do its thing. You should not need to call it.
processEvent in interface BatchListener<QueryBatch>processEvent in interface QueryBatchListenerbatch - the batch of uris and some metadata about the current status of the jobpublic ExportListener withConsistentSnapshot()
Specifies that documents should be retrieved as they were when this QueryBatcher job started. This enables a point-in-time export so that all documents are as they were at that point in time. This requires that the server be configured to allow such queries by setting the merge timestamp to a timestamp before the job starts or a sufficiently large negative value. This should only be used when the QueryBatcher is constructed with a query, not with an Iterator.
QueryBatcher.withConsistentSnapshot()public ExportListener withMetadataCategory(DocumentManager.Metadata category)
Adds a metadata category to retrieve and make available from DocumentRecord.getMetadata to listeners registered with onDocumentReady. To specify the format for the metdata, call withNonDocumentFormat.
category - the metadata category to retrieveDocumentManager.setMetadataCategoriespublic ExportListener withNonDocumentFormat(Format nonDocumentFormat)
Sets the format for metadata available from DocumentRecord.getMetadata to listeners registered with onDocumentReady (assuming withMetadataCategory has been called to request specific metdata). If setNonDocumentFormat is not called, the server default format will be used.
nonDocumentFormat - the format in which retrieve the metadataDocumentManager.setNonDocumentFormatpublic ExportListener withTransform(ServerTransform transform)
Sets the server tranform to modify the document contents.
transform - the name of the transform already installed in the REST serverpublic ExportListener onDocumentReady(Consumer<DocumentRecord> listener)
Adds a listener to process each retrieved document, which is the way users of ExportListener can provide custom code to export documents. This custom code could write the document or a portion of the document to the file system, a REST service, or any target supported by Java. If further information is required about the document beyond what DocumentRecord can provide, register a listener with QueryBatcher.onUrisReady instead.
listener - the code which will process each documentConsumer,
DocumentRecordpublic ExportListener onBatchFailure(BatchFailureListener<Batch<String>> listener)
When a batch fails or a callback throws an Exception, run this listener code. Multiple listeners can be registered with this method.
listener - the code to run when a failure occursCopyright © 2013-2017 MarkLogic Corporation.