使用mysql shell 从mongodb导数据到mysql

导出mongodb 数据。

#mongoexport -u admin -p='ooxx' --authenticationDatabase admin -d res -c reseinfo -f _id,resid,atid,atinfo,baseinfo  --limit 10000  -o /tmp/1.json

(_id,resid,atid,atinfo,baseinfo为指定的字段)

安装mysql shell

rpm –ivh mysql-shell-8.0.24-1.el7.x86_64.rpm

安装x插件(mysql8.0默认已安装)

#mysqlsh -u user -h localhost --classic --dba enableXProtocol

或者

mysql> INSTALL PLUGIN mysqlx SONAME 'mysqlx.so';

启动mysql shell 建立 X 会话

#mysqlsh root@localhost:33060

MySQL  localhost:33060+ ssl  test SQL >\py

MySQL  localhost:33060+ ssl  test py >util.import_json("/tmp/1.json",{"schema": "test", "table": "tjson1","convertBsonOid": True})

即导入成功。33060是X 插件的侦听端口

扩展测试:通过虚拟列来优化mysql json数据类型的字段的检索

添加虚拟列

mysql>alter table tjson1 add atid bigint generated always as (doc->'$.atid');

查看表结构

mysql>desc tjson1;

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

| Field    | Type      | Null | Key | Default | Extra            |

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

| doc      | json      | YES  |    | NULL    |                  |

| id        | int(11)    | NO  | PRI | NULL    | auto_increment    |

| atid | bigint(20) | YES  |    | NULL    | VIRTUAL GENERATED |

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

给虚拟列添加索引

mysql>alter table tjson1 add index idx_atid(atid);

直接在json列中检索数据

mysql>explain SELECT * FROM tjson1 WHERE JSON_EXTRACT(doc,'$.atid')='155745128';

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

| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra      |

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

|  1 | SIMPLE      | tjson1 | NULL      | ALL  | NULL          | NULL | NULL    | NULL | 241834 |  100.00 | Using where |

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

通过虚拟列检索数据

> explain SELECT * FROM tjson1 WHERE atid=155745460;

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

| id | select_type | table  | partitions | type | possible_keys | key          | key_len | ref  | rows | filtered | Extra |

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

|  1 | SIMPLE      | tjson1 | NULL      | ref  | idx_atid | idx_atid | 9      | const |    2 |  100.00 | NULL  |

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

两者的执行计划差异明显

你可能感兴趣的:(使用mysql shell 从mongodb导数据到mysql)