2016-08-12 178 views
0

我想做一些POC,在此我從Rest api獲取數據。我以Json格式獲取數據,然後放入一個文本文件。現在我嘗試在數據庫中的文件數據。下面找到Mule流的XML格式。從服務這樣使用Mule將數據插入數據庫esb

[{ 
    "Address": "372 Willene Drive", 
    "CustomerID": 1010007031, 
    "DOB": "1981-09-19", 
    "FirstName": "Aaliyah", 
    "LastName": "Gonzalez", 
    "MiddleName": "R", 
    "PhoneNumber": "7775271592" 
}, { 
    "Address": null, 
    "CustomerID": 1010007743, 
    "DOB": "1937-05-28", 
    "FirstName": "Aaron", 
    "LastName": "Green", 
    "MiddleName": "T", 
    "PhoneNumber": "0924758727" 
}, { 
    "Address": "7 Country Lake Drive", 
    "CustomerID": 1010004653, 
    "DOB": "1936-03-07", 
    "FirstName": "Aaron", 
    "LastName": "Gutierrez", 
    "MiddleName": "Q", 
    "PhoneNumber": "9919500942" 
}, { 
    "Address": "157 Tamir Avenue", 
    "CustomerID": 1010005851, 
    "DOB": "1955-12-19", 
    "FirstName": "Abigail", 
    "LastName": "Garcia", 
    "MiddleName": "G", 
    "PhoneNumber": "4695049914" 
}, { 
    "Address": "5 Cross Road", 
    "CustomerID": 1010007962, 
    "DOB": "1939-07-23", 
    "FirstName": "Abigail", 
    "LastName": "Gomez", 
    "MiddleName": "R", 
    "PhoneNumber": "6267010014" 
}] 

<?xml version="1.0" encoding="UTF-8"?> 

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:db="http://www.mulesoft.org/schema/mule/db" 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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd"> 
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8082" doc:name="HTTP Listener Configuration"/> 
    <http:request-config name="HTTP_Request_Configuration" host="$host" port="$port" doc:name="HTTP Request Configuration"/> 
    <db:generic-config name="Generic_Database_Configuration" url="localDB Connection" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" doc:name="Generic Database Configuration"/> 
    <file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/> 

    <flow name="Flowname"> 
     <http:listener config-ref="HTTP_Listener_Configuration" path="/Customer" doc:name="HTTP" /> 
     <http:request config-ref="HTTP_Request_Configuration" path="/Services/Customers/api/2.0/search/{a}" method="GET" doc:name="HTTP"> 
      <http:request-builder> 
       <http:uri-param paramName="a" value="s"/> 
      </http:request-builder> 
     </http:request> 
     <file:outbound-endpoint path="D:\Docs" outputPattern="test.txt" responseTimeout="10000" doc:name="File"/> 
     <json:json-to-object-transformer returnClass="java.util.HashMap" mimeType="text/plain" doc:name="JSON to Object"/> 
     <logger message="#[message.payload]" level="INFO" doc:name="Logger"/> 
     <set-payload value="#[message.payload.CustomerID],#[message.payload.Address],#[message.payload.DOB],#[message.payload.FirstName],#[message.payload.LastName],#[message.payload.MiddleName],#[message.payload.PhoneNo]" doc:name="Set Payload"/> 
     <db:insert config-ref="Generic_Database_Configuration" doc:name="Database"> 
      <db:parameterized-query><![CDATA[INSERT INTO dbo.tblCustomer (Customerid,Address,Dob,Firstname,LastName,Middlename,Phoneno) VALUES (#[Message.payload.CustomerID],#[Message.payload.Address],#[Message.payload.DOB],#[Message.payload.FirstName],#[Message.payload.LastName],#[Message.payload.MiddleName],#[Message.payload.PhoneNo])]]></db:parameterized-query> 
     </db:insert> 
    </flow> 
</mule> 

獲取數據能否請您就這方面的幫助。

回答

0

我只是學習騾,也有成千上萬的問題,但我想我可以回答你的問題。

您的流量中有一些流量。你一次提出太多問題。

這裏是你的流量(我加了1/2的HTTP徵求意見) enter image description here

第一個問題是,我們有3個入口點。 2個HTTP和1個文件。 HTTP1等待一個http呼叫。 HTTP2等待另一個,因爲我們有文件組件,它實際上假設要獲取數據,所以它們什麼都不做。

讓我們擺脫不必要的HTTP並重新配置文件以從本地目錄中獲取某個文件。喲可以添加另一部分的流從HTTP抓取文件(存儲在不需要的文件中),然後使用它。我跳過這個,因爲它不適合將數據插入數據庫的問題主題。

配置中的第二個問題是您將JSON映射到Map。你有陣列。所以,讓我們將其映射到數組。看代碼。

第三個問題是,你有數據庫連接器與一個記錄一起工作,但你在Arry中有很多。所以,讓我們逐一使用ForEach循環來處理數組和處理記錄。

爲了顯示它的工作原理,我添加了幾個記錄器。

這是自我解釋的流動 enter image description here

這裏是代碼

<?xml version="1.0" encoding="UTF-8"?> 

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:db="http://www.mulesoft.org/schema/mule/db" 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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd"> 
    <db:generic-config name="Generic_Database_Configuration" url="localDB Connection" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" doc:name="Generic Database Configuration"/> 
    <file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/> 

    <flow name="Flowname"> 
     <file:inbound-endpoint path="C:\tmp" responseTimeout="10000" doc:name="FileIn"> 
      <file:filename-regex-filter pattern="text.txt" caseSensitive="true"/> 
     </file:inbound-endpoint> 

     <file:file-to-string-transformer doc:name="File to String"/> 

     <json:json-to-object-transformer returnClass="java.util.ArrayList" mimeType="text/plain" doc:name="JSON to Object"/> 
     <logger message="#[payload]" level="INFO" doc:name="Log Whole Payload"/> 
     <foreach doc:name="For Each"> 
      <logger message="#[payload]" level="INFO" doc:name="Log one Record"/> 
      <logger message="#[payload.Address] #[payload.CustomerID]" level="INFO" doc:name="Log two fields"/> 
      <db:insert config-ref="Generic_Database_Configuration" doc:name="Database"> 
      <db:parameterized-query><![CDATA[INSERT INTO dbo.tblCustomer (Customerid,Address,Dob,Firstname,LastName,Middlename,Phoneno) VALUES (#[Message.payload.CustomerID],#[Message.payload.Address],#[Message.payload.DOB],#[Message.payload.FirstName],#[Message.payload.LastName],#[Message.payload.MiddleName],#[Message.payload.PhoneNo])]]></db:parameterized-query> 

     </db:insert> 
      <logger message="Record succesfull" level="INFO" doc:name="Log Success"/> 

     </foreach> 

    </flow> 
</mule> 

變化的LocalDB連接真實的URL像

的jdbc:SQLSERVER:// MYSERVER:1433;的databaseName = MYDB;用戶=本身份識別碼;密碼=輸入mypassword;

你很好走。

運行應用程序。

您的數據文件複製到c:\ tmp目錄\的text.txt

應用會搶了文件,轉換成字符串(刪除文件以備下一個),將字符串轉換爲對象和數組每個對象(這是你的行)記錄它,記錄兩個字段,存儲記錄並使成功的日誌消息。

然後應用程序將等待另一個text.txt繼續。

當然,所有這些步驟都不是必需的,但它們可以幫助您感受發生了什麼,什麼時候發生了什麼。

+0

謝謝亞歷克斯,我的問題可能不清楚,但你明白我想要的。我會嘗試這種方法,並讓你知道是否有任何疑問。 – venkat

+0

非常感謝Alex。按照您的建議,我完成了POC。再次感謝您的快速響應。 – venkat

+0

這是使用Mule插入數據庫的最有效方法嗎?爲什麼不使用dataweave和映射到數據庫?以某種方式不通過循環通過對象和做插入一個接一個 – insaneyogi