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

时间:2019-03-27 11:10:47

标签: marklogic transformation marklogic-8 marklogic-9

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

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

它返回文档的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])

1 个答案:

答案 0 :(得分:1)

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

如果需要可变结构,请尝试使用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))

希望有帮助,