2012-12-23 97 views
1

我有一個JSON文件作爲下同時使用騾子ESB,騾工作室

[ 
    { "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數據庫插入數據的PostgreSQL如何正確地從JSON文件中讀取數據。我的配置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 &quot;tblTest&quot;(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列,而要首先分析它,然後想將其存儲在ie類別,作者,標題,價格各個表列。

我需要爲配置文件做些什麼改變,我該如何處理?

感謝

回答

2

變換JSON數據到一個java.util.List然後將其分成幾個java.util.Map S和他們每個人寫的DB。

<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 &quot;tblTest&quot;(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>