O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

How to Handle NoSQL with a Relational Database

448 visualizações

Publicada em

It can be difficult to scale out relational databases and provide more schema flexibility, thus the rise of NoSQL. However, you shouldn’t have to sacrifice data integrity and transactions in order to scale out on commodity hardware and support semi-structured data. By using an RDBMS with built-in sharding and JSON support, you don’t have to. You get the scalability and flexibility of a NoSQL database along with the consistency and reliability of a relational database – and the ability to mix and match relational and JSON data.

In this webinar, we’ll explain how MariaDB Platform can be deployed as a NoSQL database by using the Spider storage engine and built-in SQL functions for JSON. In addition, we’ll discuss how you can access relational data as JSON documents, and how to enforce data integrity if a relational data model is extended with JSON.

Publicada em: Dados e análise
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

How to Handle NoSQL with a Relational Database

  1. 1. Handling NoSQL with a Relational Database Thomas Boyd Director, Technical Marketing MariaDB Corporation
  2. 2. Agenda ● Relational Databases & SQL ● Document Databases & NoSQL ● MariaDB: SQL and NoSQL together ○ Scalability ○ JSON Support ○ Node.js Integration ● Q & A
  3. 3. Relational Databases & SQL
  4. 4. Proven Technology ● 1970: “Relational Completeness of Data Base Sublanguages” ● ANSI managed Structured Query Language (SQL) ● Large ecosystem of RDBMS Vendors and Tools ● Large Talent Pool ○ Developers & Data Modelers ○ DBAs/Operators ○ Architects
  5. 5. Relational: Modeling Orders Id Number Rep_Id 1 1001 4 2 1002 8 3 1003 5 4 1004 7 Order_Lines Order_ID Line_ID Line_Number Prodcut_ID Qty Price 1 1 1 101 1 5.55 1 2 2 123 10 12 1 3 3 103 1 7.25 2 4 1 101 2 11.05 2 5 2 114 3 3.45
  6. 6. NoSQL & Document Databases
  7. 7. NoSQL & Document Databases ● NoSQL ○ Key-Value, Wide Column, Document, and more ● Drivers of Popularity of Document Databases ○ Scalability ○ JSON Support & Schema-less ○ Drivers and Modern Application Stacks (Node.js)
  8. 8. Document: Modeling { order_ID: 1, number: 101 sales_rep: {first_name: “John”, last_name: “Smith”, …}, order_Lines: [ {line_id: 1, number: 101, …}, {line_id: 2, number: 102, …}, {line_id: 3, number: 103, …}] }
  9. 9. NoSQL: Caveats ● Limited Transaction support ● Proprietary and inefficient SQL access ● Fewer integrations and smaller ecosystem ● Smaller talent pool ● Down-sides to documents and schema-less ○ Data hygiene ○ Storage size and query efficiency ○ Expensive updates of denormalized data
  10. 10. MariaDB: Combining SQL & NoSQL
  11. 11. Default Database on Leading Linux Distros, Available on Leading Cloud Platforms Cloud Services & StacksLinux Distributions
  12. 12. 12 Million Users in 45 Countries Trust Critical Business Data to MariaDB Technology & InternetTelecom Retail & EcommerceTravel Financial Services Gvmt & Education Media & Social
  13. 13. MariaDB: Scalability
  14. 14. Scalability ● MariaDB has multiple options for Scalability ○ Read Scalability with Replicas ○ Write Scalability with Spider (Partitioning/Sharding) ○ Scaling for Big Data Analytics with MariaDB ColumnStore ○ Other techniques via MaxScale router ○ Vertical scaling
  15. 15. Scalability: Read Scalability via Replicas MariaDB Platform MariaDB Server (slave) MariaDB Server (master) MariaDB Server (slave) MariaDB MaxScale
  16. 16. Scalability: Write Scalability via Spider ● Range ● Hash ● Key ● List MariaDB Platform MariaDB ServerMariaDB Server MariaDB Server MariaDB Server MariaDB MaxScale Spider Storage Engine
  17. 17. Scalability: Big Data via Platform & ColumnStore MariaDB Platform MariaDB MaxScale CDC MariaDB Server InnoDB MariaDB Server ColumnStore Transactional Analytical
  18. 18. MariaDB: JSON Support
  19. 19. Structured and semi-structured data with relational + JSON Structured Semi-structured
  20. 20. Structured and semi-structured data with relational + JSON Structure data described by the schema Structured described by the data
  21. 21. JSON Example: definition CREATE TABLE IF NOT EXISTS products ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, type VARCHAR(1) NOT NULL, name VARCHAR(40) NOT NULL, format VARCHAR(20) NOT NULL, price FLOAT(5, 2) NOT NULL, attr JSON NOT NULL);
  22. 22. Example: create INSERT INTO products (type, name, format, price, attr) VALUES ('M', 'Aliens', 'Blu-ray', 13.99,'{"video": {"resolution": "1080p", "aspectRatio": "1.85:1"}, "cuts": [{"name": "Theatrical", "runtime": 138}, {"name": "Special Edition", "runtime": 155}], "audio": ["DTS HD", "Dolby Surround"]}'); INSERT INTO products (type, name, format, price, attr) VALUES ('B', 'Foundation', 'Paperback', 7.99, '{"author": "Isaac Asimov", "page_count": 296}');
  23. 23. Example: read field SELECT name, format, price, JSON_VALUE(attr, '$.video.aspectRatio') AS aspect_ratio FROM products WHERE type = 'M'; name format price aspect_ratio Aliens Blu-ray 13.99 1.85:1
  24. 24. Example: null field SELECT type, name, format, price, JSON_VALUE(attr,$.video.aspectRatio') AS aspect_ratio FROM products; type name format price aspect_ratio M Aliens Blu-ray 13.99 1.85:1 B Foundation Paperback 7.99 NULL
  25. 25. Example: contains value SELECT id, name, format, price FROM products WHERE type = 'M' AND JSON_CONTAINS(attr, '"DTS HD"', '$.audio') = 1; name format price aspect_ratio Aliens Blu-ray 13.99 1.85:1
  26. 26. Example: read array SELECT name, format, price, JSON_QUERY(attr, '$.audio') AS audio FROM products WHERE type = 'M'; name format price audio Aliens Blu-ray 13.99 [ "DTS HD", "Dolby Surround" ]
  27. 27. Example: read array element SELECT name, format, price, JSON_VALUE(attr, '$.audio[0]') AS default_audio FROM products WHERE type = 'M'; name format price audio Aliens Blu-ray 13.99 DTS HD
  28. 28. Example: read object SELECT name, format, price, JSON_QUERY(attr, '$.video') AS video FROM products WHERE type = 'M'; name format price video Aliens Blu-ray 13.99 { "resolution": "1080p", "aspectRatio": "1.85:1" }
  29. 29. Example: field equals SELECT id, name, format, price FROM products WHERE type = 'M' AND JSON_VALUE(attr, '$.video.resolution') = '1080p'; name format price aspect_ratio Aliens Blu-ray 13.99 1.85:1
  30. 30. Example: indexing (1/2) ALTER TABLE products ADD COLUMN video_resolution VARCHAR(5) AS (JSON_VALUE(attr, '$.video.resolution')) VIRTUAL; EXPLAIN SELECT name, format, price FROM products WHERE video_resolution = '1080p'; id select_type table type possible_keys 1 SIMPLE products ALL NULL
  31. 31. Example: indexing (2/2) CREATE INDEX resolutions ON products(video_resolution); EXPLAIN SELECT name, format, price FROM products WHERE video_resolution = '1080p'; id select_type table ref possible_keys 1 SIMPLE products ref resolutions
  32. 32. Example: insert field UPDATE products SET attr = JSON_INSERT(attr, '$.disks', 1) WHERE id = 1; name format price disks Aliens Blu-ray 13.99 1 SELECT name, format, price, JSON_VALUE(attr, '$.disks') AS disks FROM products WHERE type = 'M';
  33. 33. Example: insert array UPDATE products SET attr =JSON_INSERT(attr, '$.languages', JSON_ARRAY('English', 'French')) WHERE id = 1; name format price languages Aliens Blu-ray 13.99 [ "English", "French" ] SELECT name, format, price, JSON_QUERY(attr, '$.languages') AS languages FROM products WHERE type = 'M';
  34. 34. Example: add array element UPDATE products SET attr = JSON_ARRAY_APPEND(attr, '$.languages', 'Spanish') WHERE id = 1; name format price languages Aliens Blu-ray 13.99 [ "English", "French", "Spanish" ] SELECT name, format, price, JSON_QUERY(attr, '$.languages') AS languages FROM products WHERE type = 'M';
  35. 35. Example: remove array element UPDATE products SET attr = JSON_REMOVE(attr, '$.languages[0]') WHERE id = 1; name format price languages Aliens Blu-ray 13.99 [ "French", "Spanish" ] SELECT name, format, price, JSON_QUERY(attr, '$.languages') AS languages FROM products WHERE type = 'M';
  36. 36. Example: relational to JSON SELECT JSON_OBJECT('name', name, 'format', format, 'price', price) AS data FROM products WHERE type = 'M'; data { "name": "Tron", "format": "Blu-ray", "price": 29.99 }
  37. 37. Example: relational + JSON SELECT JSON_OBJECT('name', name, 'format', format, 'price', price, 'resolution', JSON_VALUE(attr, '$.video.resolution')) AS data FROM products WHERE type = 'M'; data { "name": "Aliens", "format": "Blu-ray", "price": 13.99, "resolution": "1080p" }
  38. 38. Example: relational + JSON SELECT JSON_MERGE( JSON_OBJECT( 'name', name, 'format', format), attr) AS data FROM products WHERE type = 'M'; data { "name": "Tron", "format": "Blu-ray", "video": { "resolution": "1080p", "aspectRatio": "1.85:1"}, "cuts": [ {"name": "Theatrical", "runtime": 138}, {"name": "Special Edition", "runtime": 155}], "audio": [ "DTS HD", "Dolby Surround"]}
  39. 39. Example: constraints (1/3) ALTER TABLE products ADD CONSTRAINT check_attr CHECK ( type != 'M' or (type = 'M' and JSON_TYPE(JSON_QUERY(attr, '$.video')) = 'OBJECT' and JSON_TYPE(JSON_QUERY(attr, '$.cuts')) = 'ARRAY' and JSON_TYPE(JSON_QUERY(attr, '$.audio')) = 'ARRAY' and JSON_TYPE(JSON_VALUE(attr, '$.disks')) = 'INTEGER' and JSON_EXISTS(attr, '$.video.resolution') = 1 and JSON_EXISTS(attr, '$.video.aspectRatio') = 1 and JSON_LENGTH(JSON_QUERY(attr, '$.cuts')) > 0 and JSON_LENGTH(JSON_QUERY(attr, '$.audio')) > 0));
  40. 40. Example: constraints (2/3) INSERT INTO products (type, name, format, price, attr) VALUES ('M', 'Tron', 'Blu-ray', 29.99, '{"video": {"aspectRatio": "2.21:1"}, "cuts": [{"name": "Theatrical", "runtime": 96}], "audio": ["DTS HD", "Dolby Digital"], "disks": 1}'); ERROR 4025 (23000): CONSTRAINT `check_attr` failed for `test`.`products` no resolution field!
  41. 41. Example: constraints (3/3) INSERT INTO products (type, name, format, price, attr) VALUES ('M', 'Tron', 'Blu-ray', 29.99, '{"video": {"resolution": "1080p", "aspectRatio": "2.21:1"}, "cuts": [{"name": "Theatrical", "runtime": 96}], "audio": ["DTS HD", "Dolby Digital"], "disks": "one"}'); ERROR 4038 (HY000): Syntax error in JSON text in argument 1 to function 'json_type' at position 1 "one" is not a number!
  42. 42. MariaDB: Node.js
  43. 43. • Ryan Dahl, JSConf 2009 • Event Loop, Callbacks & Non-blocking IO • Different mindset from synchronous programming
  44. 44. GA MariaDB Access since 2014 2011-2013: MySQL Development Jan 2014: MySQL GA Release Sep 2016: MySQL2 GA Release Dec 2018: MariaDB RC Jan 2019: MariaDB GA Release
  45. 45. Node.js Driver ● MariaDB Released Driver available Jan 2019 ● Increased performance ○ Pipelining of statements ○ Bulk operations ● ORM Support: Sequelize ● Frameworks: Feathers.js
  46. 46. Sequelize Model Definition class Task extends Model {} Task.init({ title: Sequelize.STRING, description: Sequelize.TEXT, deadline: Sequelize.DATE }, { sequelize, modelName: 'task' }) http://docs.sequelizejs.com/
  47. 47. Installation js-demo$ npm install mariadb + mariadb@2.0.2-rc updated 1 package and audited 180 packages in 1.321s found 0 vulnerabilities js-demo$ • NPM Package Manager • Download at https://mariadb.com/downloads/#connectors js-demo$ tar -xzf mariadb-connector-nodejs-2.0.2-rc.tar.gz js-demo$ cd mariadb-connector-nodejs-2.0.2-rc js-demo$ npm link js-demo$ cd <project directory> js-demo$ npm link mariadb
  48. 48. Resources https://mariadb.com https://mariadb.com/resources/ https://mariadb.com/contact
  49. 49. Q & A
  50. 50. Thank You

×