MySQL 8.0 shell util 特性

作者:杨涛涛
背景本文介绍 MySQL 8.0 shell 子模块 Util 的两个导入特性 importTable/import_table(JS和Python 版本的命名差异)、importJson/import_json的使用方法 。其中 import_table 是通过传统 MySQL 协议来通信,Import_json 是通过 X 插件协议来通信 。
MySQL 一直以来提供导入文件 SQL 命令 load data infile(单线程)以及对应的可执行文件 mysqlimport(多线程) 。
比如我导入 100W 行示例数据到表 ytt.tl1,花了 24 秒 。这个已经是 MySQL 默认导入来的最快的 。
[root@mysql-dev ytt]# time mysqlimport --login-path=ytt_master --fields-terminated-by=, --use-threads=4 ytt /var/lib/mysql-files/tl1.csvytt.tl1: Records: 1048576 Deleted: 0 Skipped: 0 Warnings: 0real 0m24.815suser 0m0.013ssys 0m0.031s分析那我们现在看下 mysqlimport 工具的升级版,mysqlshell 的 util 工具集 。使用这两个工具之前,必须得临时开启 local_infile 选项 。
1. import_table
[root@mysql-dev ytt]# mysqlshMySQL Shell 8.0.17Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates.Other names may be trademarks of their respective owners.Type 'help' or '?' for help; 'quit' to exit.建立 3306 端口的新连接
MySQL JS > c admin@127.0.0.1:3306Creating a session to 'admin@127.0.0.1:3306'Fetching schema names for autocompletion... Press ^C to stop.Your MySQL connection id is 56Server version: 8.0.17 MySQL Community Server - GPLNo default schema selected; type \use <schema>to set one.我这里切换为 python 模式
MySQL 127.0.0.1:3306 ssl JS > pySwitching to Python mode...MySQL 127.0.0.1:3306 ssl Py > \use yttDefault schema set to `ytt`.清空掉示例表 Ytt.tl1
MySQL 127.0.0.1:3306 ssl ytt Py > sql truncate tl1;Fetching table and column names from `ytt` for auto-completion... Press ^C to stop.Query OK, 0 rows affected (0.2354 sec)示例表表结构
MySQL 127.0.0.1:3306 ssl ytt Py > sql show create table tl1G*************************** 1. row *************************** Table: tl1Create Table: CREATE TABLE `tl1` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `r1` int(11) DEFAULT NULL, `r2` int(11) DEFAULT NULL, `r3` varchar(30) COLLATE utf8mb4_general_ci DEFAULT NULL, `r4` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci1 row in set (0.0011 sec)import_table 有两个参数,第一个参数定义导入文件的路径,第二个定义相关选项,比如导入的格式,并发的数量等 。
定义文件路径(参数1)
MySQL 127.0.0.1:3306 ssl ytt Py > y_file1='/var/lib/mysql-files/tl1.csv'定义选项(参数2)
MySQL 127.0.0.1:3306 ssl ytt Py > y_options1={"schema":"ytt","table":"tl1","fieldsTerminatedBy":",","showProgress":True,"threads":4}执行导入:
MySQL 127.0.0.1:3306 ssl ytt Py > util.import_table(y_file1,y_options1);Importing from file '/var/lib/mysql-files/tl1.csv' to table `ytt`.`tl1` in MySQL Server at 127.0.0.1:3306 using 1 thread[Worker000] ytt.tl1: Records: 1048576 Deleted: 0 Skipped: 0 Warnings: 0100% (40.87 MB / 40.87 MB), 2.14 MB/sFile '/var/lib/mysql-files/tl1.csv' (40.87 MB) was imported in 16.7394 sec at 2.44 MB/sTotal rows affected in ytt.tl1: Records: 1048576 Deleted: 0 Skipped: 0 Warnings: 0只花了不到 17 秒,比传统 mysqlimport 快了不少 。
我们上面指定了显式指定了字段分隔符,那有没有已经定义好的组合格式呢? 答案是有的,选项 dialect 可以指定以下格式:csv,tsv,json,csv-unix
那么上面的导入,我们可以更简单,
【MySQL 8.0 shell util 特性】改下变量 y_options1 的定义
MySQL 127.0.0.1:3306 ssl ytt Py > sql truncate tl1;Query OK, 0 rows affected (0.0776 sec)MySQL 127.0.0.1:3306 ssl ytt Py > y_options1={"schema":"ytt","table":"tl1","dialect":"csv-unix","showProgress":True,"threads":4}MySQL 127.0.0.1:3306 ssl ytt Py > util.import_table(y_file1,y_options1);Importing from file '/var/lib/mysql-files/tl1.csv' to table `ytt`.`tl1` in MySQL Server at 127.0.0.1:3306 using 1 thread[Worker000] ytt.tl1: Records: 1048576 Deleted: 0 Skipped: 0 Warnings: 0100% (40.87 MB / 40.87 MB), 2.67 MB/sFile '/var/lib/mysql-files/tl1.csv' (40.87 MB) was imported in 14.1000 sec at 2.90 MB/sTotal rows affected in ytt.tl1: Records: 1048576 Deleted: 0 Skipped: 0 Warnings: 0导入时间差不多 。这里要说明下,dialect 选项的优先级比较低,比如添加了'linesTerminatedBy':'rn',则覆盖他自己的'n' 。
选项 diaelect 还有一个可选值为 json,可以直接把 json 结果导入到文档表里 。比如我新建一张表 tl1_json


推荐阅读