Git Product home page Git Product logo

data-weave-native's Introduction

DW CLI

DW CLI allows to execute queries and generate data directly from the command line.

Supported 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

DW CLI is compiled with Graal AOT for fast bootstrap so download the one for your target os.

How to install it

  1. Download the zip Mac | Linux
  2. Unzip the file on your <home_directory>/.dw
  3. Add <home_directory>/.dw/bin to your PATH

How to use it

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.

Show documentation

dw

.........................................................................
.%%%%%....%%%%...%%%%%%...%%%%...%%...%%..%%%%%%...%%%%...%%..%%..%%%%%%.
.%%..%%..%%..%%....%%....%%..%%..%%...%%..%%......%%..%%..%%..%%..%%.....
.%%..%%..%%%%%%....%%....%%%%%%..%%.%.%%..%%%%....%%%%%%..%%..%%..%%%%...
.%%..%%..%%..%%....%%....%%..%%..%%%%%%%..%%......%%..%%...%%%%...%%.....
.%%%%%...%%..%%....%%....%%..%%...%%.%%...%%%%%%..%%..%%....%%....%%%%%%.
.........................................................................


Usage:

dw [-p <weavePath>]? [-i <name> <path>]* [-v]? [-o <outputPath>]? [[-f <filePath>] | [-m <nameIdentifier>] | <scriptContent>]

Arguments Detail:

 --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.

 Examples

 dw -i payload <fullpathToUser.json> "output application/json --- payload filter (item) -> item.age > 17"

 Documentation reference:

 https://docs.mulesoft.com/mule-runtime/4.2/dataweave

Query content from a file

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"

Query content from standard input

cat <fullpathToUser.json> | dw "output application/json --- payload filter (item) -> item.age > 17"

Redirecting the output to a file

dw "output application/xml --- users: {( 1 to 100 map (item) -> {user: "User" ++ item} )}" >> out.xml

DW Important ENV variables

  • 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 standar 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

Using external DW Modules with --path

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")}'

CURL + DW => Power API Playground

An interesting use case for the CLI is to combine it with curl

Example query github commits

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}"

HTTP POST data generated by DW

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/

Documentation

For more info about the language see the docs site

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.