Streams and Queries Example
This page provides an example of streams
and queries
, and how multiple queries can be chained to one another.
There are multiple type of queries such as window query
, join query
, pattern query
, etc. Below example explains how pass-through
and selection queries
work. For more info refer the Stream Query Guide.
Example
The following is an example annotated with descriptive comments.
-- Defines `InputTemperatureStream` stream to pass events having `sensorId` and `temperature` attributes of types `string` and `double`.
CREATE STREAM InputTemperatureStream (sensorId string, temperature double);
-- Optional `@info` annotation to name the query.
@info(name = 'Pass-through')
-- Query to consume events from `InputTemperatureStream`, produce new events by selecting all the attributes from the incoming events, and outputs them to `TemperatureStream`.
insert into TemperatureAndSensorStream
select *
from InputTemperatureStream;
@info(name = 'Simple-selection')
-- Selects only the `temperature` attribute from events, and outputs to `TemperatureOnlyStream`.
-- Consumes events from `TemperatureAndSensorStream`. The schema of the stream is inferred from the previous query, hence no need to be defined.
insert into TemperatureOnlyStream
select temperature
from TemperatureAndSensorStream;
Events at Each Stream
When an event with values ['aq-14'
, 35.4
] is sent to InputTemperatureStream
stream, it is converted and travels through the streams as below.
- InputTemperatureStream : [
'aq-14'
,35.4
] - TemperatureAndSensorStream : ['aq-14',
35.4
] - TemperatureOnlyStream : [
35.4
]