问题描述
今天在用sharding-proxy时,碰到个比较棘手的问题:同事给了一张表,这张表数据量较大,大概1500万数据,要我将它导入分布式数据库。
sharding-proxy虽然可以作为单个mysql看待,但毕竟还不能算是真正的mysql,因此在使用上有一些限制,而今天导入大量数据就是它的一个限制。众所周知的一点是,mysqldump虽是官方产品,但是其只支持单线程,3000条/每分钟的导入速度实在让我不敢恭维,为此我尝试使用mydumper多线程导入,但是在导入时报错提示不认识我的逻辑库名。这让我觉得很尴尬。思考了一下,这是在sharding-proxy使用上的一大痛点,可以暂时先通过拆分文件手动开多线程方式先把数据导入。
split用于把大的文件拆分为一个个小文件。
用法
[root@mysql3 ~]# split --help
Usage: split [OPTION]... [INPUT [PREFIX]]
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default
size is 1000 lines, and default PREFIX is 'x'. With no INPUT, or when INPUT
is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-a, --suffix-length=N generate suffixes of length N (default 2)
--additional-suffix=SUFFIX append an additional SUFFIX to file names
-b, --bytes=SIZE put SIZE bytes per output file
-C, --line-bytes=SIZE put at most SIZE bytes of lines per output file
-d, --numeric-suffixes[=FROM] use numeric suffixes instead of alphabetic;
FROM changes the start value (default 0)
-e, --elide-empty-files do not generate empty output files with '-n'
--filter=COMMAND write to shell COMMAND; file name is $FILE
-l, --lines=NUMBER put NUMBER lines per output file
-n, --number=CHUNKS generate CHUNKS output files; see explanation below
-u, --unbuffered immediately copy input to output with '-n r/...'
--verbose print a diagnostic just before each
output file is opened
--help display this help and exit
--version output version information and exit
SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
CHUNKS may be:
N split into N files based on size of input
K/N output Kth of N to stdout
l/N split into N files without splitting lines
l/K/N output Kth of N to stdout without splitting lines
r/N like 'l' but use round robin distribution
r/K/N likewise but only output Kth of N to stdout
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'split invocation'
常用范例
1.以行数拆分
用行数拆分之前可以通过wc -l
命令查看一下文件的行数:
[root@mysql3 app]# wc -l psn_info_b.sql
3156 psn_info_b.sql
split --lines=300 psn_info_b.sql
300行拆分一个文件。
被拆分后的文件:
-rw-r--r-- 1 root root 312593114 3月 6 14:41 xaa
-rw-r--r-- 1 root root 312614369 3月 6 14:41 xab
-rw-r--r-- 1 root root 312613258 3月 6 14:41 xac
-rw-r--r-- 1 root root 312613953 3月 6 14:41 xad
-rw-r--r-- 1 root root 312615921 3月 6 14:41 xae
-rw-r--r-- 1 root root 312618100 3月 6 14:41 xaf
-rw-r--r-- 1 root root 312611264 3月 6 14:41 xag
-rw-r--r-- 1 root root 312598085 3月 6 14:41 xah
-rw-r--r-- 1 root root 312594193 3月 6 14:41 xai
-rw-r--r-- 1 root root 312603655 3月 6 14:41 xaj
-rw-r--r-- 1 root root 186174347 3月 6 14:42 xak
2.以文件大小拆分
split -b 2M 07996bb7-6f5f-49b3-a3c9-c706bd545468_100_218_pcloud_web_2019_12_17.sql
[root@mysql3 sql]# ll
total 88140
-rw-r--r-- 1 app app 35650743 Dec 17 12:34 07996bb7-6f5f-49b3-a3c9-c706bd545468_100_218_pcloud_web_2019_12_17.sql
-rw-r--r-- 1 app app 18948817 Dec 17 12:40 07996bb7-6f5f-49b3-a3c9-c706bd545468_20_29_venus_hygeia_2019_12_17.sql
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xaa
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xab
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xac
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xad
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xae
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xaf
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xag
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xah
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xai
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xaj
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xak
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xal
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xam
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xan
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xao
-rw-r--r-- 1 root root 2097152 Mar 6 16:13 xap
-rw-r--r-- 1 root root 2096311 Mar 6 16:13 xaq
如果使用-d
,则拆分文件名末尾为数字,加-a
是定义末尾数字的长度,默认为2:
split -b 2M -d -a 4 07996bb7-6f5f-49b3-a3c9-c706bd545468_100_218_pcloud_web_2019_12_17.sql
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0000
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0001
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0002
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0003
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0004
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0005
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0006
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0007
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0008
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0009
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0010
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0011
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0012
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0013
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0014
-rw-r--r-- 1 root root 2097152 Mar 6 16:18 x0015
-rw-r--r-- 1 root root 2096311 Mar 6 16:18 x0016