mysql's nosql -- nyc code camp july 18th 2015

40
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1

Upload: dave-stokes

Post on 14-Aug-2015

266 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.1

Page 2: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2

Insert Picture Here

NoSQL, HTTP, and JSON with MySQLDave StokesMySQL Community Manager

[email protected]@stokerSlideshare.net/davestokes

Insert Picture Here

Page 3: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3

Safe Harbor

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 decision. The development, release, and timing

of any features or functionality described for Oracle’s products

remains at the sole discretion of Oracle.

Page 4: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.4

MySQL

Most popular database on the web Ubiquitous 16+ million instances Feeds 80% of Hadoop installs 20 Years Old

Page 5: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.5

Relational Data

● Based on relational calculus, set theory

● Been heavily used for decades

● Many vendors

● Goal: Store data efficiently

Page 6: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.6

Relational Data● ACID (from Wikipedia https://en.wikipedia.org/wiki/ACID)

● Atomicity – equires that each transaction be "all or nothing": if one part of the transaction fails, the entire transaction fails, and the database state is left unchanged

● Consistency – ensures that any transaction will bring the database from one valid state to another.

● Isolation – the concurrent execution of transactions results in a system state that would be obtained if transactions were executed serially

● Durability – means that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors.

Page 7: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.7

So why NoSQL?!?● A NoSQL (often interpreted as Not only SQL) database provides

a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases. (https://en.wikipedia.org/wiki/NoSQL)

● Motivations for this approach include simplicity of design, presumed better "horizontal" scaling to clusters of machine, which is a problem for relational databases, and presumed finer control over availability.

Page 8: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.8

CAP Theorem

CAP theorem states that it is impossible for a distributed computer system to simultaneously provide all three of the following guarantees:

● Consistency (all nodes see the same data at the same time)

● Availability (a guarantee that every request receives a response about whether it succeeded or failed)

● Partition tolerance (the system continues to operate despite arbitrary partitioning due to network failures)

(https://en.wikipedia.org/wiki/CAP_theorem)

Page 9: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.9

Want BOTH! What to do?!?!?!

● Polyglot Databases – Use best storage approach for the data

● Oracle, Postgresql, MySQL, etc. all adopting some NoSQL features

● NoSQL trying to adopt SQL

● Vendors not dumb!

● Better for consumers

Page 10: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.10

Access SQLand NoSQL

One set of disks

Simultaneous access

MySQL InnoDB tables and NDB tables

Use SQL and/or Key/Value pair

2,000,000,000 writes a minute with MySQL Cluster

At the same time!

Page 11: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.11

Page 12: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.12

Page 13: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.13

Benefits

Raw performance for simple lookups. Direct access to the InnoDB storage engine avoids the parsing and planning overhead of SQL. Running memcached in the same process space as the MySQL server avoids the network overhead of passing requests back and forth.

Data is stored in a MySQL database to protect against crashes, outages, and corruption.

The transfer between memory and disk is handled automatically, simplifying application logic.

Data can be unstructured or structured, depending on the type of application. You can make an all-new table for the data, or map the NoSQL-style processing to one or more existing tables.

Page 14: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.14

Benefits continued

You can still access the underlying table through SQL, for reporting, analysis, ad hoc queries, bulk loading, set operations such as union and intersection, and other operations well suited to the expressiveness and flexibility of SQL.

You can ensure high availability of the NoSQL data by using this feature on a master server in combination with MySQL replication.

The integration of memcached with MySQL provides a painless way to make the in-memory data persistent, so you can use it for more significant kinds of data. You can put more add, incr, and similar write operations into your application, without worrying that the data could disappear at any moment. You can stop and start the memcached server without losing updates made to the cached data. To guard against unexpected outages, you can take advantage of InnoDB crash recovery, replication, and backup procedures.

The way InnoDB does fast primary key lookups is a natural fit for memcached single-item queries. The direct, low-level database access path used by the memcached plugin is much more efficient for key-value lookups than equivalent SQL queries.

Page 15: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.15

More Benefits Continued

The serialization features of memcached, which can turn complex data structures, binary files, or even code blocks into storeable strings, offer a simple way to get such objects into a database.

Because you can access the underlying data through SQL, you can produce reports, search or update across multiple keys, and call functions such as AVG() and MAX() on the memcached data. All of these operations are expensive or complicated with the standalone memcached.

You do not need to manually load data into memcached at startup. As particular keys are requested by an application, the values are retrieved from the database automatically, and cached in memory using the InnoDB buffer pool.

Because memcached consumes relatively little CPU, and its memory footprint is easy to control, it can run comfortably alongside a MySQL instance on the same system.

Because data consistency is enforced through the usual mechanism as with regular InnoDB tables, you do not have to worry about stale memcached data or fallback logic to query the database in the case of a missing key

Page 16: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.16

Installation

mysql> install plugin daemon_memcached soname "libmemcached.so";

Page 17: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.17

Here is an example using telnet to send memcached commands and receive results through the ASCII protocol:

● telnet 127.0.0.1 11211● set a11 10 0 9● 123456789● STORED● get a11● VALUE a11 0 9● 123456789● END● quit

Set memory location 'a11' to hold 9 characters '123456789' – 10 & 0 are TTL and flags

Page 18: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.18

Can it be used with MySQL Replication?

Because the InnoDB memcached daemon plugin supports the MySQL binary log, any updates made on a master server through the memcached interface can be replicated for backup, balancing intensive read workloads, and high availability. All memcached commands are supported for binlogging.

You do not need to set up the InnoDB memcached plugin on the slave servers. In this configuration, the primary advantage is increased write throughput on the master. The speed of the replication mechanism is not affected

Page 19: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.19

With MySQL Cluster

Page 20: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.20

Bypass MySQL daemon!

● 4.2 billions reads/min

● 1.2 billion updates/min

● 2 billion writes a min

● MySQL Cluster

Page 21: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.21

Can MySQL Replication work with Hadoop?

Page 22: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.22

Page 23: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.23

Page 24: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.24

Speak HTTP, Return JSON

HTTP clients and JavaScript users connect to MySQL using HTTP. The development preview brings three APIs:

● Key-document for nested JSON documents

● CRUD for JSON mapped SQL tables

● Plain SQL with JSON replies.

Page 25: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.25

Without and With the Http Plugin

Without Plugin

● Http client

● Web service (PHP Script)

● Web Server

● MySQL Database

With Plugin

● Http client

● Http plugin

● MySQL Database

Page 26: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.26

Opps! We goofed!

The HTTP Plugin just makes MySQL a bit more web developer friendly.

● MySQL was born in a world without the web.

● MySQL grew up with backend developers.

● MySQL somewhat missed the growing role of frontend development, the developers and their ideas.

● So we made this plugin for the Front End Developers!!!

Page 27: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.27

When to use

The server plugin is most useful in environments where protocols other than HTTP are blocked:

● JavaScript code run in a browser

● For application server behind a firewall and restricted to HTTP access

● In a web services oriented environment

Page 28: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.28

Example SELECT 1shell> curl ... --url "http://127.0.0.1:8080/sql/db/SELECT+1"[{"meta":[ {"type":8,"catalog":"def", "database":"","table":"", "org_table":"","column":"1","org_column":"", "charset":63,"length":1, "flags":129,"decimals":0}],"data":[ ["1"]],"status":[{"server_status":2,"warning_count":0}]}]

Page 29: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.29

Example SELECT * FROM world ORDER BY id

URL: http://127.0.0.1:8080/sql//SELECT+%2A+FROM+world+ORDER+BY+id

Page 30: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.30

CRUD

shell> curl ... --url "http://127.0.0.1:8080/crud/db/simple/1"

{"id":"1","col_a":"Ahoy"}

Page 31: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.31

DOCUMENT

shell> # curl -i -X PUT --user basic_auth_user:basic_auth_passwd --url "http://127.0.0.1:8080/doc/db/another_table"HTTP/1.1 201 Created...{"info": "Table created"}shell> # curl -i -X PUT -d '{"words": ["Hello", "world"]}' --user basic_auth_user:basic_auth_passwd –url "http://127.0.0.1:8080/doc/db/another_table/key"HTTP/1.1 200 OK...{"info": "Document added"}shell> # curl -X DELETE -i --user basic_auth_user:basic_auth_passwd --url "http://127.0.0.1:8080/doc/db/another_table/"HTTP/1.1 200 OK...{"info": "Table dropped"}

Page 32: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.32

Configuration – under [mysqld] section

## Default database, if no database given in URL#myhttp_default_db = world## Non-SSL default MySQL SQL user#myhttp_default_mysql_user_name = http_sql_usermyhttp_default_mysql_user_passwd = sql_secretmyhttp_default_mysql_user_host = 127.0.0.1

Page 33: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.33

Installation

mysql> INSTALL PLUGIN myhttp SONAME 'libmyhttp.so';

Page 34: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.34

Native JSON Data Type

mysql> CREATE TABLE employees (data JSON);Query OK, 0 rows affected (0,01 sec)mysql> INSERT INTO employees VALUES ('{"id": 1, "name": "Jane"}');Query OK, 1 row affected (0,00 sec)mysql> INSERT INTO employees VALUES ('{"id": 2, "name": "Joe"}');Query OK, 1 row affected (0,00 sec)mysql> select * from employees;+---------------------------+| data |+---------------------------+| {"id": 1, "name": "Jane"} || {"id": 2, "name": "Joe"} |+---------------------------+2 rows in set (0,00 sec)

Page 35: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.35

Validation and Effiency

Document Validation

● Only valid JSON documents can be stored in a JSON column, so you get automatic validation of your data. If you try to store an invalid JSON document in a JSON column, you will get an error

Efficient Access

● JSON document in a JSON column it is stored in an optimized binary format that allows for quicker access to object members and array elements.

● The binary format of a JSON column contains a preamble with a lookup table. The lookup table has pointers to every key/value pair in the JSON document, sorted on the key. The JSN_EXTRACT function to perform a binary search for the ‘name’ key in the table and read the corresponding value directly, without having to parse the ‘id’ key/value pair that precedes it within the JSON document.

Page 36: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.36

JSON Functions

● Manipulating JSON documents:

● jsn_array()

● jsn_object()

● jsn_insert()

● jsn_remove()

● jsn_set()

● jsn_replace()

● jsn_append()

● jsn_merge()

● jsn_extract()

● JSON Query:

● JSN_SEARCH()

● JSN_CONTAINS()

● JSN_CONTAINS_PATH()

● JSN_VALID()

● JSN_TYPE()

● JSN_KEYS()

● JSN_LENGTH()

● JSN_DEPTH()

● JSN_UNQUOTE()

● JSN_QUOTE()

Page 37: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.37

Quick Examplemysql> desc colors;+--------------+----------+------+-----+---------+-------------------+| Field | Type | Null | Key | Default | Extra |+--------------+----------+------+-----+---------+-------------------+| popular_name | char(10) | YES | | NULL | || hue | json | YES | | NULL | |+--------------+----------+------+-----+---------+-------------------+2 rows in set (0.00 sec)

INSERT INTO `colors` VALUES ('red','{\"value\": \"f00\"}'),('green','{\"value\": \"0f0\"}'),('blue','{\"value\": \"00f\"}'),('cyan','{\"value\": \"0ff\"}'),('magenta','{\"value\": \"f0f\"}'),('yellow','{\"value\": \"ff0\"}'),('black','{\"value\": \"000\"}');

Page 38: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.38

Using jsn_extract

mysql> SELECT jsn_extract(hue, '$.value') FROM colors WHERE jsn_extract(hue, '$.value')="f0f";+-----------------------------+| jsn_extract(hue, '$.value') |+-----------------------------+| "f0f" |+-----------------------------+1 row in set (0.00 sec)

Page 39: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.39

For More Information

● Mysql.com

● Labs.mysql.com

● Planet.mysql.com

● Dave Stokes

[email protected]

@stoker

slideshare.net/davestokes

Page 40: MySQL's NoSQL -- NYC Code Camp  July 18th 2015

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.40