【问题标题】:JSON Payload ingested into MySQL database using Mule ESB使用 Mule ESB 将 JSON 有效负载摄取到 MySQL 数据库中
【发布时间】:2014-11-30 09:36:42
【问题描述】:

我正在进行网络服务调用并检索 json 有效负载:

   [
     {
       "type": " --T::00"
     },
     {
    "address": "10049 College Way N",
    "longitude": "-122.335022",
    "latitude": "47.701756",
    "incident_number": "F110104009",
    "type": "Aid Response",
    "report_location": {
        "needs_recoding": false,
        "longitude": "-122.335022",
        "latitude": "47.701756"
      }
      },
      {
    "address": "5929 Beach Dr Sw",
    "longitude": "-122.397816",
    "latitude": "47.550431",
    "incident_number": "F110104008",
    "type": "Aid Response",
    "report_location": {
        "needs_recoding": false,
        "longitude": "-122.397816",
        "latitude": "47.550431"
     }
     }

我正在遍历每一行并将键值提取到 MySQl DB 表中。我希望插入第一个值“type -t00”,事件编号、经度、纬度为空白。但是,在随后的每次迭代中,我都希望摄取能够正常工作。相反,我得到以下信息:

    ID Longitude Latitude incident_number type      
    1     null        null    null      --T::00
    65    null        null    null      1RED 1 Unit
    70    null        null    null      1RED 1 Unit
    313   null        null    null      1RED 1 Unit
    314   null        null    null      1RED 1 Unit
    315   null        null    null      1RED 1 Unit
    316   null        null    null      1RED 1 Unit

发生的情况是只有类型从 JSON 有效负载中正确摄取,但其他所有有效负载值均为空。当我调试时,我确实看到了迭代并且可以清楚地识别payload.incident_number、payload.longitude,但在我看来,问题已经被吸收了。我尝试只摄取 event_number,但它在表中显示为 null。

非常感谢任何帮助。

下面是我的configuration.xml:

       <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="demo"              
        password="Welcome1" database="demo" doc:name="MySQL Configuration"/>

  <flow name="seattleemergencyFlow1" doc:name="seattleemergencyFlow1"> 
       <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8900"  
       path="get-emergency" doc:name="HTTP"></http:inbound-endpoint>  

      <http:outbound-endpoint exchange-pattern="request-response" host="data.seattle.gov"   
       port="80" path="resource/kzjm-xkqj.json?" method="GET" contentType="application/json" 
       doc:name="HTTP"></http:outbound-endpoint>  

      <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"> 
      </json:json-to-object-transformer>

       <foreach doc:name="For Each">
        <db:insert config-ref="MySQL_Configuration" doc:name="Database">
            <db:parameterized-query><![CDATA[INSERT INTO emergencyrecords
       (incident_number, type, longitude)
         VALUES
       ( 
         #[payload.incident_number],
         #[payload.type],
         #[payload.longitude])]]></db:parameterized-query>
        </db:insert>
       </foreach>

      </flow>

     </mule>

【问题讨论】:

  • 对不起大卫..刚学习论坛。我已经完成了我之前的问题。感谢您的帮助。

标签: mysql json mule esb


【解决方案1】:

我尝试使用&lt;foreach collection="#[message.payload]" doc:name="For Each"&gt; 如下:-

     <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="demo"              
        password="Welcome1" database="demo" doc:name="MySQL Configuration"/>

  <flow name="seattleemergencyFlow1" doc:name="seattleemergencyFlow1"> 
       <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8900"  
       path="get-emergency" doc:name="HTTP"></http:inbound-endpoint>  

      <http:outbound-endpoint exchange-pattern="request-response" host="data.seattle.gov"   
       port="80" path="resource/kzjm-xkqj.json?" method="GET" contentType="application/json" 
       doc:name="HTTP"></http:outbound-endpoint>  

      <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"> 
      </json:json-to-object-transformer>

        <foreach collection="#[message.payload]" doc:name="For Each">
        <db:insert config-ref="MySQL_Configuration" doc:name="Database">
            <db:parameterized-query><![CDATA[INSERT INTO emergencyrecords
       (incident_number, type, longitude)
         VALUES
       ( 
         #[payload.incident_number],
         #[payload.type],
         #[payload.longitude])]]></db:parameterized-query>
        </db:insert>
       </foreach>

      </flow>

     </mule>

这很奇怪,因为只返回了 type 值,而剩余的值为 null .. 但是当我从 json 中删除 type 时,如下所示:-

[

     {
    "address": "10049 College Way N",
    "longitude": "-122.335022",
    "latitude": "47.701756",
    "incident_number": "F110104009",
    "type": "Aid Response",
    "report_location": {
        "needs_recoding": false,
        "longitude": "-122.335022",
        "latitude": "47.701756"
      }
      },
      {
    "address": "5929 Beach Dr Sw",
    "longitude": "-122.397816",
    "latitude": "47.550431",
    "incident_number": "F110104008",
    "type": "Aid Response",
    "report_location": {
        "needs_recoding": false,
        "longitude": "-122.397816",
        "latitude": "47.550431"
     }
     }
  ]

我得到了payload.incident_numberpayload.longitude 等的所有值..

可能是因为

    {
       "type": " --T::00"
     }   

出现在 JSON 的 开头,它仅将 "type": 的值作为所有值的列表

所以...建议的解决方案是使用以下格式:-

[

     {
    "address": "10049 College Way N",
    "longitude": "-122.335022",
    "latitude": "47.701756",
    "incident_number": "F110104009",
    "type": "Aid Response",
    "report_location": {
        "needs_recoding": false,
        "longitude": "-122.335022",
        "latitude": "47.701756"
      }
      },
      {
    "address": "5929 Beach Dr Sw",
    "longitude": "-122.397816",
    "latitude": "47.550431",
    "incident_number": "F110104008",
    "type": "Aid Response",
    "report_location": {
        "needs_recoding": false,
        "longitude": "-122.397816",
        "latitude": "47.550431"
     }
     },

      {
           "type": " --T::00"
       }   
  ]

通过在末尾保留 "type" .. 您将按预期获得所有值 ..

【讨论】:

  • 感谢 Anirban...这是来自服务的响应 JSON 有效负载。无论如何我可以在 Mule 中忽略它?
  • 您可以使用您现在使用的 Mule 表达式在 Mule 中忽略并获取任何您喜欢的值。如果您能够像我提到的那样格式化 JSON,您可以获得预期的结果。 ..无论如何,如果您发现答案有用..请接受答案:)
  • 谢谢 Anirban...Mule 表达式忽略 JSON 有效负载的根节点会是什么?是否有忽略 #[payload.type[0}]?
  • 感谢您的回答,但我仍然有同样的问题。如何按照您的建议忽略根音并在末尾发布“类型”?我无法控制从网络服务发送给我的响应
猜你喜欢
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
相关资源
最近更新 更多