HStream CLI

We can run the following to use HStream CLI:

  1. docker run -it --rm --name some-hstream-admin --network host hstreamdb/hstream:v0.11.0 hstream --help

For ease of illustration, we execute an interactive bash shell in the HStream container to use HStream admin,

The following example usage is based on the cluster started in quick start, please adjust correspondingly.

  1. docker exec -it docker_hserver0_1 bash
  2. > hstream --help
  1. ======= HStream CLI =======
  2. Usage: hstream [--host SERVER-HOST] [--port INT] [--tls-ca STRING]
  3. [--tls-key STRING] [--tls-cert STRING] COMMAND
  4. Available options:
  5. --host SERVER-HOST Server host value (default: "127.0.0.1")
  6. --port INT Server port value (default: 6570)
  7. --tls-ca STRING path name of the file that contains list of trusted
  8. TLS Certificate Authorities
  9. --tls-key STRING path name of the client TLS private key file
  10. --tls-cert STRING path name of the client TLS public key certificate
  11. file
  12. -h,--help Show this help text
  13. Available commands:
  14. sql Start HStream SQL Shell
  15. nodes Manage HStream Server Cluster
  16. init Init HStream Server Cluster
  17. stream Manage Streams in HStreamDB

Security Settings (optional)

If security option is enabled, here are some options that should also be configured for CLI correspondingly.

Encryption

If server encryption is enabled, the --tls-ca option should be added to CLI connection options:

  1. hstream --tls-ca "<path to the CA certificate file>"

Authentication

If server authentication is enabled, the --tls-key and --tls-cert options should be added to CLI connection options:

  1. hstream --tls-key "<path to the trusted role key file>" --tls-cert "<path to the signed certificate file>"

Check Cluster Status

  1. > hstream nodes --help
  2. Usage: hstream nodes COMMAND
  3. Manage HStream Server Cluster
  4. Available options:
  5. -h,--help Show this help text
  6. Available commands:
  7. list List all running nodes in the cluster
  8. status Show the status of nodes specified, if not specified
  9. show the status of all nodes
  10. check-running Check if all nodes in the the cluster are running,
  11. and the number of nodes is at least as specified
  12. > hstream nodes list
  13. +-----------+
  14. | server_id |
  15. +-----------+
  16. | 100 |
  17. | 101 |
  18. +-----------+
  19. > hstream nodes status
  20. +-----------+---------+-------------------+
  21. | server_id | state | address |
  22. +-----------+---------+-------------------+
  23. | 100 | Running | 192.168.64.4:6570 |
  24. | 101 | Running | 192.168.64.5:6572 |
  25. +-----------+---------+-------------------+
  26. > hstream nodes check-running
  27. All nodes in the cluster are running.

Manage Streams

We can also manage streams through the hstream command line tool.

  1. > hstream stream --help
  2. Usage: hstream stream COMMAND
  3. Manage Streams in HStreamDB
  4. Available options:
  5. -h,--help Show this help text
  6. Available commands:
  7. list Get all streams
  8. create Create a stream
  9. delete delete a stream (Warning: incomplete implementation)

Create a stream

  1. Usage: hstream stream create --name STRING [-r|--replication-factor INT]
  2. [-b|--backlog-duration INT] [-s|--shards INT]
  3. Create a stream
  4. Available options:
  5. --name STRING stream name
  6. -r,--replication-factor INT
  7. replication-factor (default: 3)
  8. -b,--backlog-duration INT
  9. Backlog duration in seconds (default: 0)
  10. -s,--shards INT shard numbers of the stream (default: 1)
  11. -h,--help Show this help text

Example: Create a demo stream with the default settings.

  1. > hstream stream create --name demo
  2. demo

Show and delete streams

  1. > hstream stream list
  2. +------------------+---------+----------------+-------------+
  3. | Stream Name | Replica | Retention Time | Shard Count |
  4. +------------------+---------+----------------+-------------+
  5. | demo | 1 | 0sec | 1 |
  6. +------------------+---------+----------------+-------------+
  7. > hstream stream delete --name demo
  8. Done.
  9. > hstream stream list
  10. Done. No results.

HStream SQL

HStreamDB also provides an interactive SQL shell for a series of operations, such as the management of streams and views, data insertion and retrieval, etc.

  1. > hstream sql --help
  2. Usage: hstream sql [--update-interval INT] [--retry-timeout INT]
  3. Start HStream SQL Shell
  4. Available options:
  5. --update-interval INT interval to update available servers in seconds
  6. (default: 30)
  7. --retry-timeout INT timeout to retry connecting to a server in seconds
  8. (default: 60)
  9. -e,--execute STRING execute the statement and quit
  10. --history-file STRING history file path to write interactively executed
  11. statements
  12. -h,--help Show this help text

Once you entered shell, you can see the following help info:

  1. __ _________________ _________ __ ___
  2. / / / / ___/_ __/ __ \/ ____/ | / |/ /
  3. / /_/ /\__ \ / / / /_/ / __/ / /| | / /|_/ /
  4. / __ /___/ // / / _, _/ /___/ ___ |/ / / /
  5. /_/ /_//____//_/ /_/ |_/_____/_/ |_/_/ /_/
  6. Command
  7. :h To show these help info
  8. :q To exit command line interface
  9. :help [sql_operation] To show full usage of sql statement
  10. SQL STATEMENTS:
  11. To create a simplest stream:
  12. CREATE STREAM stream_name;
  13. To create a query select all fields from a stream:
  14. SELECT * FROM stream_name EMIT CHANGES;
  15. To insert values to a stream:
  16. INSERT INTO stream_name (field1, field2) VALUES (1, 2);

There are two kinds of commands:

  1. Basic shell commands, starting with :
  2. SQL statements end with ;

Basic CLI Operations

To quit current cli session:

  1. > :q

To print out help info overview:

  1. > :h

To show specific usage of some SQL statements:

  1. > :help CREATE
  2. CREATE STREAM <stream_name> [IF EXIST] [AS <select_query>] [ WITH ( {stream_options} ) ];
  3. CREATE {SOURCE|SINK} CONNECTOR <stream_name> [IF NOT EXIST] WITH ( {connector_options} );
  4. CREATE VIEW <stream_name> AS <select_query>;

Available SQL operations include: CREATE, DROP, SELECT, SHOW, INSERT, TERMINATE.

SQL Statements

All the processing and storage operations are done via SQL statements.

Stream

There are two ways to create a new data stream.

  1. Create an ordinary stream:
  1. CREATE STREAM stream_name;

This will create a stream with no particular function. You can SELECT data from the stream and INSERT to via corresponding SQL statement.

  1. Create a stream, and this stream will also run a query to select specified data from some other stream.

Adding a Select statement after Create with a keyword AS can create a stream will create a stream that processes data from another stream.

For example:

  1. CREATE STREAM stream_name AS SELECT * from demo;

In the example above, by adding an AS followed by a SELECT statement to the normal CREATE operation, it will create a stream that will also select all the data from the demo.

After Creating the stream, we can insert values into the stream.

  1. INSERT INTO stream_name (field1, field2) VALUES (1, 2);

There is no restriction on the number of fields a query can insert. Also, the type of value is not restricted. However, you need to make sure that the number of fields and the number of values are aligned.

Deletion command is DROP STREAM <Stream_name> ;, which deletes a stream, and terminate all the queries that depend on the stream.

For example:

  1. SELECT * FROM demo EMIT CHANGES;

will be terminated if the stream demo is deleted;

  1. DROP STREAM demo;

If you try to delete a stream that does not exist, an error message will be returned. To turn it off, you can use add IF EXISTS after the stream_name:

  1. DROP STREAM demo IF EXISTS;

Show all streams

You can also show all streams by using the SHOW STREAMS command.

  1. > SHOW STEAMS;
  2. +-------------+---------+----------------+-------------+
  3. | Stream Name | Replica | Retention Time | Shard Count |
  4. +-------------+---------+----------------+-------------+
  5. | demo | 3 | 0sec | 1 |
  6. +-------------+---------+----------------+-------------+

Queries

Run a continuous query on the stream to select data from a stream:

After creating a stream, we can select data from the stream in real-time. All the data inserted after the select query is created will be printed out when the insert operation happens. Select supports real-time processing on the data inserted into the stream.

For example, we can choose the field and filter the data selected from the stream.

  1. SELECT a FROM demo EMIT CHANGES;

This will only select field a from stream demo.

How to terminate a query?

A query can be terminated if we know the query id:

  1. TERMINATE QUERY <id>;

We can get all the query information by command SHOW:

  1. SHOW QUERIES;

output just for demonstration :

  1. +------------------+------------+--------------------------+----------------------------------+
  2. | Query ID | Status | Created Time | SQL Text |
  3. +------------------+------------+--------------------------+----------------------------------+
  4. | 1361978122003419 | TERMINATED | 2022-07-28T06:03:42+0000 | select * from demo emit changes; |
  5. +------------------+------------+--------------------------+----------------------------------+

Find the query to terminate, make sure is id not already terminated, and pass the query id to TERMINATE QUERY

Or under some circumstances, you can choose to TERMINATE ALL;.

View

View is a projection of specified data from streams. For example,

  1. CREATE VIEW v_demo AS SELECT SUM(a) FROM demo GROUP BY a;

the above command will create a view that keeps track of the sum of a (which have the same values, because of groupby) and have the same value from the point this query is executed.

The operations on view are very similar to those on streams.

Except we can not use SELECT ... EMIT CHANGES performed on streams because a view is static and there are no changes to emit. Instead, for example, we select from view with:

  1. SELECT * FROM v_demo WHERE a = 1;

This will print the sum of a when a = 1.

If we want to create a view to record the sum of as, we can:

  1. CREATE STREAM demo2 AS SELECT a, 1 AS b FROM demo;
  2. CREATE VIEW v_demo2 AS SELECT SUM(a) FROM demo2 GROUP BY b;
  3. SELECT * FROM demo2 WHERE b = 1;