Introduction
An ARRAY represents a collection of elements. A common example is an order list belonging to a customer. One customer can have multiple orders, and each order contains several attributes.
We are building upon what we already have :
- Databricks series : Starting a new python project in Visual Studio Code (Part I)
- Databricks series : setting up a connection to Databricks from local IDE (Part II)
- Databricks series : Setting up a Databricks project in Visual Studio Code (part III)
- Databricks series : Deployment from Visual Studio Code (Part IV)
- Databricks series : Execute the job in Databricks (Part V)
- Databricks series : A configrable framework in Databricks (Part VI)
- Databricks series : Loading strategies in Databricks (Part VII)
- Databricks series : Data contracts in Databricks (Part VIII)
- Databricks series : Debugging Python code in Visual Studio Code with arguments (Part IX)
- Databricks series : ODCS Data contracts in Databricks (Part X)
- Databricks series : reading an API (Part XI)
- Databricks series : Building a mediallion architecture with SCD Type 2 (step 1)(Part XII)
- Databricks series : Building a mediallion architecture with SCD Type 2 (step 2) (Part XIII)
- Databricks series : Building a mediallion architecture with SCD Type 2 (step 3) (Part XIV)
- Databricks series : Working with JSON in Databricks SQL : STRUCT
An example
{ "customer_id": 1001, "name": "Jan Jansen", "orders": [ { "order_id": "ORD001", "amount": 125.50, "status": "COMPLETED" }, { "order_id": "ORD002", "amount": 89.95, "status": "OPEN" } ] }
The orders property is an ARRAY.
Each element inside the ARRAY is a STRUCT containing:
order_id
amount
status
The resulting Spark data type is conceptually:
Create a source table
Lets create a table :
CREATE OR REPLACE TABLE main.demo.customer_array_json ( customer_id BIGINT, payload STRING );
Insert the example data:
INSERT INTO main.demo.customer_array_json VALUES ( 1001, '{ "customer_id": 1001, "name": "Jan Jansen", "orders": [ { "order_id": "ORD001", "amount": 125.50, "status": "COMPLETED" }, { "order_id": "ORD002", "amount": 89.95, "status": "OPEN" } ] }' );
Parse the JSON
We use from_json() again, but this time we define an ARRAY containing STRUCT elements.
SELECT from_json( payload, 'STRUCT< customer_id: BIGINT, name: STRING, orders: ARRAY< STRUCT< order_id: STRING, amount: DOUBLE, status: STRING > > >' ) AS customer FROM main.demo.customer_array_json;
Expand the ARRAY using EXPLODE
SELECT customer.customer_id, customer.name, order.order_id, order.amount, order.status FROM ( SELECT from_json( payload, 'STRUCT< customer_id: BIGINT, name: STRING, orders: ARRAY< STRUCT< order_id: STRING, amount: DOUBLE, status: STRING > > >' ) AS customer FROM main.demo.customer_array_json ) parsed LATERAL VIEW EXPLODE(customer.orders) exploded AS order;
Notice what happened: one customer record became two rows because the customer has two orders. This is an important difference compared with STRUCT. A STRUCT extracts fields from an existing row, while EXPLODE expands an ARRAY into multiple rows.
Create a SQL View
We can make this transformation reusable by creating a view.
CREATE OR REPLACE VIEW main.demo.v_customer_orders AS SELECT customer.customer_id, customer.name, order.order_id, order.amount, order.status FROM ( SELECT from_json( payload, 'STRUCT< customer_id: BIGINT, name: STRING, orders: ARRAY< STRUCT< order_id: STRING, amount: DOUBLE, status: STRING > > >' ) AS customer FROM main.demo.customer_array_json ) parsed LATERAL VIEW EXPLODE(customer.orders) exploded AS order;
Query the view:
SELECT * FROM main.demo.v_customer_orders;
ARRAY is useful when a JSON document contains lists of repeating elements, such as:
Customer orders
Product items
Transaction lines
Event collections
API result lists
One important consideration: EXPLODE changes the granularity of the data. If one customer has five orders, the resulting dataset contains five rows for that customer.


Geen opmerkingen:
Een reactie posten