MySQL as a Document Store
-
Upload
dave-stokes -
Category
Internet
-
view
135 -
download
1
Transcript of MySQL as a Document Store
MySQl aS A dOCUMENTSTORE
[email protected] @ Stokerslideshare.net/davidmstokeselelphantanddolphin.blogger.com
"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 Harbor
2
Hello!I am Dave Stokes, MySQL Community Manager
[email protected] @Stoker
Slides at Slideshare.net/davidmstokes
Blogs: Opensourcedba.wordpress.com elephantanddolphin.blogger.com 3
1.Json & json dATATYPE
MySQL 5.7
4
“JSON JavaScript Object Notation is a lightweight
data-interchange format. It is built on two structures: A collection of name/vaule pairs and and ordered list
of values.”
5
MySQL JSON data type
× Schemaless data× UTF8MB4× It is just like a INT, or REAL, or TIMESTAMP. You
can store an entire document in a column of a row of a table.
You could store JSON in a CHAR/TEXT field but it is not sexy and you end up using REGEXP --- Ughhh!
6
Big Very few developers are learning SQL (Structured Query Language), Relational Theory, Sets, data normalization or other database related subjects!
7
SQLData is normalized and formats are rigorously enforced. JOIN and logical operators allow complex queries. Costly to change schema.
The big contest of 2014 In case you missed it
NoSQLSchemaless, no need for relational overhead, changes as fast as the data, easy to implement.
8
MySQl offers both SQL & NoSQLRDMS
Good old relational database
InnoDB/Memcached
This plug-in allows direct access to InnoDB or NDB storage engine data using the memcached protocol, up to 9x faster
JSON
Data is stored in JSON format. No schema needed. Functions supplied for SQL based access & manipulation
9
But what if you wanted a database but do not know sql?
10
New Definitions
11
12
JSON Documents and Collections
A JSON document is a data structure composed of field/value pairs stored within a collection. The values of fields often contain other documents, arrays, and lists of documents.
CRUD Operations
Create, Read, Update and Delete (CRUD) operations are the four basic operations that can be performed on a database Collection or Table
13
X ProtocolThe 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.
14
Mysqlsh has three modes
\js -- JavaScript\py -- python
\SQL -- SQL (like old Mysql shell)
15
1. MySQL 5.17.12 or later2. Install X plugin
a. mysql> INSTALL PLUGINmysqlx SONAME “mysqlx.so”; OR
b. mysqlsh -u user -h localhost --classic --dba enableXProtocol
3. Install mysqlsh *16
Installation needs● Note: Installation of
MySQL Shell using the MySQL APT repository is only supported on Ubuntu 14.04 LTS (“Trusty Tahr”) and Ubuntu 15.10 (“Wily Werewolf”).
mysqlsh$ mysqlsh --uri dstokes:hidave@localhost world_x --nodemysqlx: [Warning] Using a password on the command line interface can be insecure.Creating a Node Session to dstokes@localhost:33060/world_xDefault schema `world_x` accessible through db.
Welcome to MySQL Shell 1.0.5 Development Preview
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type '\help', '\h' or '\?' for help, type '\quit' or '\q' to exit.
Currently in JavaScript mode. Use \sql to switch to SQL mode and execute queries.mysql-js>
17
MySQLSh shell SessionsMySQL Shell is a unified interface to operate MySQL Server through scripting languages such as JavaScript or Python. To maintain compatibility with previous versions, SQL can also be executed in certain modes. A connection to a MySQL server is required. In MySQL Shell these connections are handled by a Session object.
18
XSession: Use this session type for new application development. It offers the best integration with MySQL Server, and therefore, it is used by default. SQL execution is not supported Node Session: Use this session type for SQL execution on a MySQL Server with the X Protocol enabled. SQL execution is availableClassic Session Use this session type to interact with MySQL Servers that do not have the X Protocol enabled
● --node creates a Node Session.
● --classic creates a Classic Session.
● --x creates an XSession.
session
mysql-js> session<NodeSession:dstokes@localhost:33060/world_x>mysql-js> The above tells us we are a JavaScript mode Account is dstokes@localhost on port 33060 The Database/schema is world_x
19
session
mysql-js> session<NodeSession:dstokes@localhost:33060/world_x>mysql-js> The above tells us we are a JavaScript mode Account is dstokes@localhost on port 33060 The Database/schema is world_x
20
WHICH DATABASE AND WHICH COLLECTIONmysql-js> db<Schema:world_x>mysql-js> db.getCollections()[ <Collection:countryinfo>]mysql-js>
21
<-- The Database
← the table
Batch Mode Example
22
echo "SELECT * FROM world_x.city LIMIT 2;" | mysqlsh --json=pretty --sqlc --uri user@host{ "executionTime": "0.00 sec", "info": "", "rows": [ { "ID": 1, "Name": "Kabul", "CountryCode": "AFG", "District": "Kabol", "Info": "{\"Population\": 1780000}" }, { "ID": 2, "Name": "Qandahar", "CountryCode": "AFG", "District": "Qandahar", "Info": "{\"Population\": 237500}" } ], "warningCount": 0, "warnings": [], "hasData": true, "affectedRowCount": 0, "autoIncrementValue": 0}
This is a batch mode example where a query is sent to mysqlsh in sql mode and the output is produced in ‘pretty’ JSON format.
Interactive help
mysql-js> var mySchema = session.getSchema('Zendcon')The schema Zendcon does not exist, do you want to create it? [y/N]: y
mysql-js> mySchema<Schema:Zendcon>mysql-js>
Note the interactive error correction when a non existent schema is accessed.
23
db
24
$ mysqlsh --uri dstokes:hidave@localhost world_x Default schema `world_x` accessible through db....mysql-js> db<Schema:world_x>mysql-js> db.getCollections()[ <Collection:countryinfo>]mysql-js> db.countryinfo.find().limit(1)[ { "GNP": 828, "IndepYear": null, "Name": "Aruba" ….
db is a global variable assigned to the current active schema that you specified on the command line.
Basic Operators
25
db.name.add()The add() method inserts one document or a list of documents into the named collection.
db.name.find()The find() method returns some or all documents in the named collection.
db.name.modify()The modify() method updates documents in the named collection.
db.name.remove()The remove() method deletes one document or a list of documents from the named collection.
Create a Collection named ‘Zendcon’ and list all collections
26
mysql-js> db.createCollection("Zendcon")<Collection:Zendcon>mysql-js> db.getCollections()[ <Collection:Zendcon>, <Collection:countryinfo>]mysql-js>
mysql-js>db.countryinfo.add( { GNP: .6, IndepYear: 1967, Name: "Sealand", _id: "SEA", demographics: { LifeExpectancy: 79, Population: 27 }, geography: { Continent: "Europe", Region: "British Islands", SurfaceArea: 193 }, government: { GovernmentForm: "Monarchy", HeadOfState: "Michael Bates" } })
27
\use world_x
So lets use the world_x sample database and add a new record!
28
mysql-js> db.countryinfo.find("_id = 'USA'")[ { "GNP": 8510700, "IndepYear": 1776, "Name": "United States", "_id": "USA", "demographics": { "LifeExpectancy": 77.0999984741211, "Population": 278357000 }, "geography": { "Continent": "North America", "Region": "North America", "SurfaceArea": 9363520 }, "government": { "GovernmentForm": "Federal Republic", "HeadOfState": "Donald J Clinton" } }]1 document in set (0.00 sec)
Use find to search
All the old qualifiers are available for limit, sort, etc.
29
Examples
mysql-js> db.countryinfo.find("GNP > 500000")
mysql-js> db.countryinfo.find("GNP > 500000 and demographics.Population
< 100000000")
db.countryinfo.find("Name = :country").bind("country", "Italy")
What if you want only certain tags?mysql-js> db.countryinfo.find("GNP > 5000000").fields(["GNP", "Name"])[ { "GNP": 8510700, "Name": "United States" }]1 document in set (0.00 sec)mysql-js>
30
More Examples
db.countryinfo.find().limit(5)
db.countryinfo.find().sort(["IndepYear desc"]).limit(8)
db.countryinfo.find().sort(["IndepYear desc"]).limit(8).skip(1)
db.countryinfo.modify("_id = 'SEA'").
set("demographics", {LifeExpectancy: 78, Population: 28})
db.countryinfo.modify("Name = 'Sealand'").unset("GNP")
31
db.countryinfo.modify().
set("Airports", [])
This will add a new array field named Airports
In all records
32
db.countryinfo.modify("Name = 'France'").arrayAppend("$.Airports", "ORY")
db.countryinfo.find("Name = 'France'")[ { "Airports": [ "ORY" ], "GNP": 1424285, "IndepYear": 843, "Name": "France", "_id": "FRA",
33
Remove examples
db.countryinfo.remove("_id = 'SEA'")
db.countryinfo.remove().limit(1)
db.countryinfo.remove().sort(["Name desc"]).limit(1)
34
Indexes
db.countryinfo.createIndex("pop"). field("demographics.Population", "INTEGER", false).execute()
35
Indexes can be added -- this index is on population value under the demographics key, of type INTEGER, no NULLs allowed.There is also a dropIndex
You can also access
relational tables!!
36
37
Seeing tables
mysql-js> db.getTables()[ <Table:city>, <Table:country>, <Table:countrylanguage>]mysql-js>
Basic table operationsdb.name.insert() The insert() method inserts one or more records into the named table.
db.name.select() The select() method returns some or all records in the named table.
db.name.update() The update() method updates records in the named table.
db.name.delete() The delete() method deletes one or more records from the named table.
38
Insert Example
db.city.insert("ID", "Name", "CountryCode", "District", "Info").values(null, "Olympia", "USA", "Washington", '{"Population": 5000}')
39
SELECT Examplesmysql-js> db.city.select().limit(1) +----+-------+-------------+----------+-------------------------+| ID | Name | CountryCode | District | Info |+----+-------+-------------+----------+-------------------------+| 1 | Kabul | AFG | Kabol | {"Population": 1780000} |+----+-------+-------------+----------+-------------------------+1 row in set (0.00 sec)
mysql-js> db.city.select(["Name", "CountryCode"])
db.city.select(["Name", "CountryCode"]).where("Name like 'Z%'")
40
UPdate & delete
db.city.update().set("Name", "Beijing").where("Name = 'Peking'")
db.city.delete().where("Name = 'Olympia'")
41
Supported languagesJavaScript, Python and SQL *
● More on the way 42
THANKS!Any questions?
You can find me at @stoker or [email protected]
slideshare.net/davidmstokes
Elephantanddolphin.blogger.com https://legacy.joind.in/talk/view/1953843