Flink queries
Cloudera SQL Stream Builder (SSB) supports Flink SQL queries with filters, windows, joins, aggregations, Top-N, and deduplication on streaming data.
A query on a streaming table runs continuously.
For more information, see Flink DML.
| Query form | Description | Usage notes |
|---|---|---|
SELECT |
Reads columns and expressions from a table. | Supports projections, expressions, built-in functions, user-defined functions,
and table functions such as UNNEST. |
WHERE |
Filters rows with a Boolean condition. | Filter rows before aggregations and joins to reduce the amount of state that the job keeps. |
| Window aggregation | Groups rows into time windows with the TUMBLE,
HOP, CUMULATE, and SESSION window
table-valued functions, then aggregates each window. |
The table needs a time attribute. For more information, see Windowing and Event time and watermarks. |
JOIN |
Combines rows from two or more tables. | SSB supports regular joins, interval joins, temporal joins on event time or processing time, and lookup joins. For more information, see Creating regular joins, Creating interval joins, Creating temporal joins, and Creating lookup joins. |
GROUP BY |
Aggregates rows by key. | On streaming tables, the results update as new rows arrive. Write updating results to a sink that supports updates, such as an upsert sink. |
| Top-N | Returns the first N rows for each group, by an order that you set. | Use ROW_NUMBER() with an OVER window, and
filter the row number with QUALIFY. |
| Deduplication | Keeps the first or the last row for each key. | Use ROW_NUMBER() ordered by a time attribute, and keep row
number 1 with QUALIFY. |
ORDER BY |
Sorts the results. | On streaming tables, the first sort key must be a time attribute in ascending order. |
UNION ALL |
Combines the results of queries that have the same columns. |
Top-N query
The following query returns the three most expensive orders for each currency:
SELECT order_id, currency, price,
ROW_NUMBER() OVER (PARTITION BY currency ORDER BY price DESC) AS rownum
FROM orders
QUALIFY rownum <= 3;
Deduplication query
The following query keeps the first event for each order:
SELECT order_id, price, currency, order_time,
ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY order_time ASC) AS rownum
FROM orders
QUALIFY rownum = 1;
For more information, see Queries, Top-N, and Deduplication in the Apache Flink documentation.
