如何在使用Mule ESB、Mule Studio将数据插入PostgreSQL时正确读取JSON文件中的数据
我有一个JSON文件,如下所示如何在使用Mule ESB、Mule Studio将数据插入PostgreSQL时正确读取JSON文件中的数据,mule,mule-studio,Mule,Mule Studio,我有一个JSON文件,如下所示 [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }
[
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
]
我正在尝试读取文件并将数据存储到PostgreSQL数据库中。我的配置xml文件如下所示
<jdbc:postgresql-data-source name="PostgreSQL_Data_Source" user="superuser" password="pwd" url="jdbc:postgresql://localhost:5432/TestDB" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>
<jdbc:connector name="Database-Connector" dataSource-ref="PostgreSQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database">
<jdbc:query key="InsertQuery" value="INSERT INTO "tblTest"(category,author,title,price)VALUES (?,?,?,?)"/>
</jdbc:connector>
<flow name="testxmlFlow1" doc:name="testxmlFlow1">
<file:inbound-endpoint path="C:\InputFolder" responseTimeout="10000" doc:name="File"/>
<byte-array-to-string-transformer doc:name="Byte-Array-to-String"/>
<jdbc:outbound-endpoint exchange-pattern="one-way" queryKey="InsertQuery" queryTimeout="-1" connector-ref="Database-Connector" doc:name="Database"/>
</flow>
我并不是想把它存储在一个完整的JSON列中,而是想先解析它,然后将它存储在各个表列中,即category、author、title、price
我需要在配置文件中对此进行哪些更改,以及如何处理这些更改
谢谢将JSON数据转换为
java.util.List
然后将其拆分为几个java.util.Map
s,并将它们分别写入数据库
<jdbc:postgresql-data-source name="PostgreSQL_Data_Source"
user="superuser" password="pwd" url="jdbc:postgresql://localhost:5432/TestDB"
transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source" />
<jdbc:connector name="Database-Connector" dataSource-ref="PostgreSQL_Data_Source"
validateConnections="true" queryTimeout="-1" pollingFrequency="0"
doc:name="Database">
<jdbc:query key="InsertQuery"
value="INSERT INTO "tblTest"(category,author,title,price)VALUES (#[message.payload.category],#[message.payload.author],#[message.payload.title],#[message.payload.price])" />
</jdbc:connector>
<flow name="testxmlFlow1" doc:name="testxmlFlow1">
<file:inbound-endpoint path="C:\InputFolder"
responseTimeout="10000" doc:name="File" />
<json:json-to-object-transformer
returnClass="java.util.List" doc:name="JSON to List" />
<collection-splitter />
<jdbc:outbound-endpoint exchange-pattern="one-way"
queryKey="InsertQuery" queryTimeout="-1" connector-ref="Database-Connector"
doc:name="Database" />
</flow>