To define a stream, you use the definition CQL REGISTER STREAM clause.
In the REGISTER STREAM clause, you specify the stream name (the name of the input stream) and the schema specification character string (the content of the stream data that identifies it as the input stream). The following shows the format for specifying a REGISTER STREAM clause:
REGISTER STREAM stream-name
(schema-specification-character-string); |
For example, the following shows a CQL statement that defines a stream for a temperature analysis system:
REGISTER STREAM temperature_stream
(observation_time TIME, id INTEGER, temperature INTEGER); |
For the stream name, temperature_stream is specified. The parameters observation_time (observation time), id (observation site ID), and temperature (measured temperature) are specified using the TIME, INTEGER, and INTEGER data types, respectively.
To define a query, you use the definition CQL REGISTER QUERY clause. In the REGISTER QUERY clause, you specify the name of the query, a stream clause, a SELECT clause, a FROM clause, and a WHERE clause, in that order. For the query name, you specify the name of the stream that is to be output after the stream data has been processed by the query (output stream).
The following table shows what the stream clause, SELECT clause, FROM clause, and WHERE clause, which make up the CQL REGISTER QUERY clause, do.
Table 2-1 Correspondence between query actions and clauses in a CQL statement
Clause in a CQL statement | Query action |
---|
Stream clause | Specifies a stream operation action. |
SELECT and WHERE clauses | Specifies a relation operation action. |
FROM clause | Specifies a window operation action. |
The format for specifying a REGISTER QUERY clause is shown below:
REGISTER QUERY query-name
stream-clause (
SELECT-clause
FROM-clause
WHERE-clause); |
The following paragraphs explain the clauses in the REGISTER QUERY clause.
- Stream clause
- In the stream clause, you specify how to output the stream data. Depending on exactly what you want to do, you can specify an ISTREAM clause, a DSTREAM clause, or an RSTREAM clause. For details about these clauses, see 2.2.2(3) Stream operations (outputting the data processing results).
- SELECT clause
- In the SELECT clause, you specify the specific data to be extracted from the tuples that are to be processed. To identify the data you want, specify the data item names used in the schema specification character string of the REGISTER STREAM clause. The actual tuples to be processed are determined by the WHERE clause.
- FROM clause
- In the FROM clause, you specify the name of the stream that the query is to process, and the processing window. Depending on exactly what you want to do, you specify one of these four window types: a ROWS window, a RANGE window, a NOW window, or a PARTITION BY window.
- You specify the stream data using the stream name specified in the REGISTER STREAM clause. Following the stream name, you specify the window type and size enclosed in square brackets ([ ]). For details about windows, see 2.2.2(1) Window operations (retrieving data for analysis).
- For a query to process a relation, you must specify the name of the relation.
- WHERE clause
- In the WHERE clause, you specify the criteria for selecting which tuples retrieved from the window are to be processed.
For example, a query following the code for the temperature analysis system described above in (1) Defining a stream (REGISTER STREAM clause) is shown below.
REGISTER STREAM temperature_stream
(observation_time TIME, id INTEGER, temperature INTEGER);
REGISTER QUERY sensor_filter
ISTREAM (
SELECT id, temperature
FROM temperature_stream[ROWS 3]
WHERE id = 1); |
The following paragraphs explain the items specified in the above REGISTER QUERY clause.
- Query name
The query name is specified as sensor_filter. This is the name of the input stream used to acquire the stream data to be processed.
- Stream clause
The operator ISTREAM is specified as the stream operation.
- SELECT clause
The data items id and temperature are specified as the information to be retrieved from the tuples selected by the WHERE clause. These are data item names specified in the schema specification character string of the REGISTER STREAM clause.
- FROM clause
The line temperature_stream[ROWS 3] specifies the name of the stream and of the window to be processed by the query. The stream name is the one specified in the REGISTER STREAM clause. For the window, [ROWS 3] is specified. This means that three most recent tuples are to be retrieved from the stream data in descending time order.
- WHERE clause
Here, the condition id = 1 specifies which tuples are to be operated on from those that are retrieved in the window. This means that tuples with an ID equal to 1 are to be selected from the tuples retrieved in the window.
You can also specify other clauses in the REGISTER QUERY clause that are not described here. For details on these other clauses, see the uCosminexus Stream Data Platform - Application Framework Application Development Guide.