EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换

本篇主要介绍在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开发。

[TOC]

1. 数据转换概念

数据转换是ESB平台的核心模块,是解决异构系统通讯的必备组件。举个例子,订单系统暴露了一个Web Service,而我们的网上商城生成的订单是Json或者Flat file格式,将JSON数据或者Flat file文本格式转换成Web Service的XML格式,这个需求就可以通过ESB来实现。我们在第三篇讲解了Mule Message的结构,回忆一下Mule Message的结构。

mule-message.png

我们通常说的数据转换就是对Mule Message中Payload做操作,改变数据类型或者数据结构,数据内容等。在做数据转换之前,我们需要确定数据的源类型和目标类型,然后选择合适的数据转换组件。

datatransformer.png

2. 数据智能感知 - DataSense

DataSense是Mule最强大的功能之一,Anypoint Studio会主动获取元数据类型和结构等信息,以帮助开发者在应用程序中准确的映射或者使用数据。

DataSense配合企业版的DataWeave可视化数据转化组件,或者我们自研的社区版可视化数据转化组件InfoMapper,能够很大的加速应用集成的开发,方便开发者完成数据转换和映射。本文后半段会介绍DataWeave和InfoMapper的强大之处。

DataSense举例说明,如果您的应用程序连接到一个天气预报的Web Service,那么DataSense会捕获Web Service使用的数据类型和结构的信息,这些信息对开发者非常有用,它会展示这个Web Service期望的的入参是什么,出参是什么。

天气预报WebService中规定的入参和出参如下图:

1564470992331.png

Anypoint Studio中的Web Service Consumer组件,会清楚的显示Expected Data,也就是期望的入参数据格式。如下图Input页签,可以看到需要输入theCityName的XML数据。

1564471385813.png

同时Web Service Consumer组件也会展示这个WebService的输出参数是什么。如下图Output页签。

1564471333527.png

3. 简单数据转换组件

Mule ESB内置了很多常用的简单的数据转换组件,从组件名称就可以看到组件的适用范围,源数据类型和目标数据类型。另外Mule ESB也支持自定义Transformer,通过Java或者其他脚本语言实现复杂的数据转换逻辑。

3.1 Object to JSON

Object to JSON组件能够将Java对象转换成JSON格式,方便其他语言比如C#,Python解析和使用。常见的使用场景就是将Map对象转换成JSON格式。

Mule UI Config:

object-to-json.png

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:

1564467261853.png

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:

1564468173779.png

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:

1564468870166.png

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数据,非常好用方便。

1564472332491.png

5. 社区版的InfoMapper(自研的可视化数据转换器)

上文提到的DataWeave是Mule企业版才提供的功能,社区版并不提供该组件。我们也可以通过扩展组件的方式达到类似的功能,我们自研的可视化转换组件InfoMapper,其功能和DataWeave相当。

1564472729142.png

数据转换是ESB很常用的功能和使用场景,使用Anypoint Studio中提供的简单数据转换组件,或者企业版的DataWeave,社区版的InfoMapper这些高级转换组件,配合强大的DataSense,数据转换是非常轻松的事情。

本文同步发文于EnjoyingSoft Blogs博客园CSDN

访问EnjoyingSoft 网站,获取更多Mule ESB 社区版 实施帮助。

欢迎转载,但必须保留原文和此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271