DW CLI allows to execute queries and generate data directly from the command line.
DW CLI is compiled with Graal AOT for fast bootstrap so download the one for your target OS.
- It includes all the modules that are included in the weave runtime. wlang
- It includes file-module, http-module and raml-module from [data-weave-io]((https://github.com/mulesoft-labs/data-weave-io)
- It includes dependency-management-module. This module allows to declare dependencies using annotations.
- Data Formats are:
Format | MimeType | Extension |
---|---|---|
Xml | application/xml | .xml |
Json | application/json | .json |
CSV | application/csv | .csv |
Properties | text/x-java-properties | .properties |
TextPlain | text/plain | .txt |
Yaml | application/yaml | .yaml |
Binary | application/octet-stream | .bin |
Multipart | multipart/form-data | .multipart |
UrlEncoded | application/x-www-form-urlencoded | .urlencoded |
NDjson | application/x-ndjson | .ndjson |
brew tap mulesoft-labs/data-weave
brew install dw
- Download the zip Mac | Linux | Windows
- Unzip the file on your
<home_directory>/.dw
- Add
<home_directory>/.dw/bin
to your PATH
Execute the gradle task nativeImage
./gradlew native-cli:nativeImage
It takes several minutes so good time to take and refill your mate.
Once it finishes you will find the dw
binary in native-cli/build/graal/dw
If the directory containing the dw
executable is in your PATH, you can run dw
from anywhere. If it is not, go to the bin
directory referenced in the installation instructions and run dw
from there.
dw
.........................................................................
.%%%%%....%%%%...%%%%%%...%%%%...%%...%%..%%%%%%...%%%%...%%..%%..%%%%%%.
.%%..%%..%%..%%....%%....%%..%%..%%...%%..%%......%%..%%..%%..%%..%%.....
.%%..%%..%%%%%%....%%....%%%%%%..%%.%.%%..%%%%....%%%%%%..%%..%%..%%%%...
.%%..%%..%%..%%....%%....%%..%%..%%%%%%%..%%......%%..%%...%%%%...%%.....
.%%%%%...%%..%%....%%....%%..%%...%%.%%...%%%%%%..%%..%%....%%....%%%%%%.
.........................................................................
Usage:
dw [-p <weavePath>]? [-i <name> <path>]* [-v]? [-o <outputPath>]? [[-f <filePath>] | [-m <nameIdentifier>] | <scriptContent>]
Arguments Detail:
--spell | Runs a spell. Use the <spellName> or <wizard>/<spellName> for spells from a given wizard.
--update-grimoires | Update all wizard grimoires
--add-wizard | Downloads wizard grimoire so that its spell are accessible
--path or -p | Path of jars or directories where weave files are being searched.
--input or -i | Declares a new input.
--verbose or -v | Enable Verbose Mode.
--output or -o | Specifies output file for the transformation if not standard output will be used.
--main or -m | The full qualified name of the mapping to be execute.
--file or -f | Path to the file
--eval | Evaluates the script instead of writing it
--version | The version of the CLI and Runtime
--clean-cache | Cleans the cache where all artifacts are being downloaded this force to download all artifacts every time
Example:
dw -i payload <fullpathToUser.json> "output application/json --- payload filter (item) -> item.age > 17"
Documentation reference:
https://docs.mulesoft.com/mule-runtime/4.3/dataweave
Input file users.json
[
{
"name": "User1",
"age": 19
},
{
"name": "User2",
"age": 18
},
{
"name": "User3",
"age": 15
},
{
"name": "User4",
"age": 13
},
{
"name": "User5",
"age": 16
}
]
Let's query users old enough to drink alcohol:
dw -i payload <fullpathToUsers.json> "output application/json --- payload filter (item) -> item.age > 17"
cat <fullpathToUser.json> | dw "output application/json --- payload filter (item) -> item.age > 17"
dw "output application/xml --- users: {( 1 to 100 map (item) -> {user: "User" ++ item} )}" >> out.xml
- DW_HOME = The directory where the home will be found if not defined
~/.dw
will be used - DW_LIB_PATH = The directory where libraries are going to be search by default. If not defined
${DW_HOME}/libs
will be used - DW_DEFAULT_INPUT_MIMETYPE = The default mimeType that is going to be used for the standard input. If not defined
application/json
will be used - DW_DEFAULT_OUTPUT_MIMETYPE = The default output mimeType that is going to be if not defined. If not defined
application/json
will be used
This specifies the path where to search it will also search on the DW_LIB_PATH
env directory path.
DIRECTORY=<pick a directory>
cd $DIRECTORY
echo '%dw 2.0
fun addUnderscore(s: String): String = s ++ "_"' > utils.dwl
dw --path $DIRECTORY 'output application/json import * from utils --- {underscored: addUnderscore("hello")}'
An interesting use case for the CLI is to combine it with curl
We can use the Github API to query commits of a repo.
We can easily get the first commit by doing:
curl "https://api.github.com/repos/mulesoft/mule/commits?per_page=5" | dw "payload[0]"
or we can get the message by doing:
curl "https://api.github.com/repos/mulesoft/mule/commits?per_page=5" | dw "{message: payload[0].commit.message}"
This example will create a really big csv and it will stream it to the HTTP server on localhost.
dw "output application/csv --- (1 to 10000000000000000000000) map (item) -> {name: 'User \$(item)'}" | curl -X POST -T "/dev/stdin" http://localhost:8081/
For more info about the language see the docs site
Spells are just executables scripts that can be called from the command line using the Spell
name.
For example
dw --eval --spell Playground
It is going to execute the Playground spell that is going to be located in
{User_Home}/.dw/grimoires/data-weave-grimoire/Playground/src/Main.dwl
A grimoire is a collection of spells
from a given wizard
. The default grimoire
is located in data-weave-grimoire
But it can also be consumed from other trusted Wizard
. For example leansh/Tutorial
is going to looks for a github repo called leansh-data-weave-grimoire under leansh user.