mysql 大表拆分成csv匯出,mysql拆分csv匯出

來源:互聯網
上載者:User

mysql 大表拆分成csv匯出,mysql拆分csv匯出

最近公司有一個幾千萬行的大表需要按照城市的id欄位拆分成不同的csv檔案。

寫了一個自動化的shell指令碼

在/home/hdh 下面

linux-xud0:/home/hdh # ll
total 16
-rwxrwxrwx 1 root root 902 Dec 28 07:47 cf.sh
-rwxrwxrwx 1 root root 6 Dec 28 07:47 id_1.txt
-rwxrwxrwx 1 root root 6 Dec 27 20:00 log

vim  cf.sh

#!/bin/bash
DATE=$(date +'%Y%m%d')
cd /home/hdh

rm -r /home/hdh/file      #刪除上次執行shell 產生的file 檔案夾
mkdir /home/hdh/file     #建立一個file檔案夾用於存放產生的csv 檔案
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 欄位的值拿到存到  /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}' into outfile '/home/hdh/c.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' ;"
mysql -uroot -proot <<EOF
SELECT * FROM xizang.test where id='${line}' into outfile '/home/hdh/file/${line}_c.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' ;   #以id 為where  的條件,產生到對應的csv 中。
exit
EOF
done

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.