gpt4 book ai didi

mysql - 从 mysql RDS 导出数据以导入到 questDb

转载 作者:行者123 更新时间:2023-12-05 02:25:55 25 4
gpt4 key购买 nike

我在 mysql RDS 中有一个大表(约 5 亿行),我需要将特定列从它导出到 csv,以便导入到 questDb。

通常我会使用 into outfile 但 RDS 不支持此操作,因为无法访问文件系统。

我尝试使用工作台进行导出,但由于表的大小,我不断遇到内存不足的问题。

最佳答案

终于在这个帮助下弄明白了:Exporting a table from Amazon RDS into a CSV file

只要您有某种顺序列(例如,自动递增整数 PK 或日期列。如果您有大量数据,请确保将日期列编入索引!

#!bin/bash

# Maximum number of rows to export/total rows in table, set a bit higher if live data being written
MAX=500000000
# Size of each export batch
STEP=1000000

mkdir -p parts

for (( c=0; c<= $MAX; c = c + $STEP ))
do
mysql --port 3306 --protocol=TCP -h <rdshostname> -u <username> -p<password> --quick --database=<db> -e "select column1, column2, column3 <table> order by <timestamp> ASC limit $STEP offset $c" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > export$c.csv

# split down in to chunks under questdbs 65k line limit
split -d -l 64999 --additional-suffix=.csv $FILE_NAME.csv ./parts/$FILE_NAME

done

# print out import statements to a file
for i in $(ls -v ./parts); do echo "COPY reading from '$i';" >> import.sql; done;

一种略有不同的方法可能会更快,具体取决于您现有的索引,它是逐月逐步处理数据:

#!bin/bash
START_YEAR=2020
END_YEAR=2022

mkdir -p parts

for (( YEAR=$START_YEAR; YEAR<=$END_YEAR; YEAR++ ))
do
for (( MONTH=1; MONTH<=12; MONTH++ ))
do

NEXT_MONTH=1
let NEXT_YEAR=$YEAR+1
if [ $MONTH -lt 12 ]
then
let NEXT_MONTH=$MONTH+1
NEXT_YEAR=$YEAR
fi

FILE_NAME="export-$YEAR-$MONTH-to-$NEXT_YEAR-$NEXT_MONTH"

mysql --port 3306 --protocol=TCP -h <rdshost> -u app -p<password> --quick --database=<database> -e "select <column1>, <column2>, round(UNIX_TIMESTAMP(<dateColumn>)) * 1000000 as date from <table> where <table>.<dateColumn> >= '$YEAR-$MONTH-01 00:00:00' and table.<dateColumn> < '$NEXT_YEAR-$NEXT_MONTH-01 00:00:00' order by <table>.<dateColumn> ASC" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > $FILE_NAME.csv

# split down in to chunks under questdbs 65k line limit
split -d -l 64999 --additional-suffix=.csv $FILE_NAME.csv ./parts/$FILE_NAME
done
done

# print out import statements to a file
for i in $(ls -v ./parts); do echo "COPY reading from '$i';" >> import.sql; done;

上述脚本将输出一个import.sql,其中包含导入数据所需的所有sql 语句。请参阅:https://questdb.io/docs/guides/importing-data/

关于mysql - 从 mysql RDS 导出数据以导入到 questDb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74179359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com