public class ApplyTransformListener extends java.lang.Object implements QueryBatchListener
Modifies documents in-place in the database by applying a server-side transform
.
If the transform modifies documents to no longer match the query,
ApplyTransformListener should only be used when:
QueryBatcher.withConsistentSnapshot()
is called, ornewQueryBatcher(Iterator<String>)
is used to traverse a static data set
function transform_function(context, params, content) {
var document = content.toObject();
document.someProperty = params.newValue;
return document;
};
exports.transform = transform_function;
installed in the server like so (using the MarkLogic Java Client API):
restAdminClient.newServerConfigManager().newTransformExtensionsManager().writeJavascriptTransform(
"myTransform", new FileHandle(new File("myTransform.sjs")));
you can run the transform on documents matching a query like so:
ServerTransform transform = new ServerTransform(transformName2)
.addParameter("newValue", "some new value");
ApplyTransformListener listener = new ApplyTransformListener()
.withTransform(transform)
.withApplyResult(ApplyResult.REPLACE);
QueryBatcher batcher = moveMgr.newQueryBatcher(query)
.onUrisReady(listener);
JobTicket ticket = moveMgr.startJob( batcher );
batcher.awaitCompletion();
moveMgr.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.
In this listener, we initialize only the HostAvailabilityListener's RetryListener and not NoResponseListener's RetryListener because if we get empty responses when we try to apply a transform to the batch of URIs retrieved from the server, we are not sure what happened in the server - if the transform has been applied or it has not been applied. Retrying in those scenarios would apply the transform twice if the transform has been already applied and this is not desirable.
In order to handle such scenarios where we get an empty response, it is recommended to add a BatchFailureListener which would take care of apply transform failures and retry only for those URIs for which the apply transform has failed. If the transform is idempotent, we can just initialize the RetryListener of the NoResponseListener by calling NoResponseListener.initializeRetryListener(this) and add it to the BatchFailureListeners similar to what we have in the other listeners.
Modifier and Type | Class and Description |
---|---|
static class |
ApplyTransformListener.ApplyResult
Either
ApplyTransformListener.ApplyResult.REPLACE each document with the result of the transform, or run
the transform with each document as input, but ApplyTransformListener.ApplyResult.IGNORE the result. |
Constructor and Description |
---|
ApplyTransformListener() |
Modifier and Type | Method and Description |
---|---|
void |
initializeListener(QueryBatcher queryBatcher)
This implementation of initializeListener adds this instance of
ApplyTransformListener to the two RetryListener's in this QueryBatcher so
they will retry any batches that fail during the apply-transform request.
|
ApplyTransformListener |
onBatchFailure(BatchFailureListener<Batch<java.lang.String>> listener)
Deprecated.
|
ApplyTransformListener |
onFailure(BatchFailureListener<QueryBatch> listener)
When a batch fails or a callback throws an Exception, run this listener
code.
|
ApplyTransformListener |
onSkipped(QueryBatchListener listener)
When documents were not found and therefore not transformed, run this
listener code.
|
ApplyTransformListener |
onSuccess(QueryBatchListener listener)
When a batch has been successfully transformed, run this listener code.
|
void |
processEvent(QueryBatch batch)
The standard BatchListener action called by QueryBatcher.
|
ApplyTransformListener |
withApplyResult(ApplyTransformListener.ApplyResult applyResult)
|
ApplyTransformListener |
withTransform(ServerTransform transform)
The ServerTransform to run on each document from each batch.
|
public void initializeListener(QueryBatcher queryBatcher)
initializeListener
in interface QueryBatchListener
queryBatcher
- the QueryBatcher which will call this Listenerpublic void processEvent(QueryBatch batch)
processEvent
in interface BatchListener<QueryBatch>
processEvent
in interface QueryBatchListener
batch
- the batch of uris and some metadata about the current status of the jobpublic ApplyTransformListener onSuccess(QueryBatchListener listener)
listener
- the code to run when a batch is successfully transformedpublic ApplyTransformListener onSkipped(QueryBatchListener listener)
listener
- the code to run when documents were not transformed@Deprecated public ApplyTransformListener onBatchFailure(BatchFailureListener<Batch<java.lang.String>> listener)
onFailure(BatchFailureListener)
listener
- the code to run when a failure occurspublic ApplyTransformListener onFailure(BatchFailureListener<QueryBatch> listener)
listener
- the code to run when a failure occurspublic ApplyTransformListener withTransform(ServerTransform transform)
transform
- the ServerTransform to run on each document from each batchpublic ApplyTransformListener withApplyResult(ApplyTransformListener.ApplyResult applyResult)
REPLACE
each document with the result of the transform, or run
the transform with each document as input, but IGNORE
the result.applyResult
- the behavior required after each transform is runCopyright © 2013-2018 MarkLogic Corporation.