Contenu connexe Similaire à ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~ (20) ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~1. 1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ドキュメントデータベースとして
MySQLを使う!?
~MySQL JSON UDF~
日本オラクル株式会社
山崎 由章 / MySQL Senior Sales Consultant,
Asia Pacific and Japan
2. 2 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。
また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことは
できません。以下の事項は、マテリアルやコード、機能を提供することをコミットメン
ト(確約)するものではないため、購買決定を行う際の判断材料になさらないで下さ
い。オラクル製品に関して記載されている機能の開発、リリースおよび時期につい
ては、弊社の裁量により決定されます。
OracleとJavaは、Oracle Corporation 及びその子会社、関連会社の米国及びその他の国における登録商標です。文中
の社名、商品名等は各社の商標または登録商標である場合があります。
3. 3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
MySQL Connect 2013の中でこんなセッションが・・・
One More Step to the NoSQL Side: MySQL JSON Functions
4. 4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
http://labs.mysql.com/ にアクセスしてみると・・・
MySQL JSON UDFs !?
5. 5 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
MySQLにJSONを扱うための
関数を追加できる!!
※現時点(2013/9/30)では、Lab版(実験版)
6. 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:ユーザ定義関数)
7. 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ファイル参照
8. 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
9. 9 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
JSON UDFのデモ
10. 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だけ
11. 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)
12. 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)
13. 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)
14. 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)
15. 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)
16. 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)
17. 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
18. 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)
19. 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)
20. 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)
21. 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)
22. 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)
23. 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)」の
カテゴリでレポートを下さい。