BatchListener<QueryBatch>
,
QueryBatchListener
public class UrisToWriterListener extends java.lang.Object implements QueryBatchListener
Facilitates writing uris to a file when necessary because
setting merge
timestamp and
withConsistentSnapshot
is not an option, but you
need to run DeleteListener or ApplyTransformListener.
FileWriter writer = new FileWriter("uriCache.txt");
QueryBatcher getUris = dataMovementManager.newQueryBatcher(query)
.withBatchSize(5000)
.onUrisReady( new UrisToWriterListener(writer) )
.onQueryFailure(exception -> exception.printStackTrace());
JobTicket getUrisTicket = dataMovementManager.startJob(getUris);
getUris.awaitCompletion();
dataMovementManager.stopJob(getUrisTicket);
writer.flush();
writer.close();
// now we have the uris, let's step through them
BufferedReader reader = new BufferedReader(new FileReader("uriCache.txt"));
QueryBatcher performDelete = dataMovementManager.newQueryBatcher(reader.lines().iterator())
.onUrisReady(new DeleteListener())
.onQueryFailure(exception -> exception.printStackTrace());
JobTicket ticket = dataMovementManager.startJob(performDelete);
performDelete.awaitCompletion();
dataMovementManager.stopJob(ticket);
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.
Modifier and Type | Class | Description |
---|---|---|
static interface |
UrisToWriterListener.OutputListener |
Constructor | Description |
---|---|
UrisToWriterListener(java.io.Writer writer) |
Modifier and Type | Method | Description |
---|---|---|
void |
initializeListener(QueryBatcher queryBatcher) |
This implementation of initializeListener adds
this instance of UrisToWriterListener to the two RetryListener's in
this QueryBatcher so they will retry any batches that fail during
the uris request.
|
UrisToWriterListener |
onBatchFailure(BatchFailureListener<Batch<java.lang.String>> listener) |
Deprecated.
|
UrisToWriterListener |
onFailure(BatchFailureListener<QueryBatch> listener) |
When a batch fails or a callback throws an
Exception, run this listener code.
|
UrisToWriterListener |
onGenerateOutput(UrisToWriterListener.OutputListener listener) |
|
void |
processEvent(QueryBatch batch) |
The method called by QueryBatcher or
WriteBatcher to run your custom code on this batch.
|
UrisToWriterListener |
withRecordPrefix(java.lang.String prefix) |
|
UrisToWriterListener |
withRecordSuffix(java.lang.String suffix) |
public void initializeListener(QueryBatcher queryBatcher)
initializeListener
in interface QueryBatchListener
queryBatcher
- the QueryBatcher which will call
this Listenerpublic void processEvent(QueryBatch batch)
QueryBatchListener
The method called by QueryBatcher or WriteBatcher to run your custom code on this batch. You usually implement this as a lambda expression.
For example, see the lambda expression passed to onUrisReady:
QueryBatcher qhb = dataMovementManager.newQueryBatcher(query)
.withBatchSize(1000)
.withThreadCount(20)
.onUrisReady(batch -> {
for ( String uri : batch.getItems() ) {
if ( uri.endsWith(".txt") ) {
batch.getClient().newDocumentManager().delete(uri);
}
}
})
.onQueryFailure(queryBatchException -> queryBatchException.printStackTrace());
JobTicket ticket = dataMovementManager.startJob(qhb);
qhb.awaitCompletion();
dataMovementManager.stopJob(ticket);
processEvent
in interface BatchListener<QueryBatch>
processEvent
in interface QueryBatchListener
batch
- the batch of uris and some metadata about
the current status of the jobpublic UrisToWriterListener withRecordSuffix(java.lang.String suffix)
public UrisToWriterListener withRecordPrefix(java.lang.String prefix)
public UrisToWriterListener onGenerateOutput(UrisToWriterListener.OutputListener listener)
@Deprecated public UrisToWriterListener onBatchFailure(BatchFailureListener<Batch<java.lang.String>> listener)
onFailure(BatchFailureListener)
listener
- the code to run when a failure
occurspublic UrisToWriterListener onFailure(BatchFailureListener<QueryBatch> listener)
listener
- the code to run when a failure
occursCopyright © 2013-2019 MarkLogic Corporation.