2016-08-12 8 views
0

私はいくつかのPOCを実行したいと思います。これは残りのAPIからデータを取得しました。私はJson形式でデータを取得してから、1つのテキストファイルに入れます。今私はそのファイルのデータをDBにしようとします。以下に、MuleのXML形式のフローを示します。このデータをMule esbを使用してDBに挿入

[{ 
    "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

私はMuleも学んでいて、数千もの質問をしていますが、私はあなたに答えてもいいと思います。

フローにはいくつかのフローがあります。あなたは一度にあまりにも多くの質問をする。

はここにあなたの流れである enter image description here

まず問題は、我々は3つのエントリポイントを持っているということです(私はコメントのためのHTTP用の1/2を加えました)。 2つのHTTPと1つのファイル。 HTTP1は1回のhttpコールを待機します。 HTTP2は別のものを待っていて、実際にデータを取得しようとしているファイルコンポーネントがあるので、何もしません。

不要なHTTPを取り除き、ローカルディレクトリからファイルを取得するためにFileを再構成しましょう。 Yoは、HTTPからファイルを取得するために、フローの別の部分を追加することができます(必要ではないファイルに格納して)。 DBにデータを挿入している質問トピックには合わないので、これをスキップします。

設定で2番目の問題は、JSONをマップにマップすることです。あなたは配列を持っています。そこで、配列にマップしてみましょう。コードを参照してください。

第3の問題は、1つのレコードで動作するが、Arryで多くのレコードを扱うDatabase Conectorがあることです。ですから、配列のForEachループを使用してレコードを1つずつ処理してみましょう。

これが動作することを示すために、私はいくつかのLoggerを追加しました。ここで

は自明の流れ 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> 

変更

JDBCのような本当のURLにlocalDB接続です:のSQLServer:// myserverの:1433; databaseName = myDB;ユーザー= myid;パスワード= mypassword;

あなたはいいです。

アプリケーションを実行します。

アプリケーションは、ファイルをつかむ(次のいずれかの準備ができてファイルを削除します)文字列に変換し、オブジェクトの配列に文字列を変換し、ためになります\ TMP \ TEXT.TXT:

は、cにデータファイルをコピーします。各オブジェクト(あなたの行)はそれを記録し、2つのフィールドを記録し、レコードを格納し、成功のログメッセージを作成します。

次に、アプリケーションは、別のtext.txtが続行するのを待ちます。

もちろん、これらの手順はすべて必要ではありませんが、何が起こっているのか、いつ、何が起こっているのかを感じるのに役立ちます。

+0

ありがとうアレックス、私の質問はクリアされないかもしれませんが、あなたは私が望むものを理解しています。私はこのアプローチを試し、疑問があればお知らせします。 – venkat

+0

非常に感謝のアレックス。あなたが提案したように私はPOCを行った。もう一度あなたの素早い応答に感謝します。 – venkat

+0

これは、Muleを使用してデータベースに挿入する最も効率的なアプローチですか?なぜdataweave&mapをデータベースに使用しないのですか?何とかオブジェクトをループして1つ1つ挿入 – insaneyogi

関連する問題