Get Started Free
‹ Back to courses
course: ksqlDB 101

Splitting Two Streams with ksqlDB

2 min
Allison

Allison Walther

Integration Architect (Presenter)

Splitting Two Streams with ksqlDB

You can perform the inverse of merging streams with ksqlDB: you can split them. By specifying a predicate on a field in the data, you can route messages to different target streams.

Take the example of a stream of orders for all countries:

orders-for-all-countries

You may want to split these orders for country-specific processing:

orders-combined

To do this in ksqlDB, use SQL statements with appropriate predicates:

CREATE STREAM ORDERS_US AS
   SELECT * FROM ORDERS_COMBINED
   WHERE SOURCE = 'US';

CREATE STREAM ORDERS_UK AS
   SELECT * FROM ORDERS_COMBINED
   WHERE SOURCE = 'UK';

CREATE STREAM ORDERS_OTHER AS
   SELECT * FROM ORDERS_COMBINED
   WHERE SOURCE != 'US'
   AND SOURCE != 'UK';

Use the promo code KSQLDB101 & CONFLUENTDEV1 to get $25 of free Confluent Cloud usage and skip credit card entry.

Be the first to get updates and new content

We will only share developer content and updates, including notifications when new content is added. We will never send you sales emails. 🙂 By subscribing, you understand we will process your personal information in accordance with our Privacy Statement.

Splitting Two Streams with ksqlDB

Hi, I'm Allison Walther with Confluent. In this lesson, we'll be talking about splitting streams with ksqlDB. KsqlDB's create stream as feature allows us to create many different views of our data. We can create streams from a sub-set of events in another stream, or from a combination of events in multiple streams and/or tables. This feature makes it easy to split a stream of events based on some criteria. Here, we create one stream with a select that is only getting US orders, and we create another stream with a select for UK orders. Now, US and UK-specific applications can read from streams that are custom-built for them and not have to worry about filtering out events that don't apply. That's a wrap on this lesson. Let's jump into splitting streams in an exercise.