使用rest webservice并将内容保存到mule中的数据库中

时间:2014-07-27 10:31:36

标签: mule

我点击了一个休息的webservice并获得了一个JSON对象,如下所示:

{
  "version" : "1.0.1",
  "observations" : [ {
    "id" : 2743,
    "sensor" : "RedBus-11",
    "privacy" : "public",
    "record" : [ {
      "output" : [ {
        "name" : "temperature",
        "value" : "35.0",
        "type" : "DECIMAL"
      } ],
      "starttime" : "06-JUN-2014 11:09:39 IST",
      "position-global" : {
        "latitude" : "19.066680908203125",
        "longitude" : "72.86734008789062",
        "altitude" : "2m"
      }
    } ]
  }]
}

在将JSON转换为对象时,我得到一个链接的HashMap。我需要将链接散列图中的内容保存为高度,起始时间,纬度到MySQL数据库,但我只能在数据库中保留链接散列映射的值。请求任何人提供帮助。

我的配置文件如下:

<jdbc-ee:mysql-data-source name="MySQL" user="user" password="password" url="jdbc:mysql://localhost:3306/TESTDB"    transactionIsolation="UNSPECIFIED" doc:name="MySQL Data Source"></jdbc-ee:mysql-data-source>
<jdbc-ee:connector name="MySQL_JDBC_Connector" dataSource-ref="MySQL" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database"> 
    <jdbc-ee:query key="InsertRecord" value="INSERT INTO TEMP(A1) VALUES(#[message.payload])"></jdbc-ee:query>  
</jdbc-ee:connector>

<flow name="MuleRESTExperimentFlow1" doc:name="MuleRESTExperimentFlow1">   
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"></http:inbound-endpoint>

    <http:rest-service-component httpMethod="GET" serviceUrl="REST Webservice"></http:rest-service-component>

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

    <expression-transformer expression="#[message.payload.observations]" doc:name="Expression"></expression-transformer>

    <jdbc-ee:outbound-endpoint exchange-pattern="one-way" queryKey="InsertRecord" queryTimeout="-1" connector-ref="MySQL_JDBC_Connector" doc:name="Destination"></jdbc-ee:outbound-endpoint> 

</flow>

请告知如何在骡子中实现这一目标。

2 个答案:

答案 0 :(得分:0)

不确定是什么阻止了你。

一旦你拥有Map有效载荷(事实上LinkedHashMap没有区别),你可以使用MEL表达式,如:

#[message.payload.observations[0].record[0].['position-global'].altitude]

读取字段值。

答案 1 :(得分:0)

JSON文件中存在一个小问题。 position-global 元素中包含 - ..所以我将元素更改为 positionglobal ..现在它正常工作..我可以使用MEL表达式单独插入每个元素值。还使用MEL打印控制台中每个元素值的值。现在所有元素值都可以使用MEL提取..唯一的问题是元素位置 - 全局中的 - ,如果从中删除 - ..它将提取 positionglobal 值...现在,您可以从此示例中提取并插入需要插入到DB中的任何值: -

<jdbc-ee:mysql-data-source name="MySQL" user="user" password="password" url="jdbc:mysql://localhost:3306/TESTDB"    transactionIsolation="UNSPECIFIED" doc:name="MySQL Data Source"></jdbc-ee:mysql-data-source>
    <jdbc-ee:connector name="MySQL_JDBC_Connector" dataSource-ref="MySQL" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database"> 
        <jdbc-ee:query key="InsertRecord" value="INSERT INTO TEMP(A1) VALUES(#[message.payload.observations[0].id],#[message.payload.observations[0].sensor],#[message.payload.observations[0].privacy],#[message.payload.observations[0].record[0].output[0].name],#[message.payload.observations[0].record[0].output[0].value],#[message.payload.observations[0].record[0].output[0].type],#[message.payload.observations[0].record[0].starttime],#[message.payload.observations[0].record[0].positionglobal.latitude],#[message.payload.observations[0].record[0].positionglobal.longitude],#[message.payload.observations[0].record[0].positionglobal.altitude])"></jdbc-ee:query>  
    </jdbc-ee:connector>

<flow name="MuleRESTExperimentFlow1" doc:name="MuleRESTExperimentFlow1">   
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"></http:inbound-endpoint>

<http:rest-service-component httpMethod="GET" serviceUrl="REST Webservice"></http:rest-service-component>

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

            <logger message="ID:- #[message.payload.observations[0].id]" level="INFO" doc:name="Logger"/>
            <logger message="SENSOR #[message.payload.observations[0].sensor]" level="INFO" doc:name="Logger"/>
            <logger message="PRIVACY #[message.payload.observations[0].privacy]" level="INFO" doc:name="Logger"/>
            <logger message="NAME #[message.payload.observations[0].record[0].output[0].name]" level="INFO" doc:name="Logger"/>
            <logger message="VALUE #[message.payload.observations[0].record[0].output[0].value]" level="INFO" doc:name="Logger"/>
            <logger message="TYPE #[message.payload.observations[0].record[0].output[0].type]" level="INFO" doc:name="Logger"/>        
            <logger message="STARTTIME #[message.payload.observations[0].record[0].starttime]" level="INFO" doc:name="Logger"/>       
            <logger message="LATITUDE #[message.payload.observations[0].record[0].positionglobal.latitude]" level="INFO" doc:name="Logger"/>        
            <logger message="LONGITUDE #[message.payload.observations[0].record[0].positionglobal.longitude]" level="INFO" doc:name="Logger"/>       
            <logger message="ALTITUDE #[message.payload.observations[0].record[0].positionglobal.altitude]" level="INFO" doc:name="Logger"/>        


<jdbc-ee:outbound-endpoint exchange-pattern="one-way" queryKey="InsertRecord" queryTimeout="-1" connector-ref="MySQL_JDBC_Connector" doc:name="Destination"></jdbc-ee:outbound-endpoint> 

    </flow>

更新了观察清单: -

 <jdbc-ee:connector name="Database_Global" dataSource-ref="DB_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database">

        <jdbc-ee:query key="InsertQuery" value="INSERT INTO TEMP(A1) VALUES(#[payload.id],#[payload.sensor],#[payload.privacy],#[payload.record[0].output[0].name],#[payload.record[0].output[0].value],#[payload.record[0].output[0].type],#[payload.record[0].starttime],#[payload.record[0].positionglobal.latitude],#[payload.record[0].positionglobal.longitude],#[payload.record[0].positionglobal.altitude])"/> 

    </jdbc-ee:connector>  

    <flow name="MuleRESTExperimentFlow1" doc:name="MuleRESTExperimentFlow1">   
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"></http:inbound-endpoint>

    <http:rest-service-component httpMethod="GET" serviceUrl="REST Webservice"></http:rest-service-component>

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

                <logger message="ID:- #[message.payload.observations[0].id]" level="INFO" doc:name="Logger"/>
                <logger message="SENSOR #[message.payload.observations[0].sensor]" level="INFO" doc:name="Logger"/>
                <logger message="PRIVACY #[message.payload.observations[0].privacy]" level="INFO" doc:name="Logger"/>
                <logger message="NAME #[message.payload.observations[0].record[0].output[0].name]" level="INFO" doc:name="Logger"/>
                <logger message="VALUE #[message.payload.observations[0].record[0].output[0].value]" level="INFO" doc:name="Logger"/>
                <logger message="TYPE #[message.payload.observations[0].record[0].output[0].type]" level="INFO" doc:name="Logger"/>        
                <logger message="STARTTIME #[message.payload.observations[0].record[0].starttime]" level="INFO" doc:name="Logger"/>       
                <logger message="LATITUDE #[message.payload.observations[0].record[0].positionglobal.latitude]" level="INFO" doc:name="Logger"/>        
                <logger message="LONGITUDE #[message.payload.observations[0].record[0].positionglobal.longitude]" level="INFO" doc:name="Logger"/>       
                <logger message="ALTITUDE #[message.payload.observations[0].record[0].positionglobal.altitude]" level="INFO" doc:name="Logger"/>        

    <foreach collection="#[message.payload.observations]" doc:name="For Each">                     
    <jdbc-ee:outbound-endpoint exchange-pattern="one-way" queryKey="InsertRecord" queryTimeout="-1" connector-ref="MySQL_JDBC_Connector" doc:name="Destination"></jdbc-ee:outbound-endpoint> 
       </foreach>   

        </flow>