Joining Kafka and Kudu tables

Enrich a stream of orders from Kafka with metadata from a table in Kudu by using a lookup join, so that each order gets the latest metadata of its item.

  • Connect the deployment to the Cloudera Base on premises cluster that runs Kudu. For more information, see Using Cloudera Base Connector in the Anywhere Cloud documentation.
  • Add a Kudu catalog. In this example, the catalog is named kudu. For more information, see Adding a Kudu catalog.
  • Ensure that the Kudu table item_meta exists, with the columns id and info.
  • Ensure that the Kafka brokers are reachable, and that the orders topic exists. If the brokers need authentication, see Configuring Kafka authentication.

The orders topic contains JSON records with the columns order_number, price, order_time, and item_id.

  1. In the Streaming SQL Console, click Projects in the left navigation pane, and click the name of your project.
    The project opens, with the Explorer panel on the left. To open the Streaming SQL Console, see Accessing Cloudera SQL Stream Builder.
  2. In the Explorer, open the menu of Jobs, and click New Job.
    The Create New Job window opens.
  3. Enter a Job Name, or click Generate Job Name.
  4. Click Create.
    The job opens in a new tab, with an empty SQL editor.
  5. Enter the following statements in the SQL editor:
    CREATE TABLE orders (
      order_number STRING,
      price DOUBLE,
      order_time TIMESTAMP(3),
      item_id STRING,
      proc_time AS PROCTIME()
    ) WITH (
      'connector' = 'kafka',
      'properties.bootstrap.servers' = '<broker>:9093',
      'topic' = 'orders',
      'scan.startup.mode' = 'earliest-offset',
      'format' = 'json'
    );
    
    CREATE TABLE enriched_orders (
      order_time TIMESTAMP(3),
      item_id STRING,
      info STRING,
      price DOUBLE
    ) WITH (
      'connector' = 'blackhole'
    );
    
    INSERT INTO enriched_orders
    SELECT o.order_time, o.item_id, k.info, o.price
    FROM orders AS o
    JOIN kudu.`default`.item_meta FOR SYSTEM_TIME AS OF o.proc_time AS k
      ON o.item_id = k.id;
  6. Click the Execute job icon.
    The job starts, and its status icon in the Explorer turns green. In the Logs tab, check that the statements ran without errors.
  7. When you have finished, click Stop job.

Each order is joined with the latest info of its item, and the job writes the order time, the item, the info, and the price. The example writes to a Blackhole table. To read the result, replace the table with a Kafka table and read the topic.