1. 程式人生 > >EnjoyingSoft之Mule ESB開發教程第六篇:Data Transform - 資料轉換

EnjoyingSoft之Mule ESB開發教程第六篇:Data Transform - 資料轉換

目錄

  • 1. 資料轉換概念
  • 2. 資料智慧感知 - DataSense
  • 3. 簡單資料轉換元件
    • 3.1 Object to JSON
    • 3.2 JSON to XML
    • 3.3 JSON to Object
    • 3.4 XML to JSON
  • 4. 企業版的DataWeave Transformer(視覺化高階資料轉換器)
  • 5. 社群版的InfoMapper(自研的視覺化資料轉換器)

本篇主要介紹在Mule ESB中使用資料轉換。資料轉換是ESB最核心的功能,它消除了異構應用之間的技術差異,讓不同的應用服務協調運作,實現了不同服務之間的通訊。資料轉換或者說訊息轉換,包括了資料結構,資料型別,資料內容的轉換等。

作為開源ESB產品中很成熟的平臺,Mule ESB內建很多的訊息轉換元件,比如Object to JSON,Object to XML,XML to JSON等,同時也支援使用自定義的Transformer來擴充自定義轉換邏輯。

另外Mule ESB企業版更擁有圖形化的訊息轉換元件DataWeave,可以很靈活的進行資料的轉換,包括一對一,一對多,多對一的對映。我們在社群版上也研發了相應的擴充套件元件InfoMapper,做到類似DataWeave的強大功能。

Mule ESB起源於一個社群專案,經過十多年的發展,Mule ESB擁有眾多的企業客戶案例,2017年成功在紐交所上市。我們作為MuleSoft的重要合作伙伴,參與其中,使用Mule ESB企業版實施開發,或者Mule ESB社群版實施開發,幫助國內眾多的行業領先客戶成功上線Mule ESB整合專案。

Mule ESB的原始碼託管在GitHub上,英文文件也非常豐富,Mule ESB的中文教程卻非常少,我們使用8篇Mule ESB的開發教程來幫助大家迅速上手Mule ESB開發。

  • EnjoyingSoft之Mule ESB開發教程第一篇:初識Mule ESB
  • EnjoyingSoft之Mule ESB開發教程第二篇:Mule ESB基本概念
  • EnjoyingSoft之Mule ESB開發教程第三篇:Mule message structure - Mule message結構
  • EnjoyingSoft之Mule ESB開發教程第四篇:Mule Expression Language - MEL表示式
  • EnjoyingSoft之Mule ESB開發教程第五篇:控制訊息的流向-資料路由
  • EnjoyingSoft之Mule ESB開發教程第六篇:Data Transform - 資料轉換
  • EnjoyingSoft之Mule ESB開發教程第七篇:SOAP Web Service的消費和建立
  • EnjoyingSoft之Mule ESB開發教程第八篇:使用API構建Rest Service

1. 資料轉換概念

資料轉換是ESB平臺的核心模組,是解決異構系統通訊的必備元件。舉個例子,訂單系統暴露了一個Web Service,而我們的網上商城生成的訂單是Json或者Flat file格式,將JSON資料或者Flat file文字格式轉換成Web Service的XML格式,這個需求就可以通過ESB來實現。我們在第三篇講解了Mule Message的結構,回憶一下Mule Message的結構。

我們通常說的資料轉換就是對Mule Message中Payload做操作,改變資料型別或者資料結構,資料內容等。在做資料轉換之前,我們需要確定資料的源型別和目標型別,然後選擇合適的資料轉換元件。

2. 資料智慧感知 - DataSense

DataSense是Mule最強大的功能之一,Anypoint Studio會主動獲取元資料型別和結構等資訊,以幫助開發者在應用程式中準確的對映或者使用資料。

DataSense配合企業版的DataWeave視覺化資料轉化元件,或者我們自研的社群版視覺化資料轉化元件InfoMapper,能夠很大的加速應用整合的開發,方便開發者完成資料轉換和對映。本文後半段會介紹DataWeave和InfoMapper的強大之處。

DataSense舉例說明,如果您的應用程式連線到一個天氣預報的Web Service,那麼DataSense會捕獲Web Service使用的資料型別和結構的資訊,這些資訊對開發者非常有用,它會展示這個Web Service期望的的入參是什麼,出參是什麼。

天氣預報WebService中規定的入參和出參如下圖:

Anypoint Studio中的Web Service Consumer元件,會清楚的顯示Expected Data,也就是期望的入引數據格式。如下圖Input頁籤,可以看到需要輸入theCityName的XML資料。

同時Web Service Consumer元件也會展示這個WebService的輸出引數是什麼。如下圖Output頁籤。

3. 簡單資料轉換元件

Mule ESB內建了很多常用的簡單的資料轉換元件,從元件名稱就可以看到元件的適用範圍,源資料型別和目標資料型別。另外Mule ESB也支援自定義Transformer,通過Java或者其他指令碼語言實現複雜的資料轉換邏輯。

3.1 Object to JSON

Object to JSON元件能夠將Java物件轉換成JSON格式,方便其他語言比如C#,Python解析和使用。常見的使用場景就是將Map物件轉換成JSON格式。

Mule UI Config:

Mule XML Config:

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
    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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="object-to-json-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-payload value="#[[&quot;orderNo&quot;:&quot;OST0001&quot;,&quot;orderAmount&quot;:1000]]" doc:name="Set Map Payload"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>

使用Postman訪問,就可以得到一個標準的JSON資料。

{
    "orderAmount": 1000,
    "orderNo": "OST0001"
}

3.2 JSON to XML

JSON轉換成XML是很常用的轉換操作。JSON和XML都是有層級關係的資料展示結構,JSON和XML具體的區別和使用場景請參考其他文章。這裡需要強調是XML有一個唯一的根節點元素,而JSON根節點元素可能是一個物件,還可能是一個數組。使用Mule完成JSON to XML也很簡單。

Mule UI Config:

Mule XML Config:

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" 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/core http://www.mulesoft.org/schema/mule/core/current/mule.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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="json2xml-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <json:json-to-xml-transformer doc:name="JSON to XML"/>
    </flow>
</mule>

使用Postman提交Json,Json資料如下:

{
    "customer": {
        "cellPhone": "13912340002",
        "customerNo": "C0002",
        "customerName": "Alibaba"
    }
}

得到轉換之後的結果如下:

<?xml version='1.0'?>
<customer>
    <cellPhone>13912340002</cellPhone>
    <customerNo>C0002</customerNo>
    <customerName>Alibaba</customerName>
</customer>

這裡還是要再次強調一下,這個元件要求Json資料必須只有一個根節點。如果Json資料裡有多個根節點,則資料會丟失。舉例如下:

有多個根節點元素的Json資料:

{
    "cellPhone": "13912340002",
    "customerNo": "C0002",
    "customerName": "Alibaba"
}

轉換後的XML結果如下,可以看到只有第一個根元素被轉換成了XML。

<?xml version='1.0'?>
<cellPhone>13912340002</cellPhone>

3.3 JSON to Object

這個元件會將JSON資料轉成成一個Mule內建的Json物件,org.mule.module.json.JsonData。

Mule UI Config:

Mule XML Config:

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" 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/core http://www.mulesoft.org/schema/mule/core/current/mule.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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="json2object-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <json:json-to-object-transformer doc:name="JSON to Object"/>
        <logger message="#[message]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

通過列印的日誌可以看到,轉化後的payload是org.mule.module.json.JsonData。


org.mule.DefaultMuleMessage
{
  id=4b3b1290-b293-11e9-9351-005056c00001
  payload=org.mule.module.json.JsonData
  correlationId=<not set>
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=<not set>
}

3.4 XML to JSON

正如2.2章節所說,XML和Json的根節點元素存在差異。而XML的根節點元素是唯一的,所以XML to JSON比較簡單。

Mule UI Config:

Mule XML Config:

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" 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/core http://www.mulesoft.org/schema/mule/core/current/mule.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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="xml2json-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <json:xml-to-json-transformer doc:name="XML to JSON"/>
    </flow>
</mule>

使用Postman提交Xml,Xml資料如下:

<?xml version='1.0'?>
<customer>
    <cellPhone>13912340002</cellPhone>
    <customerNo>C0002</customerNo>
    <customerName>Alibaba</customerName>
</customer>

轉化後的JSON資料如下,可以看到這裡的Json是正確的。

{
    "customer": {
        "cellPhone": "13912340002",
        "customerNo": "C0002",
        "customerName": "Alibaba"
    }
}

4. 企業版的DataWeave Transformer(視覺化高階資料轉換器)

DataWeave是Mule企業版提供的視覺化的資料換器,使用這個元件可以通過拖拽的操作完成資料的對映。從下圖可以看到,DataWeave配合DataSense,通過拖拽連線的方式,可以將Http的請求資料轉換成Web Service Consumer期待的Xml資料,非常好用方便。

5. 社群版的InfoMapper(自研的視覺化資料轉換器)

上文提到的DataWeave是Mule企業版才提供的功能,社群版並不提供該元件。我們也可以通過擴充套件元件的方式達到類似的功能,我們自研的視覺化轉換元件InfoMapper,其功能和DataWeave相當。

資料轉換是ESB很常用的功能和使用場景,使用Anypoint Studio中提供的簡單資料轉換元件,或者企業版的DataWeave,社群版我們自研的InfoMapper這些高階轉換元件,配合強大的DataSense,資料轉換是非常輕鬆的事情。

本文同步發文於EnjoyingSoft Blogs

訪問EnjoyingSoft官網 ,獲取更多Mule ESB 社群版 實施幫助。

歡迎轉載,但必須保留原文和此段宣告,且在文章頁面明顯位置給出原文連結,否則保留追究法律責任的權