Mulesoft 简明教程

MuleSoft - DataWeave Language

DataWeave 基本上是一种 MuleSoft 表达式语言。它主要用于访问和转换通过 Mule 应用程序接收的数据。Mule 运行时负责运行 Mule 应用程序中的脚本和表达式,DataWeave 与 Mule 运行时紧密集成。

DataWeave is basically a MuleSoft expression language. It is mainly used for accessing and transforming the data received through a Mule application. Mule runtime is responsible for running the script and expressions in our Mule application, DataWeave is strongly integrated with Mule runtime.

Features of DataWeave Language

以下是 DataWeave 语言的一些重要特性:-

Following are some important features of DataWeave language −

可以非常轻松地将数据从一种格式转换为另一种格式。例如,我们可以将 application/json 转换为 application/xml。输入负载如下:-

Data can be transformed from one format to another very easily. For example, we can transform application/json to application/xml. The input payload is as follows −

{
   "title": "MuleSoft",
   "author": " tutorialspoint.com ",
   "year": 2019
}

以下是用于转换的 DataWeave 代码:-

Following is the code in DataWeave for transform −

%dw 2.0
output application/xml
---
{
   order: {
      'type': 'Tutorial',
      'title': payload.title,
      'author': upper(payload.author),
      'year': payload.year
   }
}

接下来, output 负载如下:-

Next, the output payload is as follows −

<?xml version = '1.0' encoding = 'UTF-8'?>
<order>
   <type>Tutorial</type>
   <title>MuleSoft</title>
   <author>tutorialspoint.com</author>
   <year>2019</year>
</order>

转换组件可以用来创建可以执行简单和复杂数据转换的脚本。

The transform component can be used for creating scripts that performs both simple as well as complex data transformations.

由于大多数 Mule 消息处理程序支持 DataWeave 表达式,因此我们可以在 Mule 事件所需的各个部分访问和使用核心 DataWeave 功能。

We can access and use core DataWeave functions on parts of the Mule event that we need as most of the Mule message processors support DataWeave expressions.

Prerequisites

在我们计算机上使用 DataWeave 脚本之前,我们需要满足以下前提条件:-

We need to satisfy the following prerequisites before using DataWeave scripts on our computer −

  1. Anypoint Studio 7 is required to use Dataweave scripts.

  2. After installing Anypoint Studio, we need to set up a project with a Transform Message component in order to use DataWeave scripts.

Steps for Using DataWeave Script with Example

为了使用 DataWeave 脚本,我们需要按照以下步骤进行操作:

In order to use DataWeave scrip, we need to follow the steps below −

Step 1

Step 1

首先,我们必须使用 File → New → Mule Project 设置一个新项目,就像我们上一章中所做的一样。

First, we need to set up a new project, as we did in the previous chapter, by using File → New → Mule Project.

Step 2

Step 2

接下来,我们需要提供项目名称。对于该示例,我们指定了名称 Mule_test_script

Next, we need to provide the name of the project. For this example, we are giving the name, Mule_test_script.

Step 3

Step 3

现在,我们需要将 Transform Message componentMule Palette tab 拖动到 canvas 中。如下所示:

Now, we need to drag the Transform Message component from Mule Palette tab into canvas. It is shown as below −

dataweave script

Step 4

Step 4

接下来,在 Transform Message component 选项卡中,单击预览以打开预览窗格。我们可以通过单击 Preview 旁边的空白矩形来展开源代码区域。

Next, in the Transform Message component tab, click on Preview to open Preview pane. We can expand the source code area by clicking the empty rectangle next to Preview.

Step 5

Step 5

现在,我们可以开始使用 DataWeave 语言进行脚本编写。

Now, we can start scripting with DataWeave language.

Example

以下是将两个字符串连接成一个字符串的简单示例:

Following is the simple example of concatenating two strings into one −

transform message component

上述 DataWeave 脚本带有键值对 ({ myString: ("hello" ++ "World") }) ,它会将两个字符串连接成一个字符串。

The above DataWeave script is having a key-value pair ({ myString: ("hello" ++ "World") }) which will concatenate two strings into one.