HStream CLI
We can run the following to use HStream CLI:
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.
docker exec -it docker_hserver0_1 bash
> hstream --help
======= HStream CLI =======
Usage: hstream [--host SERVER-HOST] [--port INT] [--tls-ca STRING]
[--tls-key STRING] [--tls-cert STRING] COMMAND
Available options:
--host SERVER-HOST Server host value (default: "127.0.0.1")
--port INT Server port value (default: 6570)
--tls-ca STRING path name of the file that contains list of trusted
TLS Certificate Authorities
--tls-key STRING path name of the client TLS private key file
--tls-cert STRING path name of the client TLS public key certificate
file
-h,--help Show this help text
Available commands:
sql Start HStream SQL Shell
nodes Manage HStream Server Cluster
init Init HStream Server Cluster
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:
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:
hstream --tls-key "<path to the trusted role key file>" --tls-cert "<path to the signed certificate file>"
Check Cluster Status
> hstream nodes --help
Usage: hstream nodes COMMAND
Manage HStream Server Cluster
Available options:
-h,--help Show this help text
Available commands:
list List all running nodes in the cluster
status Show the status of nodes specified, if not specified
show the status of all nodes
check-running Check if all nodes in the the cluster are running,
and the number of nodes is at least as specified
> hstream nodes list
+-----------+
| server_id |
+-----------+
| 100 |
| 101 |
+-----------+
> hstream nodes status
+-----------+---------+-------------------+
| server_id | state | address |
+-----------+---------+-------------------+
| 100 | Running | 192.168.64.4:6570 |
| 101 | Running | 192.168.64.5:6572 |
+-----------+---------+-------------------+
> hstream nodes check-running
All nodes in the cluster are running.
Manage Streams
We can also manage streams through the hstream command line tool.
> hstream stream --help
Usage: hstream stream COMMAND
Manage Streams in HStreamDB
Available options:
-h,--help Show this help text
Available commands:
list Get all streams
create Create a stream
delete delete a stream (Warning: incomplete implementation)
Create a stream
Usage: hstream stream create --name STRING [-r|--replication-factor INT]
[-b|--backlog-duration INT] [-s|--shards INT]
Create a stream
Available options:
--name STRING stream name
-r,--replication-factor INT
replication-factor (default: 3)
-b,--backlog-duration INT
Backlog duration in seconds (default: 0)
-s,--shards INT shard numbers of the stream (default: 1)
-h,--help Show this help text
Example: Create a demo stream with the default settings.
> hstream stream create --name demo
demo
Show and delete streams
> hstream stream list
+------------------+---------+----------------+-------------+
| Stream Name | Replica | Retention Time | Shard Count |
+------------------+---------+----------------+-------------+
| demo | 1 | 0sec | 1 |
+------------------+---------+----------------+-------------+
> hstream stream delete --name demo
Done.
> hstream stream list
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.
> hstream sql --help
Usage: hstream sql [--update-interval INT] [--retry-timeout INT]
Start HStream SQL Shell
Available options:
--update-interval INT interval to update available servers in seconds
(default: 30)
--retry-timeout INT timeout to retry connecting to a server in seconds
(default: 60)
-e,--execute STRING execute the statement and quit
--history-file STRING history file path to write interactively executed
statements
-h,--help Show this help text
Once you entered shell, you can see the following help info:
__ _________________ _________ __ ___
/ / / / ___/_ __/ __ \/ ____/ | / |/ /
/ /_/ /\__ \ / / / /_/ / __/ / /| | / /|_/ /
/ __ /___/ // / / _, _/ /___/ ___ |/ / / /
/_/ /_//____//_/ /_/ |_/_____/_/ |_/_/ /_/
Command
:h To show these help info
:q To exit command line interface
:help [sql_operation] To show full usage of sql statement
SQL STATEMENTS:
To create a simplest stream:
CREATE STREAM stream_name;
To create a query select all fields from a stream:
SELECT * FROM stream_name EMIT CHANGES;
To insert values to a stream:
INSERT INTO stream_name (field1, field2) VALUES (1, 2);
There are two kinds of commands:
- Basic shell commands, starting with
:
- SQL statements end with
;
Basic CLI Operations
To quit current cli session:
> :q
To print out help info overview:
> :h
To show specific usage of some SQL statements:
> :help CREATE
CREATE STREAM <stream_name> [IF EXIST] [AS <select_query>] [ WITH ( {stream_options} ) ];
CREATE {SOURCE|SINK} CONNECTOR <stream_name> [IF NOT EXIST] WITH ( {connector_options} );
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.
- Create an ordinary stream:
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.
- 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:
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.
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:
SELECT * FROM demo EMIT CHANGES;
will be terminated if the stream demo is deleted;
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:
DROP STREAM demo IF EXISTS;
Show all streams
You can also show all streams by using the SHOW STREAMS
command.
> SHOW STEAMS;
+-------------+---------+----------------+-------------+
| Stream Name | Replica | Retention Time | Shard Count |
+-------------+---------+----------------+-------------+
| demo | 3 | 0sec | 1 |
+-------------+---------+----------------+-------------+
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.
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:
TERMINATE QUERY <id>;
We can get all the query information by command SHOW
:
SHOW QUERIES;
output just for demonstration :
+------------------+------------+--------------------------+----------------------------------+
| Query ID | Status | Created Time | SQL Text |
+------------------+------------+--------------------------+----------------------------------+
| 1361978122003419 | TERMINATED | 2022-07-28T06:03:42+0000 | select * from demo emit changes; |
+------------------+------------+--------------------------+----------------------------------+
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,
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:
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 a
s, we can:
CREATE STREAM demo2 AS SELECT a, 1 AS b FROM demo;
CREATE VIEW v_demo2 AS SELECT SUM(a) FROM demo2 GROUP BY b;
SELECT * FROM demo2 WHERE b = 1;