A lookup join adds to each row of a stream the latest value of an external table. When a
row arrives, Flink queries the external system by the key of the row.
The table does not need a time attribute, and Flink does not keep the table in state. Use
a lookup join for tables that are large or that change often.
A lookup join uses the processing time of the stream. Add a processing-time column to the
stream table, and use it in the FOR SYSTEM_TIME AS OF clause.
Cloudera SQL Stream Builder (SSB) supports the following connectors as lookup tables:
JDBC
Kudu
For more information, see JDBC connector and Joining Kafka and Kudu
tables.
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.
In the Explorer, open the menu of Jobs,
and click New Job.
The Create New Job window opens.
Enter a Job Name, or click Generate Job
Name.
Click Create.
The job opens in a new tab, with an empty SQL editor.
Enter the following statements in the SQL editor:
CREATE TABLE orders (
order_id INT,
customer_id INT,
total DOUBLE,
proc_time AS PROCTIME()
) WITH (
'connector' = 'datagen',
'rows-per-second' = '5',
'fields.order_id.kind' = 'sequence',
'fields.order_id.start' = '1',
'fields.order_id.end' = '100000',
'fields.customer_id.min' = '1',
'fields.customer_id.max' = '100'
);
CREATE TABLE customers (
id INT,
country STRING,
zip STRING,
PRIMARY KEY (id) NOT ENFORCED
) WITH (
'connector' = 'jdbc',
'url' = 'jdbc:postgresql://<database-host>:5432/shop',
'table-name' = 'public.customers',
'username' = '<user>',
'password' = '<password>',
'lookup.cache' = 'PARTIAL',
'lookup.partial-cache.max-rows' = '10000',
'lookup.partial-cache.expire-after-write' = '10 min'
);
CREATE TABLE enriched_orders (
order_id INT,
total DOUBLE,
country STRING,
zip STRING
) WITH (
'connector' = 'blackhole'
);
INSERT INTO enriched_orders
SELECT o.order_id, o.total, c.country, c.zip
FROM orders AS o
JOIN customers FOR SYSTEM_TIME AS OF o.proc_time AS c
ON o.customer_id = c.id;
Click the Execute job icon.
The job starts, and its status icon in the Explorer turns
green.
When you have finished, click Stop job.
In this example, customers is the lookup table. The FOR
SYSTEM_TIME AS OF o.proc_time clause means that each order gets the version of
the customer row at the time the order is processed. The lookup.cache
options keep recent rows in memory, which reduces how often the job queries the database. A cached row
can be out of date for as long as its expiry time.