MySQL large table split into CSV export

Source: Internet
Author: User

Recently the company has a tens of millions of-row large table that needs to be split into different CSV files according to the city's ID field.

Wrote an automated shell script

Underneath the/HOME/HDH.

LINUX-XUD0:/HOME/HDH # LL
Total 16
-rwxrwxrwx 1 root root 902 Dec 07:47 cf.sh
-RWXRWXRWX 1 root root 6 Dec 07:47 id_1.txt
-RWXRWXRWX 1 root root 6 Dec 20:00 log

Vim cf.sh

#!/bin/bash
date=$ (date + '%y%m%d ')
Cd/home/hdh

Rm-r/home/hdh/file #删除上次执行shell generated file folder
Mkdir/home/hdh/file #创建一个file文件夹用于存放生成的csv File
chmod 777/home/hdh/file-r

echo ' MYSQL-UROOT-PROOT-E ' select DISTINCT ID from xizang.test; "|grep-evi" id "' >/home/hdh/id_1.txt #把该表的id field The value is saved to/home/hdh/id_1.txt.
#cat Id_1.txt |while Read line
For line in $ (<id_1.txt);
Do
# echo ${line} >> id_2.txt
# echo ' mysql-uroot-proot-e ' select DISTINCT ID from xizang.test where id= ' ${line} '; " ' >>id_2.txt
# MYSQL-UROOT-PROOT-E "Select DISTINCT ID from xizang.test where id= ' ${line} ' to outfile '/home/hdh/c.csv ' fields TER minated by ', ' enclosed by ' "' LINES TERMINATED by ' \ r \ n ';"
Mysql-uroot-proot <<eof
SELECT * from xizang.test where id= ' ${line} ' to outfile '/home/hdh/file/${line}_c.csv ' fields TERMINATED by ', ' enclosed   By ' "' LINES TERMINATED by ' \ r \ n '; #以id is the where condition, generated into the corresponding CSV.
Exit
Eof
Done

MySQL large table split into CSV export

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.