如何从数据库中获取文档并将其传递给使用 dmsdk 进行转换?

How to fetch document from database and pass it to transformation using dmsdk?

我需要从数据库中获取文档并使用 dmsdk 应用转换。 我使用以下代码行成功地从数据库中获取了文档:

        QueryManager queryManager=client.newQueryManager();
        StructuredQueryBuilder sqb = queryManager.newStructuredQueryBuilder();
        StructuredQueryDefinition query =sqb.collection("test");

它 returns 文档的 URI。但是我的转换接受 json 个对象作为输入。 我需要传递 json 个对象而不是 uri。

我的转型:

xquery version "1.0-ml";

module namespace test =
  "http://marklogic.com/rest-api/transform/deepan";

declare function test:transform(
  $context as map:map,
  $params as map:map,
  $content as document-node()
) as document-node()
{
    let $jsoncont := xdmp:from-json-string($content)

    let $inputval := "fname,lname"
    let $orig-value := map:get($jsoncont, "value")

    let $jscode := "var simple = require('/wdsUtils.sjs');
                    var content, input;
                    simple.createUri(content,input);"

    let $uri := xdmp:javascript-eval($jscode,('content',$orig-value,'input',$inputval))
    let $_ := map:put($content, "uri",$uri)
    let $_ := map:put($content, "value",$orig-value)
    return $content  

};

我的dmsdk代码:

    static String HOST = "localhost";
    static int PORT = 8136;
    static String USER = "admin";
    static String PASSWORD = "admin";
    private static DatabaseClient client = 
            DatabaseClientFactory.newClient(
                HOST, PORT, new DigestAuthContext(USER, PASSWORD));

    public static void loadData(String txName)
    {
        QueryManager queryManager=client.newQueryManager();
        StructuredQueryBuilder sqb = queryManager.newStructuredQueryBuilder();
        StructuredQueryDefinition query =sqb.collection("test");
        DataMovementManager dmm = client.newDataMovementManager();
        QueryBatcher batcher = dmm.newQueryBatcher(query);

        batcher.withConsistentSnapshot();
        ServerTransform txform = new ServerTransform(txName);

        ApplyTransformListener transformListener = new ApplyTransformListener()
                .withTransform(txform)
                .withApplyResult(ApplyResult.REPLACE);

        batcher.onUrisReady(transformListener)          
                .onQueryFailure( exception -> exception.printStackTrace() );

        dmm.startJob(batcher);
    }

    public static void main(String[] args) 
    {
        loadData("deepan");
    }

异常:

01:09:10.819 [main] WARN com.marklogic.client.datamovement.ApplyTransformListener - Error: com.marklogic.client.FailedRequestException: Local message: failed to apply resource at internal/apply-transform: Bad Request. Server Message: XDMP-ARGTYPE: (err:XPTY0004) fn:doc(fn:doc("/one.json")) -- arg1 is not of type xs:string* in batch with urs ([/one.json, /three.json])
01:09:10.821 [pool-1-thread-2] DEBUG com.marklogic.client.impl.OkHttpServices - Query uris with structured query <query xmlns="http://marklogic.com/appservices/search" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:search="http://marklogic.com/appservices/search" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><collection-query><uri>test</uri></collection-query></query>
01:09:10.821 [pool-1-thread-2] DEBUG com.marklogic.client.impl.OkHttpServices - Getting internal/uris as text/uri-list
01:09:10.823 [pool-1-thread-1] DEBUG com.marklogic.client.impl.OkHttpServices - Query uris with structured query <query xmlns="http://marklogic.com/appservices/search" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:search="http://marklogic.com/appservices/search" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><collection-query><uri>test</uri></collection-query></query>
01:09:10.824 [pool-1-thread-1] DEBUG com.marklogic.client.impl.OkHttpServices - Getting internal/uris as text/uri-list
01:09:10.830 [pool-1-thread-2] DEBUG com.marklogic.client.impl.OkHttpServices - Posting internal/apply-transform
01:09:10.852 [pool-1-thread-2] WARN com.marklogic.client.datamovement.ApplyTransformListener - Error: com.marklogic.client.FailedRequestException: Local message: failed to apply resource at internal/apply-transform: Bad Request. Server Message: XDMP-ARGTYPE: (err:XPTY0004) fn:doc(fn:doc("/two.json")) -- arg1 is not of type xs:string* in batch with urs ([/two.json])

xdmp:from-json-string() 需要一个字符串,但转换的 $content 参数是文档节点 (),而不是字符串。

尝试 xdmp:from-json() 而不是 xdmp:from-json-string() 将 JSON 节点转换为地图,如果你需要一个可变结构。

此外,我想知道 xdmp:javascript-eval() 是否有必要。您应该能够使用类似

的方式从 XQuery 调用该函数
let $uri := xdmp:apply(
    xdmp:function(xs:QName("createUri"), "/wdsUtils.sjs"),
    $orig-value,
    $inputval)

根据 createUri() 函数的作用,可能不需要将 $content JSON 节点转换为映射(在 JavaScript 中成为对象文字)。

map:put() 操作在节点上不起作用。相反,考虑将地图转换为 JSON 节点,其中包含

的内容
return xdmp:to-json(map:entry("uri",$uri)=>map:with("value",$orig-value))

希望对您有所帮助,