SlideShare una empresa de Scribd logo
1 de 44
Descargar para leer sin conexión
JSON + Relational
How to use hybrid data models
Rob Hedgpeth, Developer Evangelist
MariaDB Corporation
About me
I’m not a morning person,
but I like breakfast.
What’s the point?
What’s the point?
About me
I’m not a NoSQL person,
but I like JSON.
JSON
Relational
Flexibility
Simplicity
Ubiquity
Data integrity
Transactions
Reliability
What’s the difference?
Structured data is externally described
Semi-structured data is self described
Structured or semi-structured data
Structured or semi-structured data
Sports venues Attractions Restaurants
A data model can be comprised of
structured and semi-structured data.
Structured and semi-structured data
with relational + JSON
Structured Semi-structured
name latitude longitude attr
Lou Malnati’s 42.0021628 -87.7255662 { “foodType”: “Pizza”, “menu”: “loumalnatis.com/our-menu” }
name latitude longitude attr
Lou Malnati’s 42.0021628 -87.7255662 { “foodType”: “Pizza”, “menu”: “loumalnatis.com/our-menu” }
Structured and semi-structured data
with relational + JSON
Structure data described
by the schema
Structured described
by the data
Creating and querying
JSON documents
Definition
CREATE TABLE locations (
id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(100) NOT NULL,
description varchar(500),
type char(1) NOT NULL,
latitude decimal(9, 4) NOT NULL,
longitude decimal(9, 4) NOT NULL,
attr longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL
CHECK(json_valid(attr)),
PRIMARY KEY (id))
ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4;
Creating Records
INSERT INTO locations (type, name, latitude, longitude, attr) VALUES
('R', 'Lou Malnatis', 42.0021628, -87.7255662,'{"details": {"foodType": "Pizza", "menu":
"https://www.loumalnatis.com/our-menu"},
"favorites": [{"description": "Pepperoni deep dish", "price": 18.75}, {"description":
"The Lou", "price": 24.75}]}');
INSERT INTO locations (type, name, latitude, longitude, attr) VALUES
('A', 'Cloud Gate', 41.8826572, -87.6233039, '{"category": "Landmark", "lastVisitDate":
"11/10/2019"}');
Reading Fields
SELECT name, latitude, longitude,
JSON_VALUE(attr, '$.details.foodType') AS food_type
FROM locations
WHERE type = 'R';
name latitude longitude food_type
Lou Malnati’s 42.0021628 -87.7255662 Pizza
Null Fields
SELECT type, name, latitude, longitude,
JSON_VALUE(attr,'$.details.foodType') AS food_type
FROM locations;
type name latitude longitude food_type
R Lou Malnati’s 42.0021628 -87.7255662 Pizza
A Willis Tower 41.8788764 -87.6359149 NULL
Contains Path
SELECT name, latitude, longitude,
JSON_VALUE(attr, '$.details.menu') AS menu
FROM locations
WHERE type = 'R' AND
JSON_CONTAINS_PATH(attr, 'one', '$.details.menu') = 1;
name latitude longitude menu
Lou Malnati’s 42.0021628 -87.7255662 https://www.loumalnatis.com...
Contains Value
SELECT name, latitude, longitude
FROM locations
WHERE type = 'A' AND
JSON_CONTAINS(attr, '"Landmark"', '$.category') = 1;
name latitude longitude
Willis Tower 41.8788764 -87.6359149
Using JSON to filter
SELECT id, name, latitude, longitude
FROM locations
WHERE type = 'S' AND
JSON_VALUE(attr, '$.details.yearOpened') = 1924;
id name latitude longitude
7 Soldier Field 41.8623132 -87.6166884
Reading objects
SELECT name, description,
JSON_QUERY(attr, '$.details') AS details
FROM locations
WHERE type = 'R'
name description details
The Publican An award winning
American style
restaurant
{
"foodType": "American",
"menu": "thepublicanrestaurant.com/menu"
}
SELECT name, description,
JSON_QUERY(attr, '$.teams') AS home_teams
FROM locations
WHERE type = 'S';
name description home_teams
United Center An indoor arena on the
Near West Side of Chicago
[
"Bulls",
"Blackhawks"
]
Reading Arrays
Reading array elements
SELECT name, description,
JSON_VALUE(attr, '$.teams[0]') AS primary_home_team
FROM locations
WHERE type = 'S';
name description primary_home_team
United Center An indoor arena on the Near
West Side of Chicago
Bulls
Reading objects
SELECT name,
JSON_QUERY(attr, '$.teams') AS home_teams,
JSON_QUERY(attr, '$.details') AS details
FROM locations
WHERE type = 'S';
name home_teams details
United Center [
"Bulls",
"Blackhawks"
]
{
"capacity": 23500,
"yearOpened": 1994
}
Indexing (1/2)
ALTER TABLE locations ADD COLUMN
food_type VARCHAR(25) AS (JSON_VALUE(attr, '$.details.foodType')) VIRTUAL;
EXPLAIN SELECT name, latitude, longitude
FROM locations
WHERE food_type = 'Mexican';
id select_type table type possible_keys
1 SIMPLE Locations ALL NULL
Indexing (2/2)
CREATE INDEX foodtypes ON locations(food_type);
EXPLAIN SELECT name, latitude, longitude
FROM locations
WHERE food_type = 'Mexican';
id select_type table ref possible_keys
1 SIMPLE Locations ref foodtypes
Modifying JSON
documents
Inserting fields
UPDATE locations
SET attr = JSON_INSERT(attr, '$.nickname',
'The Bean')
WHERE id = 8;
name nickname
Cloud Gate The Bean
SELECT name,
JSON_VALUE(attr, '$.nickname') AS
nickname
FROM locations
WHERE type = 'A';
Inserting array
UPDATE locations
SET attr =JSON_INSERT(attr, '$.foodTypes',
JSON_ARRAY('Asian', 'Mexican'))
WHERE id = 1;
name food_types
Saucy Porka [
"Asian",
"Mexican"
]
SELECT name,
JSON_QUERY(attr, '$.foodTypes')
AS food_types
FROM locations
WHERE id = 1;
Adding array elements
UPDATE locations
SET attr = JSON_ARRAY_APPEND(attr,
'$.foodTypes', 'German’)
WHERE id = 1;
SELECT name, format, price,
JSON_QUERY(attr, '$.foodTypes')
AS food_types
FROM locations
WHERE id = 1;
name food_types
Saucy Porka [
"Asian",
"Mexican",
"German"
]
Removing array elements
UPDATE locations
SET attr = JSON_REMOVE(attr, '$.foodTypes[2]')
WHERE id = 1;
SELECT name,
JSON_QUERY(attr, '$.foodTypes')
AS food_types
FROM locations
WHERE id = 1;
name food_types
Saucy Porka [
"Asian",
"Mexican"
]
Creating JSON documents
from relational data
Returning relational data as JSON
SELECT JSON_OBJECT('name', name, 'latitude', latitude, 'longitude', longitude) AS data
FROM locations
WHERE type = 'S';
data
{
"name": "Wrigley Field",
"latitude": 41.9484384,
"longitude": -87.6553327
}
Relational + JSON
SELECT JSON_OBJECT('name', name, 'latitude', latitude, 'longitude', longitude, 'capacity',
JSON_VALUE(attr, '$.details.capacity')) AS data
FROM locations
WHERE type = 'S';
data
{
"name": "Wrigley Field",
"latitude": 41.9484384,
"longitude": -87.6553327
“capacity": 41649
}
Merging relational and JSON
SELECT JSON_MERGE(
JSON_OBJECT(
'name', name,
'latitude', latitude,
'longitude', longitude),
attr) AS data
FROM locations
WHERE type = 'R';
data
{
"name": "The Publican",
"latitude": ,
"longitude": ,
"details": {
"foodType": "American",
"menu": "http://www.thepublicanrestaurant.com/menu"},
"favorites": [
{"description": "Ribs with fries", "price": 16.99},
{"description": "BBQ Burger", "price": 12.99}
]
}
Enforcing data integrity
with JSON documents
JSON Constraints
ALTER TABLE locations ADD CONSTRAINT check_attr
CHECK (
type != 'S' or (type = 'S' and
JSON_TYPE(JSON_QUERY(attr, '$.details')) = 'OBJECT' and
JSON_TYPE(JSON_QUERY(attr, '$.details.events')) = 'ARRAY' and
JSON_TYPE(JSON_VALUE(attr, '$.details.yearOpened')) = 'INTEGER' and
JSON_TYPE(JSON_VALUE(attr, '$.details.capacity')) = 'INTEGER' and
JSON_EXISTS(attr, '$.details.yearOpened') = 1 and
JSON_EXISTS(attr, '$.details.capacity') = 1 and
JSON_LENGTH(JSON_QUERY(attr, '$.details.events')) > 0));
JSON Constraints
INSERT INTO locations (type, name, latitude, longitude, attr) VALUES
('S', 'Wrigley Field', 41.9484384, -87.6553327, '{"details": {"yearOpened": 1914, "capacity":
41649}');
ERROR 4025 (23000): CONSTRAINT `check_attr` failed for `test`.`locations`
no teams field!
JSON Constraints
INSERT INTO locations (type, name, latitude, longitude, attr) VALUES
('S', 'Wrigley Field', 41.9484384, -87.6553327, '{"details": {"yearOpened": "Last Year",
"capacity": 41649}, "teams": ["Cubs"]');
ERROR 4038 (HY000): Syntax error in JSON text in argument 1 to function 'json_type' at
position 1
"Last Year" is not a number!
Trying it out
Application Architecture
Front Middle
MariaDB
Back
Try it out!
https://github.com/mariadb-corporation/Developer-Examples
Webinar attendees only
1. Find two JSON functions that were not mentioned in this
presentation.
2. Email developers@mariadb.com and include:
a. Function name
b. Description of what the function is used for
3. Get swag!
Developer
Challenge!
THANK YOU!

Más contenido relacionado

Similar a JSON + Relational – How to Use Hybrid Data Models

JSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worldsJSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worldsoysteing
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseMarco Gralike
 
Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2Marco Gralike
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Kris Wallsmith
 
UKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseUKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseMarco Gralike
 

Similar a JSON + Relational – How to Use Hybrid Data Models (9)

JSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worldsJSON_TABLE -- The best of both worlds
JSON_TABLE -- The best of both worlds
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory Database
 
Drupal Mobile
Drupal MobileDrupal Mobile
Drupal Mobile
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
 
Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2
 
Json
JsonJson
Json
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
UKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseUKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the Database
 
quick json parser
quick json parserquick json parser
quick json parser
 

Más de DATAVERSITY

Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...DATAVERSITY
 
Data at the Speed of Business with Data Mastering and Governance
Data at the Speed of Business with Data Mastering and GovernanceData at the Speed of Business with Data Mastering and Governance
Data at the Speed of Business with Data Mastering and GovernanceDATAVERSITY
 
Exploring Levels of Data Literacy
Exploring Levels of Data LiteracyExploring Levels of Data Literacy
Exploring Levels of Data LiteracyDATAVERSITY
 
Building a Data Strategy – Practical Steps for Aligning with Business Goals
Building a Data Strategy – Practical Steps for Aligning with Business GoalsBuilding a Data Strategy – Practical Steps for Aligning with Business Goals
Building a Data Strategy – Practical Steps for Aligning with Business GoalsDATAVERSITY
 
Make Data Work for You
Make Data Work for YouMake Data Work for You
Make Data Work for YouDATAVERSITY
 
Data Catalogs Are the Answer – What is the Question?
Data Catalogs Are the Answer – What is the Question?Data Catalogs Are the Answer – What is the Question?
Data Catalogs Are the Answer – What is the Question?DATAVERSITY
 
Data Catalogs Are the Answer – What Is the Question?
Data Catalogs Are the Answer – What Is the Question?Data Catalogs Are the Answer – What Is the Question?
Data Catalogs Are the Answer – What Is the Question?DATAVERSITY
 
Data Modeling Fundamentals
Data Modeling FundamentalsData Modeling Fundamentals
Data Modeling FundamentalsDATAVERSITY
 
Showing ROI for Your Analytic Project
Showing ROI for Your Analytic ProjectShowing ROI for Your Analytic Project
Showing ROI for Your Analytic ProjectDATAVERSITY
 
How a Semantic Layer Makes Data Mesh Work at Scale
How a Semantic Layer Makes  Data Mesh Work at ScaleHow a Semantic Layer Makes  Data Mesh Work at Scale
How a Semantic Layer Makes Data Mesh Work at ScaleDATAVERSITY
 
Is Enterprise Data Literacy Possible?
Is Enterprise Data Literacy Possible?Is Enterprise Data Literacy Possible?
Is Enterprise Data Literacy Possible?DATAVERSITY
 
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...DATAVERSITY
 
Emerging Trends in Data Architecture – What’s the Next Big Thing?
Emerging Trends in Data Architecture – What’s the Next Big Thing?Emerging Trends in Data Architecture – What’s the Next Big Thing?
Emerging Trends in Data Architecture – What’s the Next Big Thing?DATAVERSITY
 
Data Governance Trends - A Look Backwards and Forwards
Data Governance Trends - A Look Backwards and ForwardsData Governance Trends - A Look Backwards and Forwards
Data Governance Trends - A Look Backwards and ForwardsDATAVERSITY
 
Data Governance Trends and Best Practices To Implement Today
Data Governance Trends and Best Practices To Implement TodayData Governance Trends and Best Practices To Implement Today
Data Governance Trends and Best Practices To Implement TodayDATAVERSITY
 
2023 Trends in Enterprise Analytics
2023 Trends in Enterprise Analytics2023 Trends in Enterprise Analytics
2023 Trends in Enterprise AnalyticsDATAVERSITY
 
Data Strategy Best Practices
Data Strategy Best PracticesData Strategy Best Practices
Data Strategy Best PracticesDATAVERSITY
 
Who Should Own Data Governance – IT or Business?
Who Should Own Data Governance – IT or Business?Who Should Own Data Governance – IT or Business?
Who Should Own Data Governance – IT or Business?DATAVERSITY
 
Data Management Best Practices
Data Management Best PracticesData Management Best Practices
Data Management Best PracticesDATAVERSITY
 
MLOps – Applying DevOps to Competitive Advantage
MLOps – Applying DevOps to Competitive AdvantageMLOps – Applying DevOps to Competitive Advantage
MLOps – Applying DevOps to Competitive AdvantageDATAVERSITY
 

Más de DATAVERSITY (20)

Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
 
Data at the Speed of Business with Data Mastering and Governance
Data at the Speed of Business with Data Mastering and GovernanceData at the Speed of Business with Data Mastering and Governance
Data at the Speed of Business with Data Mastering and Governance
 
Exploring Levels of Data Literacy
Exploring Levels of Data LiteracyExploring Levels of Data Literacy
Exploring Levels of Data Literacy
 
Building a Data Strategy – Practical Steps for Aligning with Business Goals
Building a Data Strategy – Practical Steps for Aligning with Business GoalsBuilding a Data Strategy – Practical Steps for Aligning with Business Goals
Building a Data Strategy – Practical Steps for Aligning with Business Goals
 
Make Data Work for You
Make Data Work for YouMake Data Work for You
Make Data Work for You
 
Data Catalogs Are the Answer – What is the Question?
Data Catalogs Are the Answer – What is the Question?Data Catalogs Are the Answer – What is the Question?
Data Catalogs Are the Answer – What is the Question?
 
Data Catalogs Are the Answer – What Is the Question?
Data Catalogs Are the Answer – What Is the Question?Data Catalogs Are the Answer – What Is the Question?
Data Catalogs Are the Answer – What Is the Question?
 
Data Modeling Fundamentals
Data Modeling FundamentalsData Modeling Fundamentals
Data Modeling Fundamentals
 
Showing ROI for Your Analytic Project
Showing ROI for Your Analytic ProjectShowing ROI for Your Analytic Project
Showing ROI for Your Analytic Project
 
How a Semantic Layer Makes Data Mesh Work at Scale
How a Semantic Layer Makes  Data Mesh Work at ScaleHow a Semantic Layer Makes  Data Mesh Work at Scale
How a Semantic Layer Makes Data Mesh Work at Scale
 
Is Enterprise Data Literacy Possible?
Is Enterprise Data Literacy Possible?Is Enterprise Data Literacy Possible?
Is Enterprise Data Literacy Possible?
 
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
 
Emerging Trends in Data Architecture – What’s the Next Big Thing?
Emerging Trends in Data Architecture – What’s the Next Big Thing?Emerging Trends in Data Architecture – What’s the Next Big Thing?
Emerging Trends in Data Architecture – What’s the Next Big Thing?
 
Data Governance Trends - A Look Backwards and Forwards
Data Governance Trends - A Look Backwards and ForwardsData Governance Trends - A Look Backwards and Forwards
Data Governance Trends - A Look Backwards and Forwards
 
Data Governance Trends and Best Practices To Implement Today
Data Governance Trends and Best Practices To Implement TodayData Governance Trends and Best Practices To Implement Today
Data Governance Trends and Best Practices To Implement Today
 
2023 Trends in Enterprise Analytics
2023 Trends in Enterprise Analytics2023 Trends in Enterprise Analytics
2023 Trends in Enterprise Analytics
 
Data Strategy Best Practices
Data Strategy Best PracticesData Strategy Best Practices
Data Strategy Best Practices
 
Who Should Own Data Governance – IT or Business?
Who Should Own Data Governance – IT or Business?Who Should Own Data Governance – IT or Business?
Who Should Own Data Governance – IT or Business?
 
Data Management Best Practices
Data Management Best PracticesData Management Best Practices
Data Management Best Practices
 
MLOps – Applying DevOps to Competitive Advantage
MLOps – Applying DevOps to Competitive AdvantageMLOps – Applying DevOps to Competitive Advantage
MLOps – Applying DevOps to Competitive Advantage
 

Último

Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...shambhavirathore45
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Onlineanilsa9823
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Delhi Call girls
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 

Último (20)

Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 

JSON + Relational – How to Use Hybrid Data Models

  • 1. JSON + Relational How to use hybrid data models Rob Hedgpeth, Developer Evangelist MariaDB Corporation
  • 2. About me I’m not a morning person, but I like breakfast.
  • 5. About me I’m not a NoSQL person, but I like JSON.
  • 7. What’s the difference? Structured data is externally described Semi-structured data is self described
  • 9. Structured or semi-structured data Sports venues Attractions Restaurants
  • 10. A data model can be comprised of structured and semi-structured data.
  • 11. Structured and semi-structured data with relational + JSON Structured Semi-structured name latitude longitude attr Lou Malnati’s 42.0021628 -87.7255662 { “foodType”: “Pizza”, “menu”: “loumalnatis.com/our-menu” }
  • 12. name latitude longitude attr Lou Malnati’s 42.0021628 -87.7255662 { “foodType”: “Pizza”, “menu”: “loumalnatis.com/our-menu” } Structured and semi-structured data with relational + JSON Structure data described by the schema Structured described by the data
  • 14. Definition CREATE TABLE locations ( id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, name varchar(100) NOT NULL, description varchar(500), type char(1) NOT NULL, latitude decimal(9, 4) NOT NULL, longitude decimal(9, 4) NOT NULL, attr longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK(json_valid(attr)), PRIMARY KEY (id)) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4;
  • 15. Creating Records INSERT INTO locations (type, name, latitude, longitude, attr) VALUES ('R', 'Lou Malnatis', 42.0021628, -87.7255662,'{"details": {"foodType": "Pizza", "menu": "https://www.loumalnatis.com/our-menu"}, "favorites": [{"description": "Pepperoni deep dish", "price": 18.75}, {"description": "The Lou", "price": 24.75}]}'); INSERT INTO locations (type, name, latitude, longitude, attr) VALUES ('A', 'Cloud Gate', 41.8826572, -87.6233039, '{"category": "Landmark", "lastVisitDate": "11/10/2019"}');
  • 16. Reading Fields SELECT name, latitude, longitude, JSON_VALUE(attr, '$.details.foodType') AS food_type FROM locations WHERE type = 'R'; name latitude longitude food_type Lou Malnati’s 42.0021628 -87.7255662 Pizza
  • 17. Null Fields SELECT type, name, latitude, longitude, JSON_VALUE(attr,'$.details.foodType') AS food_type FROM locations; type name latitude longitude food_type R Lou Malnati’s 42.0021628 -87.7255662 Pizza A Willis Tower 41.8788764 -87.6359149 NULL
  • 18. Contains Path SELECT name, latitude, longitude, JSON_VALUE(attr, '$.details.menu') AS menu FROM locations WHERE type = 'R' AND JSON_CONTAINS_PATH(attr, 'one', '$.details.menu') = 1; name latitude longitude menu Lou Malnati’s 42.0021628 -87.7255662 https://www.loumalnatis.com...
  • 19. Contains Value SELECT name, latitude, longitude FROM locations WHERE type = 'A' AND JSON_CONTAINS(attr, '"Landmark"', '$.category') = 1; name latitude longitude Willis Tower 41.8788764 -87.6359149
  • 20. Using JSON to filter SELECT id, name, latitude, longitude FROM locations WHERE type = 'S' AND JSON_VALUE(attr, '$.details.yearOpened') = 1924; id name latitude longitude 7 Soldier Field 41.8623132 -87.6166884
  • 21. Reading objects SELECT name, description, JSON_QUERY(attr, '$.details') AS details FROM locations WHERE type = 'R' name description details The Publican An award winning American style restaurant { "foodType": "American", "menu": "thepublicanrestaurant.com/menu" }
  • 22. SELECT name, description, JSON_QUERY(attr, '$.teams') AS home_teams FROM locations WHERE type = 'S'; name description home_teams United Center An indoor arena on the Near West Side of Chicago [ "Bulls", "Blackhawks" ] Reading Arrays
  • 23. Reading array elements SELECT name, description, JSON_VALUE(attr, '$.teams[0]') AS primary_home_team FROM locations WHERE type = 'S'; name description primary_home_team United Center An indoor arena on the Near West Side of Chicago Bulls
  • 24. Reading objects SELECT name, JSON_QUERY(attr, '$.teams') AS home_teams, JSON_QUERY(attr, '$.details') AS details FROM locations WHERE type = 'S'; name home_teams details United Center [ "Bulls", "Blackhawks" ] { "capacity": 23500, "yearOpened": 1994 }
  • 25. Indexing (1/2) ALTER TABLE locations ADD COLUMN food_type VARCHAR(25) AS (JSON_VALUE(attr, '$.details.foodType')) VIRTUAL; EXPLAIN SELECT name, latitude, longitude FROM locations WHERE food_type = 'Mexican'; id select_type table type possible_keys 1 SIMPLE Locations ALL NULL
  • 26. Indexing (2/2) CREATE INDEX foodtypes ON locations(food_type); EXPLAIN SELECT name, latitude, longitude FROM locations WHERE food_type = 'Mexican'; id select_type table ref possible_keys 1 SIMPLE Locations ref foodtypes
  • 28. Inserting fields UPDATE locations SET attr = JSON_INSERT(attr, '$.nickname', 'The Bean') WHERE id = 8; name nickname Cloud Gate The Bean SELECT name, JSON_VALUE(attr, '$.nickname') AS nickname FROM locations WHERE type = 'A';
  • 29. Inserting array UPDATE locations SET attr =JSON_INSERT(attr, '$.foodTypes', JSON_ARRAY('Asian', 'Mexican')) WHERE id = 1; name food_types Saucy Porka [ "Asian", "Mexican" ] SELECT name, JSON_QUERY(attr, '$.foodTypes') AS food_types FROM locations WHERE id = 1;
  • 30. Adding array elements UPDATE locations SET attr = JSON_ARRAY_APPEND(attr, '$.foodTypes', 'German’) WHERE id = 1; SELECT name, format, price, JSON_QUERY(attr, '$.foodTypes') AS food_types FROM locations WHERE id = 1; name food_types Saucy Porka [ "Asian", "Mexican", "German" ]
  • 31. Removing array elements UPDATE locations SET attr = JSON_REMOVE(attr, '$.foodTypes[2]') WHERE id = 1; SELECT name, JSON_QUERY(attr, '$.foodTypes') AS food_types FROM locations WHERE id = 1; name food_types Saucy Porka [ "Asian", "Mexican" ]
  • 32. Creating JSON documents from relational data
  • 33. Returning relational data as JSON SELECT JSON_OBJECT('name', name, 'latitude', latitude, 'longitude', longitude) AS data FROM locations WHERE type = 'S'; data { "name": "Wrigley Field", "latitude": 41.9484384, "longitude": -87.6553327 }
  • 34. Relational + JSON SELECT JSON_OBJECT('name', name, 'latitude', latitude, 'longitude', longitude, 'capacity', JSON_VALUE(attr, '$.details.capacity')) AS data FROM locations WHERE type = 'S'; data { "name": "Wrigley Field", "latitude": 41.9484384, "longitude": -87.6553327 “capacity": 41649 }
  • 35. Merging relational and JSON SELECT JSON_MERGE( JSON_OBJECT( 'name', name, 'latitude', latitude, 'longitude', longitude), attr) AS data FROM locations WHERE type = 'R'; data { "name": "The Publican", "latitude": , "longitude": , "details": { "foodType": "American", "menu": "http://www.thepublicanrestaurant.com/menu"}, "favorites": [ {"description": "Ribs with fries", "price": 16.99}, {"description": "BBQ Burger", "price": 12.99} ] }
  • 37. JSON Constraints ALTER TABLE locations ADD CONSTRAINT check_attr CHECK ( type != 'S' or (type = 'S' and JSON_TYPE(JSON_QUERY(attr, '$.details')) = 'OBJECT' and JSON_TYPE(JSON_QUERY(attr, '$.details.events')) = 'ARRAY' and JSON_TYPE(JSON_VALUE(attr, '$.details.yearOpened')) = 'INTEGER' and JSON_TYPE(JSON_VALUE(attr, '$.details.capacity')) = 'INTEGER' and JSON_EXISTS(attr, '$.details.yearOpened') = 1 and JSON_EXISTS(attr, '$.details.capacity') = 1 and JSON_LENGTH(JSON_QUERY(attr, '$.details.events')) > 0));
  • 38. JSON Constraints INSERT INTO locations (type, name, latitude, longitude, attr) VALUES ('S', 'Wrigley Field', 41.9484384, -87.6553327, '{"details": {"yearOpened": 1914, "capacity": 41649}'); ERROR 4025 (23000): CONSTRAINT `check_attr` failed for `test`.`locations` no teams field!
  • 39. JSON Constraints INSERT INTO locations (type, name, latitude, longitude, attr) VALUES ('S', 'Wrigley Field', 41.9484384, -87.6553327, '{"details": {"yearOpened": "Last Year", "capacity": 41649}, "teams": ["Cubs"]'); ERROR 4038 (HY000): Syntax error in JSON text in argument 1 to function 'json_type' at position 1 "Last Year" is not a number!
  • 43. Webinar attendees only 1. Find two JSON functions that were not mentioned in this presentation. 2. Email developers@mariadb.com and include: a. Function name b. Description of what the function is used for 3. Get swag! Developer Challenge!