如何在 Mule 中动态插入 json 数据到数据库

How to dynamically insert json data into database in Mule

您好,我有一个 json 文件如下,

JSON:

[
    {
        "name": "Steve",
        "salary": "00",
        "age": "26"
    },
    {
        "name': "Laura",
        "salary': "00",
        "age": "28"
    },
    {
        "name': "Jack",
        "salary": "00",
        "age": "30"
    }
]

而且我还有一个数据库 table 调用 Employee,它有名称、年龄和薪水三列。这里我想处理 Json 数据并将其存储到 Employee table 中。那么我怎样才能在 mule 中做到这一点呢?

像这样..

  1. Set Your Data into payload
  2. convert json to Object list
  3. loop them using for
  4. save each object into DB

有关详细信息,请参阅 mule 文档例如:https://docs.mulesoft.com/mule-user-guide/v/3.7/native-support-for-json

    <flow name="testFlow">

        <!--set json data as payload>
        <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"/>
         <!--loop list-->
       <!-- save each record-->
        <jdbc:query key="insert"
                value="INSERT INTO ${db.env}.YOUR_TABLE  (COLUMN1, COLUMN2) 
                VALUES (#[message.payload.getName()] , #[message.payload.getSurname()] )" />
    </flow>

通过以下方式创建您的 mule 流:

  1. 文件连接器 - 您将在其中阅读 JSON 格式文件
  2. 将该有效载荷放入 forEach
  3. 遍历有效负载并提取每条记录
  4. 将数据一一插入table。

首先你的json请求无效,你需要输入"而不是' 在 json 键中。因此,有效的 json 将是:-

[
    {
        "name": "Steve",
        "salary": "00",
        "age": "26"
    },
    {
        "name": "Laura",
        "salary": "00",
        "age": "28"
    },
    {
        "name": "Jack",
        "salary": "00",
        "age": "30"
    }
]   

您需要传递来自 POSTMAN 浏览器客户端的请求:-

现在,您可以使用以下 Mule 流程插入到您的数据库中:-

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"></http:listener-config>

    <flow name="Flow"> 
        <http:listener config-ref="HTTP_Listener_Configuration" path="/set" doc:name="HTTP"></http:listener>  
        <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="Generic_Database_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[INSERT into table1 (NAME, SALARY, AGE) VALUES (#[message.payload.name],#[message.payload.salary],#[message.payload.age]);]]></db:parameterized-query>
            </db:insert>
        </foreach>  
       <set-payload value="Inserted" doc:name="Set Payload"/>  
    </flow>

并且记得设计您的数据库 table,其中您的 salaryage 将是 String 当你经过

"salary": "00",
"age": "26"  

此处为 String

首先,您在此处提供的 JSON 数据无效,如 Anirban 所说,请确保整数数据值(即此处的年龄)不要用双引号引起来。

您也可以使用 Dataweave 转换器,因为大多数 mule 开发人员更喜欢使用它来转换 mule 消息而不是 json-to-object 转换器。显然 json-to-object transformer 也可以正常工作,但现在大多数开发人员都在使用 Dataweave。

Config xml 文件如下:-

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

<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.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.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" connectionIdleTimeout="3000000" doc:name="HTTP Listener Configuration"/>
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" database="provide your schema/database name here" doc:name="MySQL Configuration" password="provide username here" user="provide password here"/>


    <flow name="muleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/postEmp" allowedMethods="POST" doc:name="HTTP"/>
        <dw:transform-message metadata:id="35bfe913-8de7-4b3c-9ba9-98f375a2873e" doc:name="Transform Message">
            <dw:input-payload mimeType="application/json"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload map ((payload01 , indexOfPayload01) -> {
    id: payload01.id,
    name: payload01.name,
    designation: payload01.designation,
    salary: payload01.Salary
})]]></dw:set-payload>
        </dw:transform-message>
        <foreach doc:name="For Each">
            <db:insert config-ref="MySQL_Configuration" doc:name="Database-Insert">
                <db:parameterized-query><![CDATA[insert into fulltime_employee values (#[payload.id], #[payload.name], #[payload.designation], #[payload.salary])]]></db:parameterized-query>
            </db:insert>
        </foreach>
    </flow>

</mule>

当您使用 Transform 消息转换器时,它会要求提供源和目标元数据。这里源元数据将是 json 列表,目标元数据将是地图列表。要设置源元数据,只需单击源部分的添加元数据蓝色 link,将打开一个新的弹出窗口 window,然后从此处通过单击绿色加号添加新的元数据并给出名称和将其类型设置为 JSON 并从下面的下拉列表中设置示例并浏览您的 json 文件,它将自动填充元数据道具。对于目标元数据,选择 MAP 作为类型并通过单击下面的绿色符号添加姓名、薪水和年龄道具 box.Remember 此属性名称应与您提到的数据库列名称相同。