liveBook · Manning

SQL

As explained in Apache Kafka in Action

An SQL query is a declarative statement that specifies data to retrieve, transform, or aggregate.

SQL is a declarative language for expressing data retrieval, transformation, and aggregation. A few lines can replace much imperative or functional code, while leaving efficient execution to the database or processing engine. section 12.3

In relational databases, a query usually returns a result when requested. In stream processing, a query consumes events and typically produces an ongoing, potentially infinite stream of changes. Tables can retain state and expose a complete result at a point in time. section 12.3

Use SQL in stream processing to:

  • Connect a table definition to a Kafka topic rather than create an empty database table. section 12.3
  • Filter events, such as selecting products whose price exceeds 100. section 12.3
  • Aggregate an event stream, such as counting price changes per product. section 12.3
  • Balance expressive queries against coupling: shared relational databases can make services depend heavily on one another. section 17.4.1

Filtering and aggregating product-price events

Define a Flink SQL table over the products.prices.changelog Kafka topic, then run `SELECT * FROM productprices WHERE price > 100;` to retain expensive products or `SELECT product_id, COUNT(*) AS num_price_changes FROM productprices GROUP BY product_id;` to count changes per product. section 12.3

CREATE TABLE productprices (
  product_id INT,
  product_name STRING,
  price FLOAT,
  oldPrice FLOAT
) WITH (
  'connector' = 'kafka',
  'topic' = 'products.prices.changelog',
  'properties.bootstrap.servers' = 'localhost:9092',
  'format' = 'json',
  'key.format' = 'json',
  'key.fields' = 'product_id',
  'value.fields-include' = 'EXCEPT_KEY'
);

Inspect the complete CREATE TABLE definition, including Kafka connector, topic, JSON format, key fields, and schema. section 12.3

How Apache Kafka in Action covers it

How deep it goes
Contrasts database and streaming semantics and demonstrates filtering and aggregation, but defers general SQL and Flink SQL syntax.
Examples
Uses a Kafka-backed product-price table, a price filter, and a per-product price-change count.
What it assumes
Assumes awareness of databases and Kafka while introducing stream-versus-table behavior through concrete product-price operations.
What you can do after
Apply it via Flink SQL to define Kafka-backed tables, filter events, and aggregate changes.
How it connects
Connects SQL with relational databases, stream processing, Kafka, tables, and event history.

Also explained in

Compare how 25 books explain SQL →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Apache Kafka in Action. The section links point to the parts of the book it draws on.