Streaming ETL

Data Filtering

KSQL streaming queries run continuously. You can persist the streaming query output to a Kafka topic by using the KSQL CREATE STREAM AS syntax. KSQL takes a real-time feed of events from one Kafka topic, transforms them and writes them continually to another.

This example shows how to filter data streaming data from an inbound topic to exclude records that originate from a particular geography.

Environment 4.1 or higher

Directions

Confluent Platform must be installed before using this recipe. KSQL is a component of Confluent Platform and the KSQL binaries are a part of the Confluent Platform bundle.

In this example, a source event stream named purchases is used.

{

  "order_id": 1,

  "customer_name": "Maryanna Andryszczak",

  "date_of_birth": "1922-06-06T02:21:59Z",

  "product": "Nut - Walnut, Pieces",

  "order_total_usd": "1.65",

  "town": "Portland",

  "country": "United States"

}

1. In KSQL, register the purchases stream:

ksql> CREATE STREAM purchases \

      (order_id INT, customer_name VARCHAR, date_of_birth VARCHAR, \

      product VARCHAR, order_total_usd VARCHAR, \
      town VARCHAR, country VARCHAR) \

      WITH (KAFKA_TOPIC='purchases', VALUE_FORMAT='JSON');



Message

----------------

Stream created

----------------


2. Create a KSQL query that returns messages from three countries:

ksql> SELECT COUNTRY, COUNT(*) FROM PURCHASES GROUP BY COUNTRY;

United Kingdom | 248

Germany | 420

United States | 3334

3. Create a derived topic that excludes data for customers from the UK:

ksql> CREATE STREAM PURCHASES_EXCL_UK AS \
      
SELECT * FROM PURCHASES \

      WHERE COUNTRY!='United Kingdom';



Message

----------------------------

Stream created and running

----------------------------

4. Query the Kafka topic and you will see that message from the UK are now excluded:

ksql> SELECT COUNTRY, COUNT(*) FROM PURCHASES_EXCL_UK GROUP BY COUNTRY;

Germany | 420

United States | 3334
< Back to the Stream Processing Cookbook

We use cookies to understand how you use our site and to improve your experience. Click here to learn more or change your cookie settings. By continuing to browse, you agree to our use of cookies.