How to Query a Table by Segment
In this recipe we’ll learn how to query a table to find out which records are in a particular segment.
Pinot Version | 1.0.0 |
---|---|
Code | startreedata/pinot-recipes/query-by-segment |
Prerequisites
To follow the code examples in this guide, you must install Docker locally and download recipes.
Navigate to recipe
- If you haven’t already, download recipes.
- In terminal, go to the recipe by running the following command:
Launch Pinot Cluster
You can spin up a Pinot Cluster by running the following command:
This command will run a single instance of the Pinot Controller, Pinot Server, Pinot Broker, Kafka, and Zookeeper. You can find the docker-compose.yml file on GitHub.
Data generator
This recipe contains a data generator that creates events with a timestamp, count, and UUID. You can generate data by running the following command:
Output is shown below:
Kafka ingestion
We’re going to ingest this data into an Apache Kafka topic using the kcat command line tool.
We’ll also use jq
to structure the data in the key:payload
structure that Kafka expects:
We can check that Kafka has some data by running the following command:
We’ll see something like the following:
Pinot Schema and Table
Now let’s create a Pinot Schema and Table.
First, the schema:
Now for the table config:
This highlighted section indicates that we’re going to create new segments after every 100,000 rows.
We’ll create the table by running the following:
Querying by segment
Once that’s been created, we can head over to the Pinot UI and run some queries.
Pinot has several built-in virtual columns inside every schema that can be used for debugging purposes:
Column Name | Column Type | Data Type | Description |
---|---|---|---|
$hostName | Dimension | STRING | Name of the server hosting the data |
$segmentName | Dimension | STRING | Name of the segment containing the record |
$docId | Dimension | INT | Document id of the record within the segment |
The one that’s useful for us is $segmentName
, which we can use like this to count the number of records in each segment:
$segmentName | count(*) |
---|---|
events__0__142__20230330T1004Z | 100000 |
events__0__27__20230330T1003Z | 100000 |
events__0__123__20230330T1004Z | 100000 |
events__0__15__20230330T1003Z | 100000 |
events__0__185__20230330T1004Z | 100000 |
events__0__96__20230330T1003Z | 100000 |
events__0__169__20230330T1004Z | 100000 |
events__0__160__20230330T1004Z | 100000 |
events__0__77__20230330T1003Z | 100000 |
events__0__71__20230330T1003Z | 100000 |
Query Results
We can then pick one of those segments and see what records are stored in that segment:
count | ts | uuid |
---|---|---|
666 | 2023-03-30 10:03:04.498 | 8e4f5bb3-ad5c-45ec-89cb-946acee52625 |
298 | 2023-03-30 10:03:04.498 | f439c3ad-86a1-4043-895e-681d5497cda0 |
128 | 2023-03-30 10:03:04.498 | 9e29317f-c8fd-44d8-8313-aa28a80f1e5e |
40 | 2023-03-30 10:03:04.498 | 697e2dbc-aee5-466e-b24e-763b18aff003 |
659 | 2023-03-30 10:03:04.498 | 8d981789-7e61-4a25-81d1-b62616e877e8 |
436 | 2023-03-30 10:03:04.498 | f4a883f3-3e6a-4169-a7ca-2469a5100ef2 |
408 | 2023-03-30 10:03:04.498 | dc21499e-cdb3-4fae-93c1-0b9bd0233ae0 |
457 | 2023-03-30 10:03:04.498 | 0de9a860-5af3-4155-93c9-d396df3cd0b3 |
197 | 2023-03-30 10:03:04.498 | c4ddd15a-814f-4036-9eee-3121f59783f8 |
362 | 2023-03-30 10:03:04.498 | 11b00463-2587-4838-9f05-089c05ad9fea |
Query Results