MySQL as a Document Store

43
MySQl aS A dOCUMENTSTORE [email protected] @ Stoker slideshare.net/davidmstokes elelphantanddolphin.blogger.com

Transcript of MySQL as a Document Store

Page 1: MySQL as a Document Store

MySQl aS A dOCUMENTSTORE

[email protected] @ Stokerslideshare.net/davidmstokeselelphantanddolphin.blogger.com

Page 2: MySQL as a Document Store

"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

Page 3: MySQL as a Document Store

Hello!I am Dave Stokes, MySQL Community Manager

[email protected] @Stoker

Slides at Slideshare.net/davidmstokes

Blogs: Opensourcedba.wordpress.com elephantanddolphin.blogger.com 3

Page 4: MySQL as a Document Store

1.Json & json dATATYPE

MySQL 5.7

4

Page 5: MySQL as a Document Store

“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

Page 6: MySQL as a Document Store

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

Page 7: MySQL as a Document Store

Big Very few developers are learning SQL (Structured Query Language), Relational Theory, Sets, data normalization or other database related subjects!

7

Page 8: MySQL as a Document Store

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

Page 9: MySQL as a Document Store

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

Page 10: MySQL as a Document Store

But what if you wanted a database but do not know sql?

10

Page 11: MySQL as a Document Store

New Definitions

11

Page 12: MySQL as a Document Store

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.

Page 13: MySQL as a Document Store

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

Page 14: MySQL as a Document Store

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

Page 15: MySQL as a Document Store

Mysqlsh has three modes

\js -- JavaScript\py -- python

\SQL -- SQL (like old Mysql shell)

15

Page 16: MySQL as a Document Store

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”).

Page 17: MySQL as a Document Store

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

Page 18: MySQL as a Document Store

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.

Page 19: MySQL as a Document Store

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

Page 20: MySQL as a Document Store

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

Page 21: MySQL as a Document Store

WHICH DATABASE AND WHICH COLLECTIONmysql-js> db<Schema:world_x>mysql-js> db.getCollections()[ <Collection:countryinfo>]mysql-js>

21

<-- The Database

← the table

Page 22: MySQL as a Document Store

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.

Page 23: MySQL as a Document Store

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

Page 24: MySQL as a Document Store

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.

Page 25: MySQL as a Document Store

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.

Page 26: MySQL as a Document Store

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>

Page 27: MySQL as a Document Store

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!

Page 28: MySQL as a Document Store

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.

Page 29: MySQL as a Document Store

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")

Page 30: MySQL as a Document Store

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

Page 31: MySQL as a Document Store

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

Page 32: MySQL as a Document Store

db.countryinfo.modify().

set("Airports", [])

This will add a new array field named Airports

In all records

32

Page 33: MySQL as a Document Store

db.countryinfo.modify("Name = 'France'").arrayAppend("$.Airports", "ORY")

db.countryinfo.find("Name = 'France'")[ { "Airports": [ "ORY" ], "GNP": 1424285, "IndepYear": 843, "Name": "France", "_id": "FRA",

33

Page 34: MySQL as a Document Store

Remove examples

db.countryinfo.remove("_id = 'SEA'")

db.countryinfo.remove().limit(1)

db.countryinfo.remove().sort(["Name desc"]).limit(1)

34

Page 35: MySQL as a Document Store

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

Page 36: MySQL as a Document Store

You can also access

relational tables!!

36

Page 37: MySQL as a Document Store

37

Seeing tables

mysql-js> db.getTables()[ <Table:city>, <Table:country>, <Table:countrylanguage>]mysql-js>

Page 38: MySQL as a Document Store

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

Page 39: MySQL as a Document Store

Insert Example

db.city.insert("ID", "Name", "CountryCode", "District", "Info").values(null, "Olympia", "USA", "Washington", '{"Population": 5000}')

39

Page 40: MySQL as a Document Store

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

Page 41: MySQL as a Document Store

UPdate & delete

db.city.update().set("Name", "Beijing").where("Name = 'Peking'")

db.city.delete().where("Name = 'Olympia'")

41

Page 42: MySQL as a Document Store

Supported languagesJavaScript, Python and SQL *

● More on the way 42

Page 43: MySQL as a Document Store

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