+---+
| CAST(1 AS JSON) = 1 | +---+
| 1 | +---+
1 row in set (0.01 sec)
SELECT CAST('{"a": 10, "num": 1.1}' AS JSON) = CAST('{"num": 1.1, "a":10}' AS JSON);
+---+
| CAST('{"a": 10, "num": 1.1}' AS JSON) = CAST('{"num": 1.1, "a":10}' AS JSON) | +---+
| 1 | +---+
1 row in set (0.00 sec)
JSON Objects
の比較JSON
の1
は1
と同等Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JSON Comparator (cont.)
42
JSON
の値とMySQL
の値をJOIN
処理CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY, a INTEGER NOT NULL);
CREATE TABLE t2 (id INT NOT NULL PRIMARY KEY, doc JSON NOT NULL);
INSERT INTO t1 VALUES (1, 1), (2, 2);
INSERT INTO t2 VALUES (1, JSON_OBJECT('a', 1)), (2, JSON_OBJECT('a', 2));
SELECT t1.id, t1.a, t2.doc FROM t1 INNER JOIN t2 ON (t1.a=t2.doc->"$.a");
+----+---+---+
| id | a | doc | +----+---+---+
| 1 | 1 | {"a": 1} |
| 2 | 2 | {"a": 2} |
+----+---+---+
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Today’s Agenda
43
Introduction
Core New JSON Features Use cases
Document Store Enhancement feature
1
2 2 1
2
3
2
2
4
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
リレーショナルそれともスキーマレス ?
44
両方共にそれぞれのアプローチとメリット
mysql> SELECT * FROM pizza;
+---+---+---+
| code | name | price | +---+---+---+
| CLA | Classic Pizza | 400 |
| MAR | Margherita Pizza | 500 | +---+---+---+
mysql> SELECT * FROM toppings;
+---+---+
| p_code | name | +---+---+
| CLA | Pepperoni |
| CLA | Parmesan |
| MAR | Basil |
| MAR | Mozzarella | +---+---+
{
"name":"Classic Pizza", "price":400,
"toppings":[
"Pepperoni", "Parmesan"
] } {
"name":"Margherita Pizza", "price":500,
"toppings":[
"Basil", "Mozzarella"
],
"options":[
{
"name":"Olive", "price":100 }
] }
Schema Schema Less (JSON
ドキュメント)
属性値のペアのコレクション 表
,
カラム,
行Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Schema: Column へのデータ格納
45
•
アプリケーションに対し、スキーマの適用が容易•
スキーマにより長期的に、アプリケーションの変更管理がコントロールしやすい-
表,
カラム,
行を使用して効率よくデータを管理-
データにいくつかの制約を設定可能(
データ型、外部キー制約等) -
データ重複を削減する事が可能(
正規化)
+---+---+---+
| id | type | street | +---+---+---+
| 190838 | Feature | FARALLONES |
| 153676 | Feature | ELLSWORTH |
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Schema Less: JSON へのデータ格納
46
● スキーマ内でモデル化する事が困難であるデータを柔軟に表現。
-
例)
多くのお客様へSaaS
アプリケーション提供してる場合-
強力なユースケースとしてカスタムフィールドをサポート-
歴史的に エンティティ属性値モデル(EAV)
が使われているが、必ずしも十分に機能していない場合等
●
容易な非正規化は,
特定の状況 において重要である最適化手法●
容易なプロトタイピング1)
考慮すべき型 が少なくて済む2)
スキーマ強制不要,
直ぐに値を格納可能●
労力を伴うスキーマ変更不要* (MySQL 5.6+ has Online DDL)
+---+
| json_object |
+---+
| {"id": 190838, "type": "Feature", "street": "FARALLONES"} |
| {"id": 153676, "type": "Feature", "street": "ELLSWORTH"} |
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Schema + Schemaless
47
CREATE TABLE pc_components ( id INT NOT NULL PRIMARY KEY,
description VARCHAR(60) NOT NULL, vendor VARCHAR(30) NOT NULL,
serial_number VARCHAR(30) NOT NULL, attributes JSON NOT NULL);
Column データと JSON データ連携 Relational Tables
Schema less JSON Collection MySQL5.7
開発チーム
:
[ x ]
スキーマレス/
スキーマ[ x ]
迅速なプロトタイプ/
シンプルAPI
[ x ]
ドキュメントモデルオペレーション
:
[ x ]
パフォーマンス管理/
可視化[ x ]
堅牢レプリケーション,
バックアップ,
リストア[ x ]
包括的なツールによるエコシステムビジネス
:
[ x ]
データを確実に保護= ACID
トランザクション[ x ]
全てのデータをキャプチャー=
拡張性/
スキーマレス[ x ]
スケージュール/
製品化の時間=
迅速なサービス開発リレーショナル、スキーマレスデータを 同じ技術スタックで利用可能
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Resources
• http://mysqlserverteam.com/
• http://mysqlserverteam.com/tag/json/
• https://dev.mysql.com/doc/refman/5.7/en/mysql-nutshell.html
• http://dev.mysql.com/doc/relnotes/mysql/5.7/en/
• https://dev.mysql.com/doc/refman/5.7/en/json.html
• https://dev.mysql.com/doc/refman/5.7/en/json-functions.html
• http://www.thecompletelistoffeatures.com
今後も更に、関数を含む多くの機能追加を計画しています。
48
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Today’s Agenda
49
Introduction
Core New JSON Features Use cases
Document Store Enhancement feature
1
2 2 1
2
4
2
2
3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
コネクター , ドライバー , プロトコル拡張機能
MySQL
Plugins
X Protocol Plugin Memcached Plugin Core
MySQL Connectors and Drivers
X Protocol Std Protocol
Memcached driver
X Protocol 33060 Std Protocol
3306
SQL API CRUD and SQL APIs
Memcache Protocol X and Std
Protocols
MySQL Shell
50 +---+---+---+
| PLUGIN_NAME | PLUGIN_VERSION | PLUGIN_DESCRIPTION | +---+---+---+
| mysqlx | 1.0 | X Plugin for MySQL | +---+---+---+
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
X DevAPI
• X Plugin を有効にする事で、 X Protocol 経由で通信可能
• ドキュメントとテーブルのコレクションに対しての CRUD 処理
• NoSQL ライクな構文でドキュメントに対し CRUD 処理可能
• Fluent API
prod = sess.getSchema("prod") res = prod.users.
find("$.name = 'Milk'").
fields(["name", "properties"])
X Plugin (MySQL) ⇔ X Protocol ⇔ X DevAPI (Driver)
51
MySQL Connector/node.js (1.0.x)
MySQL Connector/J (6.0.x)
MySQL Connector/Net (7.0.x)
MySQL Connector/python(2.2.x)
MySQL Shell (1.0.x)
参照:
http://dev.mysql.com/downloads/connector/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Connectors include X Dev API
• Use SQL, CRUD APIs
スキーマレスドキュメントおよびリレーショナルテーブルに対応
- Classic APIs に加えて、これらの全てが追加されます
52
Operation Document Relational
Create Collection.add() Table.insert()
Read Collection.find() Table.select()
Update Collection.modify() Table.update()
Delete Collection.remove() Table.delete()
参照
) http://dev.mysql.com/doc/x-devapi-userguide/en/crud-operations-overview.html
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 53
[root@misc01 nodejs]# cat sample_node_X_API.js const mysqlx = require('mysqlx');
mysqlx.getSession({ host: 'localhost', port: 33060, dbUser: 'demo_user', dbPassword: 'password' }).then(function (session) {
return session.createSchema("test_schema").then(function (schema) { return schema.createCollection("myCollection");
}).then(function (collection) { return Promise.all([
collection.add( {baz: { foo: "bar"}},{foo: { bar: "baz"}}).execute(),
collection.find("$.baz.foo == 'bar'").execute(function (row) {console.log("Row: %j", row);
}).then(function (res) {console.log("Collection find done!");}),
collection.remove("($.foo.bar) == 'baz'").execute().then(function () { console.log("Document deleted");}), collection.drop() ]);
}).then(function () { return session.dropSchema("test_schema");
}).then(function () { return session.close(); });
}).catch(function (err) { console.log(err.stack); process.exit(); });
[root@misc01 nodejs]# node sample_node_X_API.js
Row: {"_id":"630f0d3b-f6fd-1d99-6d80-a8e90352","baz":{"foo":"bar"}}
Collection find done!
Document deleted Connector
:mysql-connector-nodejs-1.0.5.tar.gz
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |