使用 Mule esb 将数据插入数据库
Data insert into DB using Mule esb
我想做一些 POC,在此我从 Rest api 获取数据。我正在获取 Json 格式的数据,然后放入一个文本文件中。现在我尝试在数据库中保存该文件数据。下面找到 Mule 流的 XML 格式。
<?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>
像这样从服务获取数据
[{
"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"
}]
你能帮忙解决一下吗?
我也在学习 Mule,有成千上万的问题,但我想我可以回答你的问题。
您的流程中有一些流程。你一下子提的问题太多了。
这是你的流程(我为 HTTP 添加了 1/2 以供评论)
第一个问题是我们有 3 个入口点。 2 个 HTTP 和 1 个文件。
HTTP1 等待一个 http 调用。 HTTP2 等待另一个并且都什么都不做,因为我们有文件组件,它实际上应该获取数据。
让我们摆脱不必要的 HTTP 并重新配置文件以从本地目录中获取一些文件。你可以添加流程的另一部分以从 HTTP 抓取文件(存储在文件中,这不是必需的)然后使用它。我跳过这个,因为它不适合将数据插入数据库的问题主题。
您配置中的第二个问题是您将 JSON 映射到地图。你有数组。所以,让我们将它映射到数组。见代码。
第三个问题是,您的数据库连接器只能处理一条记录,但您在 Arry 中有很多。因此,让我们对 Array 使用 ForEach 循环并一条一条地处理记录。
为了证明它有效,我又添加了几个记录器。
这是不言自明的流程
这是代码
<?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 Connection 更改为真实的 url like
jdbc:sqlserver://myserver:1433;数据库名=myDB;用户=我的ID;密码=我的密码;
一切顺利。
运行申请。
将您的数据文件复制到 c:\tmp\text.txt
应用程序将抓取文件,转换为字符串(删除文件为下一个文件做好准备),将字符串转换为对象数组,并为每个对象(这是你的行)记录它,记录两个字段,存储记录并制作成功的日志消息。
然后应用程序将等待另一个 text.txt 继续。
当然,所有这些步骤都不是必需的,但它们可以帮助您感受正在发生的事情、发生的时间和发生的事情。
我想做一些 POC,在此我从 Rest api 获取数据。我正在获取 Json 格式的数据,然后放入一个文本文件中。现在我尝试在数据库中保存该文件数据。下面找到 Mule 流的 XML 格式。
<?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>
像这样从服务获取数据
[{
"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"
}]
你能帮忙解决一下吗?
我也在学习 Mule,有成千上万的问题,但我想我可以回答你的问题。
您的流程中有一些流程。你一下子提的问题太多了。
这是你的流程(我为 HTTP 添加了 1/2 以供评论)
第一个问题是我们有 3 个入口点。 2 个 HTTP 和 1 个文件。 HTTP1 等待一个 http 调用。 HTTP2 等待另一个并且都什么都不做,因为我们有文件组件,它实际上应该获取数据。
让我们摆脱不必要的 HTTP 并重新配置文件以从本地目录中获取一些文件。你可以添加流程的另一部分以从 HTTP 抓取文件(存储在文件中,这不是必需的)然后使用它。我跳过这个,因为它不适合将数据插入数据库的问题主题。
您配置中的第二个问题是您将 JSON 映射到地图。你有数组。所以,让我们将它映射到数组。见代码。
第三个问题是,您的数据库连接器只能处理一条记录,但您在 Arry 中有很多。因此,让我们对 Array 使用 ForEach 循环并一条一条地处理记录。
为了证明它有效,我又添加了几个记录器。
这是不言自明的流程
这是代码
<?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 Connection 更改为真实的 url like
jdbc:sqlserver://myserver:1433;数据库名=myDB;用户=我的ID;密码=我的密码;
一切顺利。
运行申请。
将您的数据文件复制到 c:\tmp\text.txt
应用程序将抓取文件,转换为字符串(删除文件为下一个文件做好准备),将字符串转换为对象数组,并为每个对象(这是你的行)记录它,记录两个字段,存储记录并制作成功的日志消息。
然后应用程序将等待另一个 text.txt 继续。
当然,所有这些步骤都不是必需的,但它们可以帮助您感受正在发生的事情、发生的时间和发生的事情。