如何在Mule Dataweave中连接flowVar和Json有效负载

时间:2018-05-02 15:47:14

标签: mule dataweave transformer

传入的有效载荷:

This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'TkAgg' by the following code:
  File "Desktop/object.py", line 11, in <module>
    from matplotlib import pyplot as plt
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 71, in <module>
    from matplotlib.backends import pylab_setup
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/__init__.py", line 16, in <module>
    line for line in traceback.format_stack()


  import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements
Traceback (most recent call last):
  File "Desktop/object.py", line 86, in <module>
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
  File "/home/wdjpng/.local/lib/python2.7/site-packages/tensorflow/models/research/object_detection/utils/label_map_util.py", line 132, in load_labelmap
    label_map_string = fid.read()
  File "/home/wdjpng/.local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 120, in read
    self._preread_check()
  File "/home/wdjpng/.local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 80, in _preread_check
    compat.as_bytes(self.__name), 1024 * 512, status)
  File "/home/wdjpng/.local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: data/mscoco_label_map.pbtxt; No such file or directory

flowVar值是一个ArrayList:([84hkj569],[6j93hl9])

所需的输出有效载荷:

{
    "Categories": [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        }, {
            "ID": "89k873c8",
            "Code": "CTY"
        }
    ]
}

我在数据编织中找不到办法, 你能帮忙吗

2 个答案:

答案 0 :(得分:0)

以下数据编码代码可以为您提供所需内容:

%dw 1.0
%output application/json
---
{
    Categories: payload.Categories ++ (flowVars.value map {
        "ID": $,
        "Code": "CTY"
    })
}

以下是我使用的示例流程的配置和输出:

    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    "Categories": [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        }, {
            "ID": "89k873c8",
            "Code": "CTY"
        }
    ]
}]]></dw:set-payload>
        </dw:transform-message>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"><![CDATA[
flowVars.value = new java.util.ArrayList();
flowVars.value.add("84hkj569");
flowVars.value.add("6j93hl9");
return payload;
]]></scripting:script>
        </scripting:component>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    Categories: payload.Categories ++ (flowVars.value map {
        "ID": $,
        "Code": "CTY"
    })
}]]></dw:set-payload>
        </dw:transform-message>

输出:

{
    "Categories": [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        },
        {
            "ID": "89k873c8",
            "Code": "CTY"
        },
        {
            "ID": "84hkj569",
            "Code": "CTY"
        },
        {
            "ID": "6j93hl9",
            "Code": "CTY"
        }
    ]
}

答案 1 :(得分:0)

Brad Cooper用于连接JSON有效负载和flowVars.value的数据编码代码没有任何问题。以下是适用于 3.8.1 的完整示例。

完整代码:     

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">

    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

    <flow name="sample-dataweaveFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/sample" doc:name="HTTP"/>
        <logger message="Request Received" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-variable variableName="value"><![CDATA[%dw 1.0
%output application/java
---
['84hkj569', '6j93hl9']]]></dw:set-variable>
        </dw:transform-message>
        <logger message="Flow Variable: #[flowVars.value]" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    Categories: [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        }, {
            "ID": "89k873c8",
            "Code": "CTY"
        }
    ]
}]]></dw:set-payload>
        </dw:transform-message>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="Payload: #[payload]" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    Categories: payload.Categories ++ (flowVars.value map {
        ID: $,
        Code: 'CTY' 
    })
}]]></dw:set-payload>
        </dw:transform-message>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="Final Response: #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

TEST网址:

http://localhost:8081/sample

控制台输出:

INFO  2018-05-09 01:08:51,575 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Request Received
INFO  2018-05-09 01:08:51,576 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java.
INFO  2018-05-09 01:08:51,595 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Flow Variable: [84hkj569, 6j93hl9]
INFO  2018-05-09 01:08:51,596 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java.
INFO  2018-05-09 01:08:51,620 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Payload: {
  "Categories": [
    {
      "ID": "5a873ca3",
      "Code": "CTY"
    },
    {
      "ID": "89k873c8",
      "Code": "CTY"
    }
  ]
}
INFO  2018-05-09 01:08:51,635 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Final Response: {
  "Categories": [
    {
      "ID": "5a873ca3",
      "Code": "CTY"
    },
    {
      "ID": "89k873c8",
      "Code": "CTY"
    },
    {
      "ID": "84hkj569",
      "Code": "CTY"
    },
    {
      "ID": "6j93hl9",
      "Code": "CTY"
    }
  ]
}