Marklogic 试图在调用转换模块时实现批处理吗?
我想使用MarkLogicJava客户端Api批处理一组文档。我在下面调用了一个JavaScript模块Marklogic 试图在调用转换模块时实现批处理吗?,marklogic,java,Marklogic,Java,我想使用MarkLogicJava客户端Api批处理一组文档。我在下面调用了一个JavaScript模块 import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.datamovement.ApplyTransformListener; import com.marklogic.client.datamo
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.datamovement.ApplyTransformListener;
import com.marklogic.client.datamovement.ApplyTransformListener.ApplyResult;
import com.marklogic.client.datamovement.DataMovementManager;
import com.marklogic.client.datamovement.JobTicket;
import com.marklogic.client.datamovement.QueryBatcher;
import com.marklogic.client.datamovement.WriteBatcher;
import com.marklogic.client.document.JSONDocumentManager;
import com.marklogic.client.document.ServerTransform;
import com.marklogic.client.io.DOMHandle;
import com.marklogic.client.query.StructuredQueryBuilder;
public class rest {
public static void main(String[] args) {
// TODO Auto-generated method stub
DatabaseClient client = DatabaseClientFactory.newClient
("localhost", port, "x", "x", DatabaseClientFactory.Authentication.DIGEST);
final DataMovementManager manager = client.newDataMovementManager();
// Build query
final StructuredQueryBuilder query = client
.newQueryManager()
.newStructuredQueryBuilder();
// Specify a server-side transformation module (stored procedure) by name
ServerTransform transform = new ServerTransform("restone-tsm");
ApplyTransformListener transformListener = new ApplyTransformListener()
.withTransform(transform)
.withApplyResult(ApplyResult.REPLACE) // Transform in-place, i.e. rewrite
.onSuccess(batch -> {})
.onSkipped(batch -> {})
.onBatchFailure((batch, throwable) -> {});
// Apply the transformation to only the documents that match a query.
// In this case, those in the “raw” collection.
final QueryBatcher batcher = manager
.newQueryBatcher(query.collection("accounts"));
batcher
.withBatchSize(1000)
.withThreadCount(16)
.onUrisReady(transformListener)
.onQueryFailure(exception -> exception.printStackTrace());
final JobTicket ticket = manager.startJob(batcher);
batcher.awaitCompletion();
manager.stopJob(ticket);
}
}
正如您所建议的,更改了我的转换模块(即restonetsm
)
它成功地执行了。但正如您在评论中建议在查询批处理程序中应用cts.uri
一样,我在StructuredQueryBuilder
中检查了该函数,但没有找到任何函数。但是上面的代码工作得很好
谢谢你的帮助
谢谢,请使用批处理程序,而不是单独的批处理程序,如中所述:
您需要确保有一个实现所需接口的函数,并以名称transform
将其导出到服务器上
调整转换中的逻辑,使其不执行URI查询(将由QueryBatcher处理),以便它希望转换内容
function harmonize(context, params, content)
{
var transformed = {};
transformed.Metadata = { "Source" : "International"};
transformed.Canonical= {"Future" : "Element"};
transformed.Source = content;
xdmp.documentInsert(fn.concat("/transformed", fn.baseUri(content)), transformed, {collections : "transform"});
};
exports.transform = harmonize;
谢谢@Hansen,但是.withConsistentSnapshot(),.onUrsReady()
并不是writebatcher
的方法。因此,我尝试使用查询批处理程序
,但它需要查询作为输入,但在我的情况下,我不需要任何查询,因此它不接受。哎呀,我在没有IDE帮助的情况下将其缝合在一起,并将QueryBatcher与WriteBatcher混为一谈。我认为应该是.withTransform()
用于写码器,并删除.withConsistentSnapshot()
。我已经更新了这个例子code@Hansen,使用更新的代码,我可以运行模块,但转换不是我更新问题的地方。但是相同的转换模块通过ServerEvaluationCall
工作。我用我的假设更新了最新的代码。代码对我来说运行良好,但我想检查这是否是您在上面的评论中建议的。看起来您需要从转换模块中提取cts:uris()查询逻辑,并将其应用到QueryBatcher中,然后使用转换模块转换每个选定的文档(它希望从content参数转换单个文档。
public static void main(String[] args) {
// TODO Auto-generated method stub
DatabaseClient client = DatabaseClientFactory.newClient
("localhost", pwd, "x", "x", DatabaseClientFactory.Authentication.DIGEST);
ServerTransform txform = new ServerTransform("tsm");
QueryManager qm = client.newQueryManager();
StructuredQueryBuilder query = qm.newStructuredQueryBuilder();
query.collection();
DataMovementManager dmm = client.newDataMovementManager();
QueryBatcher batcher = dmm.newQueryBatcher(query);
batcher.withBatchSize(5)
.withThreadCount(3)
.withConsistentSnapshot()
.onUrisReady(
new ApplyTransformListener().withTransform(txform))
.onBatchSuccess(batch-> {
System.out.println(
batch.getTimestamp().getTime() +
" documents written: " +
batch.getJobWritesSoFar());
})
.onBatchFailure((batch,throwable) -> {
throwable.printStackTrace();
});
// start the job and feed input to the batcher
dmm.startJob(batcher);
batcher.awaitCompletion();
dmm.stopJob(batcher);
client.release();
}
function harmonize(context, params, content)
{
var transformed = {};
transformed.Metadata = { "Source" : "International"};
transformed.Canonical= {"Future" : "Element"};
transformed.Source = content;
xdmp.documentInsert(fn.concat("/transformed", fn.baseUri(content)), transformed, {collections : "transform"});
};
exports.transform = harmonize;