20160821 coscup-my sql57docstorelab01

46
Copyright © 2015, Ora cle and/or i ts a ffiliates. All rights reserved. | 重新認識MySQL- 融合SQLNoSQL 兩個世界於一爐的MySQL 5.7 梶山隆輔(EN)及 馬楚成(ZH2016-08-21 Copyright © 2015, Ora cle and/or i ts a ffiliates. All rights reserved.

Upload: ivan-ma

Post on 14-Apr-2017

333 views

Category:

Software


2 download

TRANSCRIPT

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

重新認識MySQL-融合SQL和NoSQL 兩個世界於一爐的MySQL 5.7 梶山隆輔(EN)及 馬楚成(ZH) 2016-08-21

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved.

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

Safe Harbor Statement

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.

2

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

新! MySQL Shell – 主要功能

• 多語言 - 的JavaScript,Python中和SQL語言

• 支持文檔和關係模型

• 互動和批次處理模式

• 三個結果的格式 - TABLE,JSON,Tab分隔,

• API

– Sessions

– Schemas

– Collections

– Tables

集成开发和管理命令行管理程序

3

– CRUD on Tables and Collections

– SQL execution

– Result Processing

– Parameter Binding

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

新! Documentation

• Developer Guides

• Examples

• LINKS HERE

新樣式 - 重點在開發人員

4

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

The Workshop

• Install Virtualbox (Latest Version)

• VM

– 201608COSCUPDocStore.ova

– IMPORT to the VirtualBox

5

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

Disable

• USB

6

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

Network

• DHCP (HOST-ONLY) e.g. 192.168.56.102 – Check your IP if needed

– # ip addr

• Adapter 2 (Bridge) – Disable (Not used in Lab)

7

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

Using putty / ssh client

• Using putty or ssh client to login to the OS

• User name : root

• Password : password

• Folder : /opt/mysql/solution (The main Lab Folder)

• For ALL operations, use mysql user. (Do not use root user)

– # su - mysql

8

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

Lab 01 : JSON

• Folder : /opt/mysql/solution/12-JSON CREATE DATABASE if not exists mytest;

CREATE TABLE if not exists mytest.widgets (

id int(10) NOT NULL AUTO_INCREMENT,

widget JSON NOT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB;

9

INSERT INTO mytest.widgets (widget) VALUES ('{"series": 1}'), ('{"series": 7}'), ('{"series": 3}'), ('{"series": 4}'), ('{"series": 10}'), ('{"series": 2}'), ('{"series": 6}'), ('{"series": 5}'), ('{"series": 8}'), ('{"series": 11}'); select count(*) from mytest.widgets;

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

JSON - SELECT

• select JSON_TYPE('["a", "b", 1]');

• select JSON_TYPE('"hello"');

• select JSON_EXTRACT('{"series": 4}', '\$.series');

• select JSON_EXTRACT('[10, 20, [30, 40]]','\$[1]');

• select JSON_EXTRACT('[10, 20, [30, 40]]','\$[2]');

• SELECT JSON_EXTRACT('[10, 20, [30, 40]]', '\$[1]', '\$[0]');

• SELECT JSON_EXTRACT('[10, 20, [30, 40]]', '\$[2][*]');

• select JSON_ARRAY('a', 1, NOW());

• select JSON_OBJECT('key1', 1, 'key2', 'abc', 'key3', '["a","b", 1]');

• SELECT JSON_MERGE('["a", 1]', '{"key": "value"}');

10

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

JSON - SELECT

• select JSON_EXTRACT(widget, "$.series") from mytest.widgets;

• select widget->"$.series" from mytest.widgets ;

• select widget->>"$.series" from mytest.widgets ;

• select JSON_EXTRACT('{"name":"My Object", "numbers":[2,4,6,8,10,12,14,16,18,20]}', "$.numbers[1]");

• select JSON_TYPE( '[{"price":10,"name":"hello"}, {"price":12,"name":"hello2"},{"price":16,"name":"hello3"}, {"price":18,"name":"hello4"}]' );

11

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

GENERATED COLUMN

• alter table mytest.features add feature_type varchar(30) as (feature->"$.type") { VIRTUAL | STORED};

12

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

IMPORT (LOAD DATA) JSON to table

• Check properties.json

• load data infile '/opt/mysql/solution/12-JSON/properties.json' into table mytest.features (feature);

06-import.sh

13

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

MySQL Connectors and Driver

MySQL (+ Document Store)架构

14

MySQL

Plugins

X Protocol Plugin Memcached Plugin Core

X Protocol Std Protocol

X Protocol 33060

Std Protocol 3306

SQL API CRUD API

X and Std Protocols

MySQL Shell

memcached Client

memcache Protocol

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 15

Find row(s) with Name = ‘Aruba’ andGNP = 828

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 16

alter table mytest01 add myname varchar(30) as (json_extract(doc, "$.myname") ); alter table mytest01 add mycompany varchar(30) as (json_extract(doc, "$.mycompany") );

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 17

alter table mytest01 add unique key mytest01_myname (myname);

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

Lab02- MySQL Document Store (工作坊)

• VM已準備

– MySQL 5.7.14

– MySQL Shell 1.0.4

– Nodejs and Connector

• 1. 安裝MySQLX插件

• 2. Starting MySQL Shell (mysqlsh) login session

• 3. Loading Sample Database “world_x”

• 4. Schema, Collection, Tables, Query using MySQL Shell

• 5. Creating New Collection and Inserting Data ( in Transaction mode)

• 6. Error Handling

• 7. Adding Generated Column to Collection

18

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

1. X-Plugin安裝

• install plugin mysqlx soname 'mysqlx.so';

• Create a “LOCKED” user “mysqlxyss@localhost”

– CREATE USER IF NOT EXISTS mysqlxsys@localhost IDENTIFIED WITH

– mysql_native_password AS 'password' ACCOUNT LOCK;

– GRANT SELECT ON mysql.user TO mysqlxsys@localhost;

– GRANT SUPER ON *.* TO mysqlxsys@localhost;

00-installx.sh

19

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

2. Starting MySQL Shell (mysqlsh) login session

• Command (mysqlsh)

• mysqlsh --uroot --h127.0.0.1 --P33060

– mysql-js> \help

• Quit mysqlsh – mysql-js> \quit

• Showing the session – mysql-js>session

01-login.sh

20

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

2. XSession & NodeSession

• mysql-js> var mysqlx = require(‘mysqlx’).mysqlx;

• mysql-js> var xsession = mysqlx.getSession(‘root:pwd@localhost:33060’);

• mysql-js> var nsession = mysqlx.getNodeSession(‘root:pwd@localhost:33060’);

• nsession.sql(‘use test’);

• nsession.sql(‘select 1’);

21

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

3. Loading ‘world_x’ sample database

• Download from URL : http://dev.mysql.com/doc/index-other.html ‘world_x’

• 03-createWorldX.sh

– mysql -uroot -h127.0.0.1 < /opt/mysql/packages/world_x-db/world_x.sql

http://dev.mysql.com/doc/index-other.html

22

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

4. Schema, Collection, Tables using MySQL Shell

• var myworld_x = session.getSchema('world_x');

• var mytables = myworld_x.getTables();

• var mycollections = myworld_x.getCollections();

• print("Schema : " , myworld_x);

• print("Tables :" , mytables);

• print("Collections :" , mycollections);

04a-printSTC.sh (Print Schema, Tables, Collection)

23

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

4. Query Collection / Table

• print("Table : City [SELECT]");

• myworld_x.City.select().limit(5).execute();

• print("Collection : CountryInfo [FIND]");

• myworld_x.CountryInfo.find().limit(1);

• print("Collection : CountryInfo [FIND - condition GNP > 1000 and Name like 'A%']");

• myworld_x.CountryInfo.find("GNP > 1000 and Name like 'A%'").limit(1);

04c-query.sh

24

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

5. Creating Collection and Transaction Data

• var myworld_x = session.getSchema('world_x');

• try {

• var myt = myworld_x.getCollection('mytest01') ;

• session.dropCollection('world_x', 'mytest01');

• } catch (err) {

• print("[INFO] Collection not found : mytest01");

• }

• try {

– var myColl = myworld_x.createCollection('mytest01');

– ….

11-createCollection.sh

25

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

5. Creating Collection and Transaction Data

• session.startTransaction();

• try {

• myColl.add({name: 'Jack', age: 15, height: 1.76, weight: 69.4}).execute();

• myColl.add({name: 'Susanne', age: 24, height: 1.65}).execute();

• myColl.add({name: 'Mike', age: 39, height: 1.9, weight: 74.3}).execute();

• // Commit the transaction if everything went well

• session.commit();

• print('Data inserted successfully.');

• } catch (err) {

session.rollback();

• }

Transaction Data

26

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

6. Error Handling

• Try, Catch, Finally

• ./13-errorHandling.sh

13-errorHandling.sh

27

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

6. Error Handling

• Try, Catch, Finally (passing root as user)

• ./13-errorHandling.sh root

13-errorHandling.sh

28

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. |

6. Error Handling

• Try, Catch, Finally (passing root as user and world_x as database)

• ./13-errorHandling.sh root world_x

13-errorHandling.sh

29

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 30

Listing Schemas

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 31

Javascript var Defining allsch to be assigned with schemas list

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 32

HELP

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 33

Status

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 34

Assign var db as the schema pointing to

“world_x”

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 35

Listing rows (Limit to only 1 row)

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 36

Find row(s) with Name = ‘Aruba’

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 37

Find row(s) with Name = ‘Aruba’ andGNP = 828

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 38

Create a new Collection “mytest01”

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 39

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 40

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 41

alter table mytest01 add myname varchar(30) as (json_extract(doc, "$.myname") ); alter table mytest01 add mycompany varchar(30) as (json_extract(doc, "$.mycompany") );

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 42

alter table mytest01 add unique key mytest01_myname (myname);

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 43

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 44

Copyright © 2015, Oracle and/or i ts affiliates. All rights reserved. | 45