使用Mule ESB将JSON Payload摄入MySQL数据库

时间:2014-10-06 06:28:03

标签: mysql json mule esb

我正在进行网络服务调用并检索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数据库表中。我希望第一个值'输入-t00'与incident_number,经度,纬度插入为空。但是在每次后续迭代中,我都希望摄取能够正常工作。相反,我得到以下内容:

    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有效负载中正确地摄取了类型,但每个其他有效负载值都为null。在调试时,我确实看到了迭代,并且可以清楚地识别payload.incident_number,payload.longitude,但在我看来问题是被摄取的。我尝试仅摄取incident_number,但在表中显示为null。

非常感谢任何帮助。

我的配置文件如下:

       <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>

1 个答案:

答案 0 :(得分:0)

我尝试使用以下<foreach collection="#[message.payload]" doc:name="For Each">: -

     <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>

奇怪的是,只返回类型值并且剩余值为null。 但当我从json中删除类型时,如下所示: -

[

     {
    "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” ..您将获得所有预期的值。