# HStream CLI

We can run the following to use HStream CLI:

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

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
1
2
======= 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 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>"
1

# 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>"
1

# 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 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)
1
2
3
4
5
6
7
8
9
10
11

# 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
1
2
3
4
5
6
7
8
9
10
11
12

Example: Create a demo stream with the default settings.

> hstream stream create --name demo
demo
1
2

# 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.
1
2
3
4
5
6
7
8
9
10
11
12

# 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
1
2
3
4
5
6
7
8
9
10
11
12
13

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);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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:

> :q
1

To print out help info overview:

> :h
1

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>;
1
2
3
4
5

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:
CREATE STREAM stream_name;
1

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:

CREATE STREAM stream_name AS SELECT * from demo;
1

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);
1

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;
1

will be terminated if the stream demo is deleted;

DROP STREAM demo;
1

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;
1

# 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           |
+-------------+---------+----------------+-------------+
1
2
3
4
5
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.

SELECT a FROM demo EMIT CHANGES;
1

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>;
1

We can get all the query information by command SHOW:

SHOW QUERIES;
1

output just for demonstration :

+------------------+------------+--------------------------+----------------------------------+
| Query ID         | Status     | Created Time             | SQL Text                         |
+------------------+------------+--------------------------+----------------------------------+
| 1361978122003419 | TERMINATED | 2022-07-28T06:03:42+0000 | select * from demo emit changes; |
+------------------+------------+--------------------------+----------------------------------+
1
2
3
4
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,

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

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;
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:

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;
1
2
3