3.18.0 • Published 6 months ago

@lskjs/rabbit-cli v3.18.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

LSK.js – rabbit-cli

@lskjs/rabbit-cli – CLI for quick post json's to RabbitMQ

LSK logo NPM version NPM downloads NPM Dependency count Have TypeScript types Have tree shaking NPM Package size Package size Ask us in Telegram


Table of contents

⌨️ Install

# yarn
yarn i @lskjs/rabbit-cli 

# npm
npm i @lskjs/rabbit-cli 

Global install

npm i -g @lskjs/rabbit-cli

Basic commands

Lskrabbit understands the following commands:

  • pub - publish messages to an exchange or queues.
  • pipe - pipe messages to an exchange or queues.

See the examples section for further information.

Broker URI specification

The specification of the RabbitMQ broker URI follows the AMQP URI specification as implemented by the go RabbitMQ client library.

Examples

  • amqp://guest:guest@localhost:5672/
  • amqps://guest:guest@my-broker.dev:5671/
  • amqps://guest:guest@my-broker.dev:5671/vhost

Note that according to RFC3986 it might be necessary to escape certain characters like e.g. ? (%3F) or # (%23) as otherwise parsing of the URI may fail with an error.

Format specification pub and pipe command

Format has the following effect on the output:

FORMATFormat on consoleFormat of saved messages
rawPretty-printed metadata + rawMessage body Metadata as JSON-File + Body as-is
jsonPretty-printed JSONPretty-printed JSON

Environment variables

Use environment variables to specify standard values for variables.

Default RabbitMQ broker

The URI of the RabbitMQ broker can be set with the environment variable AMQP_URI. Example:

export AMQP_URI=amqp://guest:guest@localhost:5672/

Default RabbitMQ config

The default QUEUE, EXCHANGE, etc can be set using the environment variables. Example:

export AMQP_QUEUE=lsk_queue
export AMQP_EXCHANGE=lsk_exchange
export AMQP_KEY=lsk_key
export AMQP_PREFETCH=10
export AMQP_MAX_PRIORITY=10
echo "Hello" | lskrabbit pub

Publish messages

The pub command is used to publish messages to an exchange. The messages to be published are either read from json/txt file or stream. The general form of the pub command is:

cat [SOURCE] | lskrabbit pub 
             [--uri=URI]
             [--queue=QUEUE]
             [--exchange=EXCHANGE]
             [--key=KEY]
             [--prefetch=PREFETCH]
             [--concurrency=CONCORRENCY]
             [--extract=EXTRACT]
             [--parse=PARSE]

Command params

Key (short)KeyDescription
-u--uriURI RabbiqMQ
-q--queueQueue RabbitMq
-e--exchangeExchange
-k--keyRouting key
-p--prefetchPrefetch
-c--concurrencyConcurrency
-x--extractExtract callback
-r--parsestr => json

Message routing is either specified with a routing key and the --key option. --extract options allows you to convert data to the desired format. Option is a callback-function. You can also parse the data using callback-function in --parse option.

JSON Message has many configurable parameters. Description in the table:

Message params

KeyDescription
_qqueue
_eexchange
_kkey
_ppriority
_expexpiration
_prpersistent

Examples messages

tests/messages.json

{ "_id": 111, "_q": "lsk_queue" }
{ "_id": 222, "_q": "lsk_queue", "_p": 10 }
{ "_id": 333, "_q": "lsk_queue", "_exp": 60000 }
{ "_id": 444, "_q": "lsk_queue", "_p": 11 }
{ "_id": 555, "_q": "lsk_queue_2" }
{ "_id": 666, "_e": "lsk_exchange" }
{ "_id": 777, "_e": "lsk_exchange", "_k": "lsk_key" }
{ "_id": 888, "_e": "lsk_exchange", "_k": "lsk_key_2" }

tests/messages.txt

{ "test": 123 }
{ "test": 546 }

Examples command

  • echo "hello" | rabtap pub --exchange lsk_exchange - publish "hello" to exchange lsk_exchange

  • echo '{ "_id":"123" }' | lskrabbit pub - publish JSONEachRow

  • cat tests/messages.json | lskrabbit pub - publish messages from JSON file

  • cat tests/messages.json | docker run --rm -i lskjs/rabbit-cli pub - publish messages from JSON file with docker image

  • cat tests/messages.json | DEBUG=lsk lskrabbit pub - publish from JSON file with DEBUG

  • cat tests/messages.txt | lskrabbit pub --queue lsk_queue - publish messages to queue from txt file

  • cat tests/messages.json | lskrabbit pub -x "row => ({...row, test: row, _e: 'lsk_exchange' })" - publish messages with extract from JSON-each-ROW

  • cat tests/messages.txt | lskrabbit pub --parse "_id => ({ _id, _e: 'lsk_exchange' })" - publish messages with extract from txt file String-each-row

Publish messages from MongoDb

Install mongoexport

With brew:

brew install mongodb/brew/mongodb-database-tools

Or other MacOs, Linux, Windows.

How to usage

mongoexport [--uri=MONGO_URI] [--collection=MONGO_COLLECTION] | lskrabbit pub
            [--uri=URI]
            [--queue=QUEUE]
            [--exchange=EXCHANGE]
            [--key=KEY]
            [--prefetch=PREFETCH]
            [--concurrency=CONCORRENCY]
            [--extract=EXTRACT]
            [--parse=PARSE]

Examples command

  • mongoexport --uri=mongodb://localhost:27017/ --collection=lsk_test | lskrabbit pub --uri amqp://localhost --queue lsk_queue - publish messages from mongoDb collection

Publish messages from Clickhouse

Install clickhouse-client

Before you start, need to install clickhouse-client.

How to usage

clickhouse-client
            [--host=CLHOST]
            [--port=PORT]
            [--database=CLDB]
            [--password=CLPASSWORD]
            [--multiquery=MULTIQUERY] | lskrabbit pub
            [--uri=URI]
            [--queue=QUEUE]
            [--exchange=EXCHANGE]
            [--key=KEY]
            [--prefetch=PREFETCH]
            [--concurrency=CONCORRENCY]
            [--extract=EXTRACT]
            [--parse=PARSE]

Examples command

  • clickhouse-client -h localhost --port 9000 -d default --password qwerty --multiquery < clickhouse.sql | lskrabbit pub - publish messages from clickhouse

📖 License

This project is licensed under the MIT License - see the LICENSE file for details

👥 Contributors

👏 Contributing

  1. Fork it (https://github.com/yourname/yourproject/fork)
  2. Create your feature branch (git checkout -b features/fooBar)
  3. Commit your changes (git commit -am 'feat(image): Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

📮 Any questions? Always welcome :)

3.17.0

6 months ago

3.8.1

10 months ago

3.18.0

6 months ago

3.16.5

6 months ago

3.16.4

6 months ago

3.1.1

1 year ago

2.88.1

1 year ago

3.1.0-alpha.0

1 year ago

2.85.0

1 year ago

2.85.1

1 year ago

2.84.11

2 years ago

2.76.0

2 years ago

2.74.0

2 years ago

2.73.0

2 years ago

2.75.1

2 years ago

2.69.1

2 years ago

2.64.0

2 years ago

2.65.0

2 years ago

2.64.1

2 years ago

2.66.0

2 years ago

2.65.1

2 years ago

2.67.0

2 years ago

2.66.1

2 years ago

2.62.0

2 years ago

2.60.1

2 years ago

2.59.0

2 years ago

2.57.0

2 years ago

2.56.0

2 years ago

2.54.1

2 years ago

2.54.0

2 years ago

2.53.0

2 years ago

2.52.0

2 years ago