Beruflich Dokumente
Kultur Dokumente
the
MySQL Document Store
Alfredo Kojima Sr. Dev. Manager
Mike Frank Product Management Director
Oracle
Copyright 2014 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Internal/Restricted/Highly Restricted
Safe Harbor Statement
The following is intended to outline our general product direcNon. It is intended for
informaNon purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or funcNonality, and should not be relied upon
in making purchasing decisions. The development, release, and Nming of any features or
funcNonality described for Oracles products remains at the sole discreNon of Oracle.
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 4
Plus
MySQL has been around since 1995
Ubiquitous
Pregy much a standard
Scalable
When there are issues, they are known and understood
Large body of knowledge, from small to BIG deployments
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 5
Document Databases
Schemaless
no schema design, normalizaNon, foreign keys, constraints, data types etc
faster iniNal development
Flexible data structures
nested arrays and objects
some data is simply naturally unstructured or cannot be modeled eciently in the
relaNonal model (hierarchies, product DB etc)
persist objects without ORMs
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 6
Document Databases (Cont.)
JSON
Closer to the frontend
"naNve" in JavaScript
Node.js and full stack JavaScript
Easy to learn, easy to use
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 7
RelaNonal vs Document Databases
Why not both?
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 8
1 RelaNonal Databases, Document Databases and MySQL
2 MySQL JSON Support
3 Document Use Cases
4 The X DevAPI
5 Ge\ng it all working together
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 11
MySQL 5.7: JSON Support (cont.)
Plus
Generated/Virtual Columns
Index JSON data
Foreign keys to JSON data
SQL views for JSON data
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 12
1 RelaNonal Databases, Document Databases and MySQL
2 MySQL JSON Support
3 Document Use Cases
4 The X DevAPI
5 Ge\ng it all working together
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 14
ExtracNng JSON from a RelaNonal DB
Rela+onal In, Rela+onal + Document Out
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 15
Using MySQL as a JSON Document Container
Document In, Rela+onal + Document Out
Virtually Schemaless
Unstructured data
No clear, xed structure for the data records can have dierent elds
Oven data that is not involved in business rules
Examples: "product_info", "properNes", "opNons" etc
Data does not map cleanly into a relaNonal model (arrays, hierarchical data
etc)
Prototyping
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 16
MySQL as a JSON Document Container
Example: "properNes" table
hgps://www.mediawiki.org/wiki/Manual:Database_layout
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 17
MySQL as a JSON Document Container
Example: "product_info" table
product_id a<ribute value {
"product_id": 9,
9 size M
"size" : "M",
9 color red "color": "red",
9 fabric cogon "fabric": "cogon"
},
11 avour strawberry {
12 capacity 128GB "product_id": 11,
12 speed class class 10 "avour": "strawberry"
},
13 connecNvity Wi-Fi {
13 storage 64GB "product_id": 12,
13 screen size 8.9" "capacity": "128GB",
"speed class": "class 10"
13 resoluNon 2560 x 1600 (339 ppi) },
13 bagery life 12 hours {
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 18
MySQL as a JSON Document Container
Document In, Rela+onal + Document Out
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 19
Hybrid RelaNonal and JSON
Rela+onal + Document In, Rela+onal + Document Out
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 20
Hybrid RelaNonal and JSON
Rela+onal + Document In, Rela+onal + Document Out
product (product_id, name, category_id, product (product_id, name, category_id,
); , attributes JSON);
product_info (product_id, attribute,
value);
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 21
1 RelaNonal Databases, Document Databases and MySQL
2 MySQL JSON Support
3 Document Use Cases
4 The X DevAPI
5 Ge\ng it all working together
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 23
Document OperaNons via SQL
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 24
The X DevAPI
AbstracNon over SQL
Focused on 4 basic CRUD operaNons (Create, Read, Update, Delete)
Fluent, NaNve Language API
No knowledge of SQL needed
X Protocol
CRUD requests encoded at protocol level
Request details "visible" (vs "opaque" SQL strings)
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 25
CollecNon and Schema OperaNons
Get a handle to a Schema
mydb = session.getSchema("mydb");
Create a CollecNon
mydb.createCollection("products");
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 26
Add Document
products.add({"name":"bananas", "color":"yellow"}).execute();
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 27
Find Documents
products.find("color = 'yellow'").sort(["name"]).execute();
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 28
Modify Documents
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 29
Remove Documents
products.remove("product_id = 123").execute();
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 30
X DevAPI Sessions
X Session
Stateless
CRUD only, no SQL
Abstracts the connecNon
Node Session
Direct connecNon to a database node
Allows CRUD and SQL
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 31
Other OperaNons on CollecNons
Create an Index
db.post.createIndex("email").eld("author.email", "text(30)", false)
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 32
CRUD OperaNons NoSQL/Document and SQL/RelaNonal
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 35
MySQL 5.7, Connectors, Drivers, and Protocols
MySQL Connectors and Drivers
SQL API CRUD and SQL APIs MySQL Memcached
Shell driver
Std Protocol X Protocol
MySQL
Plugins
Core
X Protocol Plugin Memcached Plugin
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 37
X DevAPI Connectors MySQL Connector/Net 7.0
using (XSession session = Result r = coll.Add(docs).Execute();
MySQLX.GetSession("mysqlx://test:test@localhost:33060")) Console.WriteLine("Docs added: " + r.RecordsAffected);
{
string schemaName = "test"; // modify some values
Schema testSchema = session.GetSchema(schemaName); r = coll.Modify("_id = :ID").
if (testSchema.ExistsInDatabase()) Bind("Id", 2).Set("pages", "25").Execute();
session.DropSchema(schemaName); Console.WriteLine("Docs modified: " + r.RecordsAffected);
session.CreateSchema(schemaName);
// remove a book
// insert some docs r = coll.Remove("_id = :ID").Bind("Id", 4).Execute();
Collection coll = testSchema.CreateCollection("myDocs"); Console.WriteLine("Docs removed: " + r.RecordsAffected);
var docs = new[]
{ // list the results
new { _id = 1, title = "Book 1", pages = 20 }, var result30orMore = coll.Find("pages > 20").
new { _id = 2, title = "Book 2", pages = 30 }, OrderBy("pages DESC").Execute().FetchAll();
new { _id = 3, title = "Book 3", pages = 40 }, foreach(var doc in result30orMore)
new { _id = 4, title = "Book 4", pages = 50 }, {
}; Console.WriteLine(doc.ToString());
}
}
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 38
X DevAPI Connectors MySQL Connector/Node.js 1.0 NEW!
const mysqlx = require('mysqlx');! !
! const collection =
session.getSchema('myschema').getCollection('questions');!
mysqlx.getSession({!
collection.find("answer == 42")!
host: 'localhost',!
dbUser: 'myuser',! .orderBy("foo DESC")!
.limit(10)!
dbPassword: 'secret'!
}).then(session => {! .execute(doc => console.log(doc)) // print the document
received, callback called for each doc!
const collection =
session.getSchema('myschema').getCollection('mycollection');! .then(() => console.log("All done") // Promise resolves
when all data is there!
return Promise.all([!
.catch((err) => console.log("Oups, an error", err);!
collection.add({ foo: "bar", something: { nested:
[1,2,3,4] } }).execute();! !
session.close();!
])!
}).catch(err => {!
console.log(err);!
});!
!
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 39
MySQL Shell
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 40
MySQL Plugin for VisualStudio
Copyright 2016 Oracle and/or its aliates. All rights reserved. | Oracle CondenNal Restricted 41
Resources
Topic Link(s)
MySQL as a Document Database hgp://dev.mysql.com/doc/refman/5.7/en/document-database.html
MySQL Shell hgp://dev.mysql.com/doc/refman/5.7/en/mysql-shell.html
hgp://dev.mysql.com/doc/refman/5.7/en/mysqlx-shell-tutorial-javascript.html
hgp://dev.mysql.com/doc/refman/5.7/en/mysqlx-shell-tutorial-python.html
X Dev API hgp://dev.mysql.com/doc/x-devapi-userguide/en/
X Plugin hgp://dev.mysql.com/doc/refman/5.7/en/x-plugin.html
MySQL JSON hgp://mysqlserverteam.com/tag/json/
hgps://dev.mysql.com/doc/refman/5.7/en/json.html
hgps://dev.mysql.com/doc/refman/5.7/en/json-funcNons.html
Blogs hgp://mysqlserverteam.com/category/docstore/
Alfredo Kojima
alfredo.kengi.kojima@oracle.com