2014-10-06 75 views
2

我正在做一個Web服務調用和檢索一個JSON有效載荷,例如:JSON有效載荷攝取到MySQL數據庫使用騾子ESB

[ 
    { 
     "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數據庫表。我期望第一個值'type -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,但在表中爲空。

任何幫助,非常感謝。

我下面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> 
+1

對不起David..Just學習論壇。我已經完成了我之前的問題。謝謝你的幫助。 – sam 2014-10-06 16:29:11

回答

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> 

很奇怪,因爲只有值返回,剩餘值爲空.. 但當我刪除類型從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":作爲所有值的列表

因此...建議ED的解決方案是使用以下格式: - 通過保持「類型」

[ 

    { 
    "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" 
     } 
    ] 

..你會期望得到所有的值..

+0

感謝Anirban ...這是來自服務的響應JSON負載。無論如何,我可以忽略它在騾子? – sam 2014-10-06 16:32:31

+0

你可以忽略和使用你現在使用的Mule表達式在Mule中使用任何你喜歡的值。並且如果你能夠像我提到的那樣格式化JSON,你可以得到你想要的結果......不管怎樣,如果你發現回答有用..請接受答案:) – 2014-10-06 16:40:12

+0

謝謝Anirban ...什麼將是Mule表達式忽略JSON有效載荷的根節點?有沒有忽略#[payload.type [0}]? – sam 2014-10-06 16:50:40