Transcript
Page 1: MySQL's JSON Datatype

Scotland PHPTrack 2 16:15

29 October 2016

MySQL’s JSON

Data Type

Page 2: MySQL's JSON Datatype

"THE FOLLOWING IS INTENDED TO OUTLINE OUR GENERAL PRODUCT DIRECTION. IT IS INTENDED FOR INFORMATION PURPOSES ONLY, AND MAY NOT BE INCORPORATED INTO ANY CONTRACT. IT IS NOT A COMMITMENT TO DELIVER ANY MATERIAL, CODE, OR FUNCTIONALITY, AND SHOULD NOT BE RELIED UPON IN MAKING PURCHASING DECISIONS. THE DEVELOPMENT, RELEASE, AND TIMING OF ANY FEATURES OR FUNCTIONALITY DESCRIBED FOR ORACLE'S PRODUCTS REMAINS AT THE SOLE DISCRETION OF ORACLE."

Safe Harbour Agreement

Page 3: MySQL's JSON Datatype

● Dave Stokes− Started using PHP when it was called Personal

Home Page (and moved from Msql to MySQLabout the same time)

− Was hired at MySQL AB as a PHP Programmer in the MySQL Certification Group

− Now MySQL Community Manager for Oracle− Lives in Justin Texas

● Have pickup truck and hound dog as required by law

[email protected] @Stoker

Page 4: MySQL's JSON Datatype

● 21 Years Old Latest Release is MySQL 5.7, MySQL 8 Announced

Group Replication and Document Store

Oracle’s MySQL Cloud− Enterprise

Edition

● Doing very well at Oracle− Hiring− Making $

MySQL Recap

JSON Data Type

Page 5: MySQL's JSON Datatype

JSON Standard

https://tools.ietf.org/html/rfc7159

&http://www.ecma-international.org/publications/standards/Ecma-

404.htm

Page 6: MySQL's JSON Datatype

{ "id": 1, "name": "A green door", "price": 12.50, "tags": ["home", "green"]}

JSON Example

Page 7: MySQL's JSON Datatype

UTF8MB4The JSON standards specify that all JSON documents will be in the UTF8MB4 character set.

Page 8: MySQL's JSON Datatype

Not Jason

Page 9: MySQL's JSON Datatype

JSON is a data type like INT MySQL 5.7

Page 10: MySQL's JSON Datatype

Note:

MySQL handles strings used in JSON context using the utf8mb4 character set and utf8mb4_bin collation. Strings in other character sets are converted to utf8mb4 as necessary. (For strings in the ascii or utf8 character sets, no conversion is needed because ascii and utf8 are subsets of utf8mb4.)--https://dev.mysql.com/doc/refman/5.7/en/json.html

Page 11: MySQL's JSON Datatype

--https://dev.mysql.com/doc/refman/5.7/en/json.html

Optimized storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements. When the server later must read a JSON value stored in this binary format, the value need not be parsed from a text representation. The binary format is structured to enable the server to look up subobjects or nested values directly by key or array index without reading all values before or after them in the document.

Page 12: MySQL's JSON Datatype

You could store JSON data in a

CHAR/Varchar/text field but there are

no easy to use functions to help or

you end up using regex -- ughh!!!!

Page 13: MySQL's JSON Datatype

mysql>CREATE TABLE foobar (foo INT, bar JSON);

mysql>INSERT INTO foobar VALUES (1,'{ "name" : "dave", "home" : [ "Justin", "Texas", 76247 ]}');

mysql> SELECT * FROM foobar;+------+------------------------------------------------------+| foo | bar |+------+------------------------------------------------------+| 1 | {"home": ["Justin", "Texas", 76247], "name": "dave"} |+------+------------------------------------------------------+1 row in set (0.00 sec)

Page 14: MySQL's JSON Datatype

JSON Functions to ...

× Create JSON values

× Search JSON values

× Modify JSON value

× Return JSON value attributes

Page 15: MySQL's JSON Datatype

Name DescriptionJSON_APPEND() Append data to JSON documentJSON_ARRAY() Create JSON arrayJSON_ARRAY_APPEND() Append data to JSON documentJSON_ARRAY_INSERT() Insert into JSON array-> Return value from JSON column after evaluating path;

quivalent to JSON_EXTRACT().JSON_CONTAINS() Whether JSON document contains specific object at pathJSON_CONTAINS_PATH() Whether JSON document contains any data at pathJSON_DEPTH() Maximum depth of JSON documentJSON_EXTRACT() Return data from JSON document->> Return value from JSON column after evaluating path

and unquoting the result,JSON_UNQUOTE(JSON_EXTRACT()).JSON_INSERT() Insert data into JSON documentJSON_KEYS() Array of keys from JSON documentJSON_LENGTH() Number of elements in JSON documentJSON_MERGE() Merge JSON documentsJSON_OBJECT() Create JSON objectJSON_QUOTE() Quote JSON documentJSON_REMOVE() Remove data from JSON documentJSON_REPLACE() Replace values in JSON documentJSON_SEARCH() Path to value within JSON documentJSON_SET() Insert data into JSON documentJSON_TYPE() Type of JSON valueJSON_UNQUOTE() Unquote JSON valueJSON_VALID() Whether JSON value is valid

Page 16: MySQL's JSON Datatype

JSON_EXTRACT

JSON_EXTRACT(json_doc, path[, path …])

mysql> SELECT json_extract(bar,'$.Breed') FROM foo;+-----------------------------+| json_extract(bar,'$.Breed') |+-----------------------------+| NULL || ["Beagle", "Small"] |+-----------------------------+2 rows in set (0.00 sec)

Page 17: MySQL's JSON Datatype

JSON_EXTRACT shorthand ->

column->path

mysql> SELECT bar->'$.Breed' FROM foo;+---------------------+| bar->'$.Breed' |+---------------------+| NULL || ["Beagle", "Small"] |+---------------------+2 rows in set (0.00 sec)

Page 18: MySQL's JSON Datatype

JSON_contains

mysql> select * from foo;+------+------------------------------------------------+| id | bar |+------+------------------------------------------------+| 1 | {"name": "Dave"} || 2 | {"name": "Jack", "Breed": ["Beagle", "Small"]} |+------+------------------------------------------------+2 rows in set (0.00 sec)

mysql> SELECT json_contains(bar,'{\"name\": \"Dave\"}') FROM foo;+-------------------------------------------+| json_contains(bar,'{\"name\": \"Dave\"}') |+-------------------------------------------+| 1 || 0 |+-------------------------------------------+

Page 19: MySQL's JSON Datatype

JSON_contains_pathmysql> select * from foo;+------+------------------------------------------------+| id | bar |+------+------------------------------------------------+| 1 | {"name": "Dave"} || 2 | {"name": "Jack", "Breed": ["Beagle", "Small"]} |+------+------------------------------------------------+2 rows in set (0.00 sec)mysql> select json_contains_path(bar,'one','$.Breed') from foo;

+-----------------------------------------+ [ONE\ALL]| json_contains_path(bar,'one','$.Breed') |+-----------------------------------------+| 0 || 1 |+-----------------------------------------+2 rows in set (0.00 sec)

19

Page 20: MySQL's JSON Datatype

JSON_INSERTmysql> UPDATE foo set bar = JSON_INSERT(bar, '$[99]', 'x');Query OK, 2 rows affected (0.01 sec)Rows matched: 2 Changed: 2 Warnings: 0

mysql> select * from foo;+------+-------------------------------------------------------+| id | bar |+------+-------------------------------------------------------+| 1 | [{"name": "Dave"}, "x"] || 2 | [{"name": "Jack", "Breed": ["Beagle", "Small"]}, "x"] |+------+-------------------------------------------------------+2 rows in set (0.00 sec)

20

Insert position, append to end if not exist

Page 21: MySQL's JSON Datatype

JSON_REPLACE

UPDATE foo set bar =

JSON_REPLACE(bar, '$[0]',JSON_ARRAY(1,2,3));Query OK, 2 rows affected (0.00 sec)Rows matched: 2 Changed: 2 Warnings: 0

mysql> select * from foo;+------+------------------+| id | bar |+------+------------------+| 1 | [[1, 2, 3], "x"] || 2 | [[1, 2, 3], "x"] |+------+------------------+

Page 22: MySQL's JSON Datatype

...])JSON_depth

mysql> select * from foo;+------+-------------------------------------------------------+| id | bar |+------+-------------------------------------------------------+| 1 | [{"name": "Dave"}, "x"] || 2 | [{"name": "Jack", "Breed": ["Beagle", "Small"]}, "x"] |+------+-------------------------------------------------------+2 rows in set (0.00 sec)

mysql> select json_depth(bar) from foo;+-----------------+| json_depth(bar) |+-----------------+| 3 || 4 |+-----------------+

Page 23: MySQL's JSON Datatype

...])JSON_KEYS

select json_keys('{"name" : "dave", "food" : "pizza" }');+---------------------------------------------------+| json_keys('{"name" : "dave", "food" : "pizza" }') |+---------------------------------------------------+| ["food", "name"] |+---------------------------------------------------+1 row in set (0.00 sec)

Page 24: MySQL's JSON Datatype

No Indexes

JSON columns, like columns of other binary types, are not indexed directly; instead, you can create an index on a generated column that extracts a scalar value from the JSON column.--http://dev.mysql.com/doc/refman/5.7/en/json.html

Page 25: MySQL's JSON Datatype

mysql> CREATE TABLE snafu (stuff JSON, idx INT GENERATED ALWAYS AS ('stuff->$.id'));

Query OK, 0 rows affected (0.04 sec)

Generated JSON data index

Page 26: MySQL's JSON Datatype

IS THIS JSON STUFF GOOD IDEA?

Schemaless data is handy, easy to implement, and needs no data architecting.

But their is no enforced rigor to the data, is can be messy, inconsistent (E-mail, email, e_mail, eMail), and it is hard to get insights into the nature of the data.

Page 27: MySQL's JSON Datatype

New JSON FunctionsThis release adds an unquoting extraction operator ->>, sometimes also referred to as an inline path operator, for use with JSON documents stored in MySQL. The new operator is similar to the -> operator, but performs JSON unquoting of the value as well. For a JSON column mycol and JSON path expression mypath, the following three expressions are equivalent:

JSON_UNQUOTE( JSON_EXTRACT(mycol, "$.mypath") )

JSON_UNQUOTE(mycol->"$.mypath")

mycol->>"$.mypath"

The ->> operator can be used in SQL statements wherever JSON_UNQUOTE(JSON_EXTRACT()) would be allowed. This includes (but is not limited to) SELECT lists, WHERE and HAVING clauses, and ORDER BY and GROUP BY clauses.

Mysql 8 - developer milestone release

Page 28: MySQL's JSON Datatype

New JSON FunctionsStarting with MySQL 8.0 (lab release) two new aggregation functions were added and can be used to combine data into JSON arrays/objects:

JSON_ARRAYAGG()JSON_OBJECTAGG()

Mysql 8 - developer milestone release

Page 29: MySQL's JSON Datatype

preproduction releaseThe MySQL Document Store is a schema-less and

therefore schema-flexible, storage system for documents.

When using MySQL as a document store, to create documents

describing products you do not need to know and define all

possible attributes of any products before storing them and

operating with them. This differs from working with a

relational database and storing products in a table, when all

columns of the table must be known and defined before

adding any products to the database.

Page 30: MySQL's JSON Datatype

CRUD Operations -- Create, Read, Update and Delete (CRUD) operations are the four basic operations that can be performed on a database Collection or Table. In terms of MySQL this means:

X Plugin The MySQL Server plugin which enables communication using X Protocol. Supports clients that implement X DevAPI and enables you to use MySQL as a document store.

X Protocol A protocol to communicate with a MySQL Server running X Plugin. X Protocol supports both CRUD and SQL operations, authentication via SASL, allows streaming (pipelining) of commands and is extensible on the protocol and the message layer

See chapter 3 of the MySQL 5.7 Documentation

Page 31: MySQL's JSON Datatype

Q/A

[email protected]@Stoker

opensourcedba.wordpress.comelephantanddolphin.blogger.com

slideshare.net/davidmstokes


Top Related