Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: rewrite pull query docs page #7532

Merged
merged 10 commits into from
May 27, 2021
70 changes: 40 additions & 30 deletions docs/developer-guide/ksqldb-reference/select-pull-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ Synopsis

```sql
SELECT select_expr [, ...]
FROM aggregate_table
FROM table
WHERE key_column=key [AND ...]
[AND window_bounds];
```

Description
-----------

Pulls the current value from the materialized table and terminates. The result
Pulls the current value from the materialized view and terminates. The result
of this statement isn't persisted in a {{ site.ak }} topic and is printed out
only in the console.

Expand All @@ -36,13 +36,38 @@ Execute a pull query by sending an HTTP request to the ksqlDB REST API, and
the API responds with a single response.

The WHERE clause must contain a value for each primary-key column to retrieve and may
optionally include bounds on `WINDOWSTART` and `WINDOWEND` if the materialized table is windowed.
optionally include bounds on `WINDOWSTART` and `WINDOWEND` if the materialized view is windowed.
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
For more information, see
[Time and Windows in ksqlDB](../../concepts/time-and-windows-in-ksqldb-queries.md).

Example
-------

You can issue a pull query against a derived table that was created by using
the [CREATE TABLE AS SELECT](../../ksqldb-reference/create-table-as-select)
statement. First, create a table named `GRADES` by using a [CREATE TABLE](../../ksqldb-reference/create-table)
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
statement:
```sql
CREATE TABLE GRADES (ID INT PRIMARY KEY, GRADE STRING, RANK INT)
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
WITH (kafka_topic = 'test_topic', value_format = 'JSON', partitions = 1);
```
Then, create a derived table named `TOP_TEN_RANKS` by using a
[CREATE TABLE AS SELECT](../../ksqldb-reference/create-table-as-select) statement:
```sql
CREATE TABLE TOP_TEN_RANKS
AS SELECT ID, RANK
FROM GRADES
WHERE RANK <= 10;
```
You can fetch the current state of your materialized view `TOP_TEN_RANKS` by using a pull query:
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
```sql
SELECT * FROM TOP_TEN_RANKS;
```
If you want to look up only the student with `ID = 5` in the materialized view:
```sql
SELECT * FROM TOP_TEN_RANKS
WHERE ID = 5;
```
If the materialized view `pageviews_by_region` is windowed:
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
```sql
SELECT * FROM pageviews_by_region
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
WHERE regionId = 'Region_1'
Expand Down Expand Up @@ -77,35 +102,20 @@ time zone.
If no bounds are placed on `WINDOWSTART` or `WINDOWEND`, rows are returned for all windows
in the windowed table.

Also, you can issue a pull query against a derived table that was created by using the [CREATE TABLE AS SELECT](../../ksqldb-reference/create-table-as-select) statement.


You can also issue pull queries against materialized views that are created using
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
joining multiple tables:
```sql
CREATE TABLE GRADES (ID INT PRIMARY KEY, GRADE STRING, RANK INT)
WITH (kafka_topic = 'test_topic', value_format = 'JSON', partitions = 1);
CREATE TABLE LEFT_TABLE (ID BIGINT PRIMARY KEY, NAME varchar, VALUE bigint)
WITH (kafka_topic='left_topic', value_format='json', partitions=4);
```
Create a derived table, named
`TOP_TEN_RANKS`, by using a [CREATE TABLE AS SELECT](../../ksqldb-reference/create-table-as-select) statement:

```sql
CREATE TABLE TOP_TEN_RANKS
AS SELECT ID, RANK
FROM GRADES
WHERE RANK <= 10;
```
You can fetch the current state of your materialized view, which is
the `TOP_TEN_RANKS` derived table, by using a pull query:

```sql
SELECT * FROM TOP_TEN_RANKS;
CREATE TABLE RIGHT_TABLE (ID BIGINT PRIMARY KEY, F1 varchar, F2 bigint)
WITH (kafka_topic='right_topic', value_format='json', partitions=4);
```
The following statement looks up only the student with `ID = 5` in the derived table:

```sql
SELECT * FROM TOP_TEN_RANKS
WHERE ID = 5;
CREATE TABLE INNER_JOIN AS SELECT L.ID, NAME, VALUE, F1, F2 FROM LEFT_TABLE L JOIN RIGHT_TABLE R ON L.ID = R.ID;
```
You can fetch the current state of your materialized view `INNER_JOIN` by using a pull query:
cprasad1 marked this conversation as resolved.
Show resolved Hide resolved
```sql
SELECT * FROM INNER_JOIN [ WHERE where_condition ];
```
!!! note
Currently, tables derived using Table-Table joins aren't queryable directly. To derive a queryable table, you can do:
`CREATE TABLE QUERYABLE_JOIN_TABLE AS SELECT * FROM JOIN_TABLE;` and then issue pull queries against `QUERYABLE_JOIN_TABLE`.