How to rename fields when unnesting arrays or flattening objects in JSON documents
In this recipe we’ll learn how to rename fields when unnesting/exploding values or flattening objects while ingesting JSON documents into Apache Pinot.
Pinot Version | 0.10.0 |
---|---|
Code | startreedata/pinot-recipes/json-unnest-rename-fields |
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/movies.json
We’re particularly interested in the Ratings
property of each movie.
We’re going to unnest or explode the ratings so that we have one row per rating in our database after the data is ingested.
We’ll also pull out the Meta.Released
property.
Pinot Schema and Table
Now let’s create a Pinot Schema and Table.
First, the schema:
config/schema.json
You can create the schema by running the following command:`
We’ll also have the following table config:
config/table.json
The existence of the complexTypeConfig
section means that nested JSON objects will be flattened into individual fields.
It also which field in the data source should be unnested.
The Ratings
field contains an array of JSON objects and that array will be exploded to create one row for each item in the array.
The transformConfigs
section maps the following values:
Ratings.Source
to theSource
columnRatings.Value
to theRating
columnMeta.Released
to theReleased
column.
You can create the table by running the following command:`
Ingestion Job
Now we’re going to import the JSON file into Pinot. We’ll do this with the following ingestion spec:
config/job-spec.yml
The import job will map fields in each JSON document to a corresponding column in the movie_ratings
schema. If one of the fields doesn’t exist in the schema it will be skipped.
You can run the following command to run the import:
Querying
Once that’s completed, navigate to localhost:9000/#/query and click on the movie_ratings
table or copy/paste the following query:
You will see the following output:
Rated | Rating | Released | Source | Title |
---|---|---|---|---|
R | 8.7/10 | 31 Mar 1999 | Internet Movie Database | The Matrix |
R | 88% | 31 Mar 1999 | Rotten Tomatoes | The Matrix |
R | 73/100 | 31 Mar 1999 | Metacritic | The Matrix |
PG-13 | 7.8/10 | 18 Dec 2009 | Internet Movie Database | Avatar |
PG-13 | 82% | 18 Dec 2009 | Rotten Tomatoes | Avatar |
PG-13 | 83/100 | 18 Dec 2009 | Metacritic | Avatar |
PG | 6.3/10 | 31 Mar 2017 | Internet Movie Database | The Boss Baby |
PG | 53% | 31 Mar 2017 | Rotten Tomatoes | The Boss Baby |
PG | 50/100 | 31 Mar 2017 | Metacritic | The Boss Baby |
TV-PG | 5.2/10 | 13 Sep 2019 | Internet Movie Database | Tall Girl |
Query Results
We can see that each movie has multiple rows, one for each rating given to that movie.