JSON Transformation Functions
In this recipe we’ll learn how to use JSON transformation functions to extract values from nested JSON documents during the data ingestion process.
Pinot Version | 0.9.3 |
---|---|
Code | startreedata/pinot-recipes/json-transformation-functions |
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, and Zookeeper. You can find the docker-compose.yml file on GitHub.
Dataset
We’re going to import the following JSON file:
data/import.json
Pinot Schema and Table
Now let’s create a Pinot Schema and Table.
First, the schema:
config/schema.json
The subjects
and grades
columns will both contains arrays of values, which we can configure by setting "singleValueField":false
.
We’ll also have the following table config:
config/table.json
In this config we define transform configs (ingestionConfig.transformConfigs
) to extract the subject names and grades from the subjectAndGrades
property, using the jsonPathArray function. We also define one to extract the age
from the meta
property using the JSONPATHLONG function.
You can create the table and schema by running the following command:
You should see a message similar to the following if everything is working correctly:
Ingestion Job
Now we’re going to import the JSON file into Pinot. We’ll do this with the following ingestion spec:
config/table.json
The import job will map fields in each JSON document to a corresponding column in the people
schema. If one of the fields doesn’t exist in the schema it will be skipped.
In this case the name
field will be automatically mapped to the name
column. The subjectAndGrades
field is processed by transformation functions and the values are imported into the subjects
and grades
columns. The meta
field is processed by a transformation function to extract the age
property, which is stored in the age
column.
You can run the following command to run the import:
Querying
Once that’s completed, navigate to localhost:9000/#/query and click on the people
table or copy/paste the following query:
You will see the following output:
age | grades | name | subjects |
---|---|---|---|
24 | A,B | Pete | Maths,English |
28 | A,C | John | Maths,Computer Science |
Query Results