欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

10分钟了解MySQL5.7对原生JSON的支持与用法

10分钟了解MySQL5.7对原生JSON的支持与用法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xjr56807/article/details/54232153



Part1:JSON格式的支持

版本终于支持了原生的JSON格式,即将关系型数据库和文档型NO_SQL数据库集于一身。本文接下来将对这特性分别就和各自实现的方法异同进行介绍和演示。


Part2:创建相应表结构


[root@HE3 ~]# mysql -V

mysql  Ver Distrib , for linux- (x86_64) using  EditLine wrapper


  1. mysql> create database helei;
  2. Query OK, 1 row affected ( sec)
  3. mysql> use helei;
  4. Database changed
  5. mysql> create table helei (id int(10) unsigned NOT NULL,context json default null,primary key(id));
  6. Query OK, 0 rows affected ( sec)
  7. mysql> show create table helei \G
  8. *************************** 1. row ***************************
  9.        Table: helei
  10. Create Table: CREATE TABLE `helei` (
  11.   `id` int(10) unsigned NOT NULL,
  12.   `context` json DEFAULT NULL,
  13.   PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  15. 1 row in set ( sec)


Part3:构造数据&测试

  1. mysql> desc helei;
  2. +---------+------------------+------+-----+---------+-------+
  3. | Field   | Type             | Null | Key | Default | Extra |
  4. +---------+------------------+------+-----+---------+-------+
  5. | id      | int(10) unsigned | NO   | PRI | NULL    |       |
  6. | context | json             | YES  |     | NULL    |       |
  7. +---------+------------------+------+-----+---------+-------+
  8. 2 rows in set ( sec)
  9. mysql> insert into helei values(1,'{"name":"贺磊","age":100}'),(2,'{"name":"陈加持","age":30}'),(3,'{"name":"于浩","age":28}');
  10. Query OK, 3 rows affected ( sec)
  11. Records: 3  Duplicates: 0  Warnings: 0
  12. mysql> select * from helei;
  13. +----+----------------------------------+
  14. | id | context                          |
  15. +----+----------------------------------+
  16. |  1 | {"age"100"name""贺磊"}     |
  17. |  2 | {"age"30"name""陈加持"}    |
  18. |  3 | {"age"28"name""于浩"}      |
  19. +----+----------------------------------+
  20. 3 rows in set ( sec)
  21. mysql> select id,JSON_EXTRACT(context,'$.name') name,JSON_EXTRACT(context,'$.age') age from helei;
  22. +----+-------------+------+
  23. | id | name        | age  |
  24. +----+-------------+------+
  25. |  1 | "贺磊"      | 100  |
  26. |  2 | "陈加持"    | 30   |
  27. |  3 | "于浩"      | 28   |
  28. +----+-------------+------+
  29. 3 rows in set ( sec)
  30. 获取Key-Value
  31. mysql> select id,json_keys(contextfrom helei;
  32. +----+--------------------+
  33. | id | json_keys(context) |
  34. +----+--------------------+
  35. |  1 | ["age""name"]    |
  36. |  2 | ["age""name"]    |
  37. |  3 | ["age""name"]    |
  38. +----+--------------------+
  39. 3 rows in set ( sec)
  40. 获取全部Key
  41. mysql> update helei set context=JSON_INSERT(context,'$.name',"贺磊",'$.address','beijing')where id=1;
  42. Query OK, 1 row affected ( sec)
  43. Rows matched: 1  Changed: 1  Warnings: 0
  44. mysql> select * from helei;
  45. +----+------------------------------------------------------+
  46. | id | context                                              |
  47. +----+------------------------------------------------------+
  48. |  1 | {"age"100"name""贺磊""address""beijing"}   |
  49. |  2 | {"age"30"name""陈加持"}                        |
  50. |  3 | {"age"28"name""于浩"}                          |
  51. +----+------------------------------------------------------+
  52. 3 rows in set ( sec)
  53. 增加Key-Value
  54. mysql> update helei set context=JSON_SET(context,'$.name',"高穷帅")where id=1;
  55. Query OK, 1 row affected ( sec)
  56. Rows matched: 1  Changed: 1  Warnings: 0
  57. mysql> select * from helei;
  58. +----+---------------------------------------------------------+
  59. | id | context                                                 |
  60. +----+---------------------------------------------------------+
  61. |  1 | {"age"100"name""高穷帅""address""beijing"}    |
  62. |  2 | {"age"30"name""陈加持"}                           |
  63. |  3 | {"age"28"name""于浩"}                             |
  64. +----+---------------------------------------------------------+
  65. 3 rows in set ( sec)
  66. 变更key-value
  67. mysql> update helei set context=JSON_REMOVE(context,'$.name'where id=1;
  68. Query OK, 1 row affected ( sec)
  69. Rows matched: 1  Changed: 1  Warnings: 0
  70. mysql> select * from helei;
  71. +----+------------------------------------+
  72. | id | context                            |
  73. +----+------------------------------------+
  74. |  1 | {"age"100"address""beijing"} |
  75. |  2 | {"age"30"name""陈加持"}      |
  76. |  3 | {"age"28"name""于浩"}        |
  77. +----+------------------------------------+
  78. 3 rows in set ( sec)
  79. 删除Key-Value



JSON格式存储BLOB的测试

Part1:Dynamic Columns处理方式的异同

的动态列JSON格式存储

  1. mysql> insert into helei_blob values(1,'{"name":"贺磊","age":100}'),(2,'{"name":"陈加持","age":30}'),(3,'{"name":"于浩","age":28}');
  2. Query OK3 rows affected ( sec)
  3. Records3  Duplicates0  Warnings0
  4. mysql> select * from helei_blob;
  5. +----+-------------------------------+
  6. | id | blob_col                      |
  7. +----+-------------------------------+
  8. |  1 | {"name":"贺磊","age":100}     |
  9. |  2 | {"name":"陈加持","age":30}    |
  10. |  3 | {"name":"于浩","age":28}      |
  11. +----+-------------------------------+
  12. 3 rows in set ( sec)


MariaDB的动态列JSON格式存储

  1. MariaDB [helei]> create table helei (id int(10) unsigned NOT NULL,context json default null,primary key(id));
  2. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json default null,primary key(id))' at line 1
  3. 可以看到MariaDB并不能直接存储JSON类型。
  4. MariaDB [helei]> show create table helei_blob\G;
  5. *************************** 1. row ***************************
  6.        Table: helei_blob
  7. Create Table: CREATE TABLE `helei_blob` (
  8.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  9.   `blob_col` blob,
  10.   PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  12. 1 row in set ( sec)
  13. ERROR: No query specified
  14. MariaDB [helei]> insert into helei_blob values(5,column_create('color','blue','size','XL'));
  15. Query OK, 1 row affected ( sec)
  16. MariaDB [helei]> select * from helei_blob;
  17. +----+--------------------------------+
  18. | id | blob_col                       |
  19. +----+--------------------------------+
  20. |  1 | {"name":"贺磊","age":100}      |
  21. |  2 | {"name":"陈加持","age":30}     |
  22. |  3 | {"name":"于浩","age":28}       |
  23. |  5 |        3 sizecolor!XL!blue |
  24. +----+--------------------------------+
  25. 4 rows in set ( sec)
  26. 直接查询是乱码需用以下函数查询
  27. MariaDB [helei]> select id,column_json(blob_colfrom helei_blob where id =5;
  28. +----+------------------------------+
  29. | id | column_json(blob_col)        |
  30. +----+------------------------------+
  31. |  5 | {"size":"XL","color":"blue"} |
  32. +----+------------------------------+
  33. 1 row in set ( sec)
  34. MariaDB [helei]> select id,column_list(blob_colfrom helei_blob where id =5;
  35. +----+-----------------------+
  36. | id | column_list(blob_col) |
  37. +----+-----------------------+
  38. |  5 | `size`,`color`        |
  39. +----+-----------------------+
  40. 1 row in set ( sec)
  41. 获取全部Key
  42. MariaDB [helei]> select id,column_get(blob_col,'color' as charas color from helei_blob where id =5;
  43. +----+-------+
  44. | id | color |
  45. +----+-------+
  46. |  5 | blue  |
  47. +----+-------+
  48. 1 row in set ( sec)
  49. 获取Key-Value
  50. MariaDB [helei]> update helei_blob set blob_col=column_add(blob_col,'sex','man'where id=5;
  51. Query OK, 1 row affected ( sec)
  52. Rows matched: 1  Changed: 1  Warnings: 0
  53. MariaDB [helei]> select id,column_json(blob_colfrom helei_blob where id=5;
  54. +----+------------------------------------------+
  55. | id | column_json(blob_col)                    |
  56. +----+------------------------------------------+
  57. |  5 | {"sex":"man","size":"XL","color":"blue"} |
  58. +----+------------------------------------------+
  59. 1 row in set ( sec)
  60. 增加Key-Value
  61. MariaDB [helei]> update helei_blob set blob_col=column_add(blob_col,'color','black'where id=5;
  62. Query OK, 1 row affected ( sec)
  63. Rows matched: 1  Changed: 1  Warnings: 0
  64. MariaDB [helei]> select id,column_json(blob_colfrom helei_blob where id=5;
  65. +----+-------------------------------------------+
  66. | id | column_json(blob_col)                     |
  67. +----+-------------------------------------------+
  68. |  5 | {"sex":"man","size":"XL","color":"black"} |
  69. +----+-------------------------------------------+
  70. 1 row in set ( sec)
  71. 更改Key-Value
  72. MariaDB [helei]> update helei_blob set blob_col=column_delete(blob_col,'sex','man'where id=5;
  73. Query OK, 1 row affected ( sec)
  74. Rows matched: 1  Changed: 1  Warnings: 0
  75. MariaDB [helei]> select id,column_json(blob_colfrom helei_blob where id=5;
  76. +----+-------------------------------+
  77. | id | column_json(blob_col)         |
  78. +----+-------------------------------+
  79. |  5 | {"size":"XL","color":"black"} |
  80. +----+-------------------------------+
  81. 1 row in set ( sec)
  82. 删除Key-Value



——总结——

虽然和/版本对于JSON的支持是比较完整的,不过MongoDB的Sharding功能更加好用,个人更倾向于MongoDB。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。



普通分类: