10分钟了解MySQL5.7对原生JSON的支持与用法
Part1:JSON格式的支持
MySQL5.7版本终于支持了原生的JSON格式,即将关系型数据库和文档型NO_SQL数据库集于一身。本文接下来将对这特性分别就MySQL5.7和MariaDB10.1各自实现的方法异同进行介绍和演示。
Part2:创建相应表结构
[root@HE3 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.15, for linux-glibc2.5 (x86_64) using EditLine wrapper
mysql> create database helei; Query OK, 1 row affected (0.00 sec) mysql> use helei; Database changed mysql> create table helei (id int(10) unsigned NOT NULL,context json default null,primary key(id)); Query OK, 0 rows affected (0.02 sec) mysql> show create table helei \G *************************** 1. row *************************** Table: helei Create Table: CREATE TABLE `helei` ( `id` int(10) unsigned NOT NULL, `context` json DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.02 sec)
Part3:构造数据&测试
mysql> desc helei; +---------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | NULL | | | context | json | YES | | NULL | | +---------+------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> insert into helei values(1,'{"name":"贺磊","age":100}'),(2,'{"name":"陈加持","age":30}'),(3,'{"name":"于浩","age":28}'); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from helei; +----+----------------------------------+ | id | context | +----+----------------------------------+ | 1 | {"age": 100, "name": "贺磊"} | | 2 | {"age": 30, "name": "陈加持"} | | 3 | {"age": 28, "name": "于浩"} | +----+----------------------------------+ 3 rows in set (0.00 sec) mysql> select id,JSON_EXTRACT(context,'$.name') name,JSON_EXTRACT(context,'$.age') age from helei; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | "贺磊" | 100 | | 2 | "陈加持" | 30 | | 3 | "于浩" | 28 | +----+-------------+------+ 3 rows in set (0.00 sec) 获取Key-Value mysql> select id,json_keys(context) from helei; +----+--------------------+ | id | json_keys(context) | +----+--------------------+ | 1 | ["age", "name"] | | 2 | ["age", "name"] | | 3 | ["age", "name"] | +----+--------------------+ 3 rows in set (0.00 sec) 获取全部Key mysql> update helei set context=JSON_INSERT(context,'$.name',"贺磊",'$.address','beijing')where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from helei; +----+------------------------------------------------------+ | id | context | +----+------------------------------------------------------+ | 1 | {"age": 100, "name": "贺磊", "address": "beijing"} | | 2 | {"age": 30, "name": "陈加持"} | | 3 | {"age": 28, "name": "于浩"} | +----+------------------------------------------------------+ 3 rows in set (0.00 sec) 增加Key-Value mysql> update helei set context=JSON_SET(context,'$.name',"高穷帅")where id=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from helei; +----+---------------------------------------------------------+ | id | context | +----+---------------------------------------------------------+ | 1 | {"age": 100, "name": "高穷帅", "address": "beijing"} | | 2 | {"age": 30, "name": "陈加持"} | | 3 | {"age": 28, "name": "于浩"} | +----+---------------------------------------------------------+ 3 rows in set (0.00 sec) 变更key-value mysql> update helei set context=JSON_REMOVE(context,'$.name') where id=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from helei; +----+------------------------------------+ | id | context | +----+------------------------------------+ | 1 | {"age": 100, "address": "beijing"} | | 2 | {"age": 30, "name": "陈加持"} | | 3 | {"age": 28, "name": "于浩"} | +----+------------------------------------+ 3 rows in set (0.00 sec) 删除Key-Value
JSON格式存储BLOB的测试
Part1:Dynamic Columns处理方式的异同
①MySQL5.7的动态列JSON格式存储
mysql> insert into helei_blob values(1,'{"name":"贺磊","age":100}'),(2,'{"name":"陈加持","age":30}'),(3,'{"name":"于浩","age":28}'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from helei_blob; +----+-------------------------------+ | id | blob_col | +----+-------------------------------+ | 1 | {"name":"贺磊","age":100} | | 2 | {"name":"陈加持","age":30} | | 3 | {"name":"于浩","age":28} | +----+-------------------------------+ 3 rows in set (0.00 sec)
②MariaDB的动态列JSON格式存储
MariaDB [helei]> create table helei (id int(10) unsigned NOT NULL,context json default null,primary key(id)); 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 可以看到MariaDB并不能直接存储JSON类型。 MariaDB [helei]> show create table helei_blob\G; *************************** 1. row *************************** Table: helei_blob Create Table: CREATE TABLE `helei_blob` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `blob_col` blob, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified MariaDB [helei]> insert into helei_blob values(5,column_create('color','blue','size','XL')); Query OK, 1 row affected (0.01 sec) MariaDB [helei]> select * from helei_blob; +----+--------------------------------+ | id | blob_col | +----+--------------------------------+ | 1 | {"name":"贺磊","age":100} | | 2 | {"name":"陈加持","age":30} | | 3 | {"name":"于浩","age":28} | | 5 | 3 sizecolor!XL!blue | +----+--------------------------------+ 4 rows in set (0.00 sec) 直接查询是乱码需用以下函数查询 MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id =5; +----+------------------------------+ | id | column_json(blob_col) | +----+------------------------------+ | 5 | {"size":"XL","color":"blue"} | +----+------------------------------+ 1 row in set (0.00 sec) MariaDB [helei]> select id,column_list(blob_col) from helei_blob where id =5; +----+-----------------------+ | id | column_list(blob_col) | +----+-----------------------+ | 5 | `size`,`color` | +----+-----------------------+ 1 row in set (0.00 sec) 获取全部Key MariaDB [helei]> select id,column_get(blob_col,'color' as char) as color from helei_blob where id =5; +----+-------+ | id | color | +----+-------+ | 5 | blue | +----+-------+ 1 row in set (0.00 sec) 获取Key-Value MariaDB [helei]> update helei_blob set blob_col=column_add(blob_col,'sex','man') where id=5; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id=5; +----+------------------------------------------+ | id | column_json(blob_col) | +----+------------------------------------------+ | 5 | {"sex":"man","size":"XL","color":"blue"} | +----+------------------------------------------+ 1 row in set (0.00 sec) 增加Key-Value MariaDB [helei]> update helei_blob set blob_col=column_add(blob_col,'color','black') where id=5; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id=5; +----+-------------------------------------------+ | id | column_json(blob_col) | +----+-------------------------------------------+ | 5 | {"sex":"man","size":"XL","color":"black"} | +----+-------------------------------------------+ 1 row in set (0.00 sec) 更改Key-Value MariaDB [helei]> update helei_blob set blob_col=column_delete(blob_col,'sex','man') where id=5; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id=5; +----+-------------------------------+ | id | column_json(blob_col) | +----+-------------------------------+ | 5 | {"size":"XL","color":"black"} | +----+-------------------------------+ 1 row in set (0.00 sec) 删除Key-Value
——总结——
虽然MySQL5.7和MariaDB10.0/10.1版本对于JSON的支持是比较完整的,不过MongoDB的Sharding功能更加好用,个人更倾向于MongoDB。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
理解vSphere虚拟交换机中的VLAN类型
VMware vSphere虚拟机交换机支持四种VLAN类型,分别是:无、VLAN、VLAN中继、专用VLAN。 在路由/交换领域,VLAN的中继端口叫做Trunk。Trunk技术用在交换机之间互连,使不同VLAN通过共享链路与其它交换机中的相同VLAN通信。交换机之间互连的端口就称为Trunk端口。Trunk是基于OSI第二层数据链路层(Data Link Layer)的技术。 如果没有VLAN中继,假设两台交换机上分别创建了多个VLAN(VLAN是基于Layer 2的),在两台交换机上相同的VLAN(比如VLAN10)要通信,则需要将交换机A上属于VLAN10的一个端口与交换机B上属于VLAN10的一个端口互连;如果这两台交换机上其它相同VLAN间也需要通信(例如VLAN20、VLAN30),那么就需要在两个交换机之间VLAN20的端口互连,而划分到VLAN30的端口也需要互连,这样不同的交换机之间需要更多的互连线,端口利用率就太低了。 交换机通过trunk功能,事情就简单了,只需要两台交换机之间有一条互连线,将互连线的两个端口设置为trunk模式,这样就可以使交换机上不同VLAN...
- 下一篇
Centos6.5搭建smokeping服务
1.安装所需软件包。 yuminstall-yperlperl-Net-Telnetperl-Net-DNSperl-LDAPperl-libwww-perlperl-IO-Socket-SSLperl-Socket6perl-Time-HiResperl-ExtUtils-MakeMakerrrdtoolrrdtool-perlcurlhttpdhttpd-develgccmakewgetlibxml2-devellibpng-develglibpangopango-develfreetypefreetype-develfontconfigcairocairo-devellibart_lgpllibart_lgpl-develpoptpopt-devellibidnlibidn-devel 2.创建一个放置软件包的常用目录。 mkdir/smokeping/tools–p 3.下载fping软件包,并且解压,编译安装。 wgethttp://fping.org/dist/fping-3.10.tar.gz tar-zxvffping-3.10.tar.gz cdfping-3.10/...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8编译安装MySQL8.0.19
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS关闭SELinux安全模块
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库