KafkaIQ

10.7.1 – Stream-Stream Joins

Module: Section 10 – Kafka Ecosystem & Tools
Section: 10.7 – Joins & Enrichment with ksqlDB
Goal: Master stream-stream joins for temporal event correlation


Stream-Stream Join Syntax

SELECT *
FROM stream1 s1
INNER JOIN stream2 s2 WITHIN 1 HOUR
  ON s1.key = s2.key
EMIT CHANGES;

Key Requirement: WITHIN clause (time window)


Example: Order + Payment Matching

CREATE STREAM orders (
  order_id VARCHAR KEY,
  user_id BIGINT,
  amount DOUBLE,
  timestamp BIGINT
) WITH (...);

CREATE STREAM payments (
  payment_id VARCHAR KEY,
  order_id VARCHAR,
  amount DOUBLE,
  timestamp BIGINT
) WITH (...);

CREATE STREAM confirmed_orders AS
SELECT
  o.order_id,
  o.amount AS order_amount,
  p.payment_id,
  p.amount AS payment_amount
FROM orders o
INNER JOIN payments p WITHIN 10 MINUTES
  ON o.order_id = p.order_id
EMIT CHANGES;

Result: Only orders with matching payment within 10 minutes


Join Types

INNER JOIN: Only matching events

INNER JOIN stream2 WITHIN 1 HOUR ON ...

LEFT JOIN: All left events, matched right events

LEFT JOIN stream2 WITHIN 1 HOUR ON ...

OUTER JOIN: All events from both streams

FULL OUTER JOIN stream2 WITHIN 1 HOUR ON ...

Key Takeaways

  1. WITHIN Clause: Required for stream-stream joins
  2. Time Window: Events joined if within window
  3. Use Cases: Event correlation, matching flows

What's Next?

Next Module: 10.7.2 – Stream-Table Joins