ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

23
1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~ 日本オラクル株式会社 山崎 由章 / MySQL Senior Sales Consultant, Asia Pacific and Japan

description

MyNA会 2013年9月での発表資料に加筆修正しました。JSON UDFを使って、フィードバックレポートを下さい!!

Transcript of ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

Page 1: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

ドキュメントデータベースとして

MySQLを使う!?

~MySQL JSON UDF~

日本オラクル株式会社

山崎 由章 / MySQL Senior Sales Consultant,

Asia Pacific and Japan

Page 2: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはできません。以下の事項は、マテリアルやコード、機能を提供することをコミットメント(確約)するものではないため、購買決定を行う際の判断材料になさらないで下さい。オラクル製品に関して記載されている機能の開発、リリースおよび時期については、弊社の裁量により決定されます。

OracleとJavaは、Oracle Corporation 及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。

Page 3: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

MySQL Connect 2013の中でこんなセッションが・・・

One More Step to the NoSQL Side: MySQL JSON Functions

Page 4: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

http://labs.mysql.com/ にアクセスしてみると・・・

MySQL JSON UDFs !?

Page 5: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

MySQLにJSONを扱うための関数を追加できる!! ※現時点(2013/9/30)では、Lab版(実験版)

Page 6: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

セットアップ方法(Linux/UNIX環境の場合) ※詳細はREADMEファイル参照

1. http://labs.mysql.com/ から“MySQL JSON UDFs”を

ダウンロード

2.ダウンロードしたファイルに含まれている“libmy_json_udf.so”を

plugin_dir に配置

(plugin_dirの確認方法:mysql> show global variables like 'plugin_dir';)

3.CREATE FUNCTIONコマンドでUDFを作成

※UDF(User-DefinedFunction:ユーザ定義関数)

Page 7: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

mysql> create function json_valid returns integer soname 'libmy_json_udf.so';

mysql> create function json_search returns string soname 'libmy_json_udf.so';

mysql> create function json_extract returns string soname 'libmy_json_udf.so';

mysql> create function json_replace returns string soname 'libmy_json_udf.so';

mysql> create function json_append returns string soname 'libmy_json_udf.so';

mysql> create function json_remove returns string soname 'libmy_json_udf.so';

mysql> create function json_set returns string soname 'libmy_json_udf.so';

mysql> create function json_merge returns string soname 'libmy_json_udf.so';

mysql> create function json_contains_key returns integer soname 'libmy_json_udf.so';

■CREATE FUNCTION

セットアップ方法(Linux/UNIX環境の場合) ※詳細はREADMEファイル参照

Page 8: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

補足:READMEファイルのバグ

Linux/UNIX環境に関して、ファンクション作成コマンドが

以下の通り紹介されているが、ライブラリファイルの名前は

‘libmy_json_udf.so’となっている。

ファンクション作成コマンド

create function json_valid returns integer soname 'libmy_json.so';

※http://bugs.mysql.com/bug.php?id=70392

Page 9: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

JSON UDFのデモ

Page 10: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

サンプルテーブル

mysql> select * from json;

+----+--------------------------------------------------------------------------------+

| id | col1 |

+----+--------------------------------------------------------------------------------+

| 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":["farms",15]} |

| 2 | {"id":2,"Name":"Worker grandmas","price":300000,"Conditions":["factories",15]} |

| 3 | {"id":3,"Name":"Miner grandmas","price":1000000,Conditions:["mines",15]} |

| 4 | {"id":4,"Name":"Yoshiaki Yamasaki"} |

+----+--------------------------------------------------------------------------------+

4 rows in set (0.00 sec)

※id=3の列には、フォーマット間違い有り

(””が抜けている)

※id=4の列はNameだけ

Page 11: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_valid:JSONドキュメントのフォーマットチェック (フォーマットが正しければ1、正しくなければ0)

mysql> select id,json_valid(col1) from json;

+----+------------------+

| id | json_valid(col1) |

+----+------------------+

| 1 | 1 |

| 2 | 1 |

| 3 | 0 |

| 4 | 1 |

+----+------------------+

4 rows in set (0.00 sec)

Page 12: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_search:値が含まれるキーを検索

mysql> select id,json_search(col1,'"Farmer grandmas"') from json where id=1; +----+---------------------------------------+ | id | json_search(col1,'"Farmer grandmas"') | +----+---------------------------------------+ | 1 | Name:: | +----+---------------------------------------+ 1 row in set (0.00 sec) mysql> select json_search(col1,'"farms"') from json; +-----------------------------+ | json_search(col1,'"farms"') | +-----------------------------+ | 0:Conditions:: | | NULL | | NULL | | NULL | +-----------------------------+ 4 rows in set (0.00 sec)

Page 13: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_extract:値を抽出

mysql> select id,json_extract(col1,'price') from json; +----+----------------------------+ | id | json_extract(col1,'price') | +----+----------------------------+ | 1 | 50000 | | 2 | 300000 | | 3 | NULL | | 4 | NULL | +----+----------------------------+ 4 rows in set (0.00 sec) mysql> select id,json_extract(col1,'Conditions') from json; +----+---------------------------------+ | id | json_extract(col1,'Conditions') | +----+---------------------------------+ | 1 | ["farms",15] | | 2 | ["factories",15] | | 3 | NULL | | 4 | NULL | +----+---------------------------------+ 4 rows in set (0.00 sec)

Page 14: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_remove:特定の要素を除去

mysql> select id,json_remove(col1,'price') from json;

+----+------------------------------------------------------------------+

| id | json_remove(col1,'price') |

+----+------------------------------------------------------------------+

| 1 | {"id":1,"Name":"Farmer grandmas",,"Conditions":["farms",15]} |

| 2 | {"id":2,"Name":"Worker grandmas",,"Conditions":["factories",15]} |

| 3 | NULL |

| 4 | {"id":4,"Name":"Yoshiaki Yamasaki"} |

+----+------------------------------------------------------------------+

4 rows in set (0.00 sec)

Page 15: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_merge:JSONドキュメントのマージ

mysql> select id,json_merge(col1,col1) from json where id=1;

+----+----------------------------------------------------------------------------------------------------------------------------------------------------+

| id | json_merge(col1,col1) |

+----+----------------------------------------------------------------------------------------------------------------------------------------------------+

| 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":["farms",15], "id":1,"Name":"Farmer grandmas","price":50000,"Conditions":["farms",15]} |

+----+----------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

Page 16: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_contains_key:特定のキーが含まれているか確認

mysql> select id,json_contains_key(col1,'Conditions') from json;

+----+--------------------------------------+

| id | json_contains_key(col1,'Conditions') |

+----+--------------------------------------+

| 1 | 1 |

| 2 | 1 |

| 3 | 0 |

| 4 | 0 |

+----+--------------------------------------+

4 rows in set (0.00 sec)

Page 17: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_replace:値を置換

mysql> select id,json_replace(col1,'Name','"農家のおばあちゃん"') from json where id=1;

+----+--------------------------------------------------------------------------------------+

| id | json_replace(col1,'Name','"農家のおばあちゃん"') |

+----+--------------------------------------------------------------------------------------+

| 1 | {"id":1,"Name":"農家のおばあちゃん","price":50000,"Conditions":["farms",15] |

+----+--------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

※最後の’}’が欠けている現象については、以下のbugレポートを登録済み

http://bugs.mysql.com/bug.php?id=70486

Page 18: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_replace:値を置換

mysql> select id,json_replace(col1,'Conditions','0','10') from json where id=1; +----+----------------------------------------------------------------------+ | id | json_replace(col1,'Conditions','0','10') | +----+----------------------------------------------------------------------+ | 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":[10,15]} | +----+----------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> select id,json_replace(col1,'Conditions','1','10') from json where id=1; +----+---------------------------------------------------------------------------+ | id | json_replace(col1,'Conditions','1','10') | +----+---------------------------------------------------------------------------+ | 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":["farms",10]} | +----+---------------------------------------------------------------------------+ 1 row in set (0.00 sec)

Page 19: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_set:値を設定

mysql> select id,json_set(col1,'Name','"農家のおばあちゃん"') from json where id=1;

+----+--------------------------------------------------------------------------------------+

| id | json_set(col1,'Name','"農家のおばあちゃん"') |

+----+--------------------------------------------------------------------------------------+

| 1 | {"id":1,"Name":"農家のおばあちゃん","price":50000,"Conditions":["farms",15] |

+----+--------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

Page 20: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_set:値を設定

mysql> select id,json_set(col1,'Conditions','0','10') from json where id=1;

+----+----------------------------------------------------------------------+

| id | json_set(col1,'Conditions','0','10') |

+----+----------------------------------------------------------------------+

| 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":[10,15]} |

+----+----------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> select id,json_set(col1,'Conditions','1','10') from json where id=1;

+----+---------------------------------------------------------------------------+

| id | json_set(col1,'Conditions','1','10') |

+----+---------------------------------------------------------------------------+

| 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":["farms",10]} |

+----+---------------------------------------------------------------------------+

1 row in set (0.00 sec)

Page 21: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_set:値を設定

mysql> select id,json_set(col1,'Conditions','2','10') from json where id=1;

+----+-------------------------------------------------------------------------------+

| id | json_set(col1,'Conditions','2','10') |

+----+-------------------------------------------------------------------------------+

| 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":["farms",15, 10]} |

+----+-------------------------------------------------------------------------------+

1 row in set (0.00 sec)

Page 22: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

json_append:配列に値を追加

mysql> select id,json_append(col1,'Conditions','2','10') from json where id=1;

+----+-------------------------------------------------------------------------------+

| id | json_append(col1,'Conditions','2','10') |

+----+-------------------------------------------------------------------------------+

| 1 | {"id":1,"Name":"Farmer grandmas","price":50000,"Conditions":["farms",15, 10]} |

+----+-------------------------------------------------------------------------------+

1 row in set (0.00 sec)

Page 23: ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

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

色々試して、http://bugs.mysql.com/まで

フィードバックを下さい!!

※今のところ(2013/9/30) JSON UDF専用のカテゴリは無いので、

「MySQL Server: User-defined functions (UDF)」の

カテゴリでレポートを下さい。