通过 .csv 中的 MySql 将大量数据行插入云 sql table
Inserting large number of data rows into cloudsql table via PyMySql from .csv
我是 Cloud 的新手SQL,我正在尝试插入 40 多个不同列和超过 150 万行的记录。但是我无法在 Google CloudSQL 中执行此操作。我已经采取了下面列出的许多措施来解决这个问题,但我得到的主要错误是:
ERROR:
textPayload: "2019-04-12T06:10:47.348295Z 8554 [Note] Aborted connection 8554 to db: 'xxxxx_xxx' user: 'root' host: 'x.x.x.x' (Got an error reading communication packets)"
总结:
- 我正在使用 Python、
PyMySql
将 150 万行数据插入 35 列的 table
- 实例、数据库、Table 已在云中创建SQL。
- 系统配置:vCPU - 4,内存 - 15 GB,SSD 存储 - 10 GB
- 我可以在我的本地系统中完全正常地加载这些数据。
- 在GoogleCloudSQl中,部署时间特别长,部署成功。
- 但是当我查看我的 table 时,它是空的。
- 实例中的
MySql
错误日志显示以上内容。
我已经尝试了以下操作:
使用 API URL
/ .txt
/ .json
文件上传,现在使用 .csv
.
以为是系统问题,把系统从8GB内存升级到15GB内存
考虑到 SQL 默认配置造成了限制,我添加了以下内容:
sql_mode : MAXDB,NO_AUTO_CREATE_USER
max_allowed_packet: 1073741824
net_read_timeout: 4294967295
wait_timeout: 31536000
插入的行数较少,可插入的最大行数 = 100
def adddata():
try:
conn = pymysql.connect(unix_socket='/cloudsql/' + 'karto-235001:asia-east1:karto', user='xxx', password='xxx', db='xxx')
cur = conn.cursor()
insert_ = "INSERT INTO data_table(a, b, c) VALUES (%s, %s, %s)"
with open('info.csv', newline='') as myFile:
reader = csv.reader(myFile)
for item in reader:
cur.execute(insert_, (item[3], item[4], item[5]))
conn.commit()
cur.close()
finally:
conn.close()
我已在线查看并实施了 CloudSQL 和其他 stack-overflow 用户推荐的解决方案。如果有人能确定我做错了什么,或者我的代码或配置是否有问题?非常感谢。
我看到您想使用 Python 上传包含在 CSV 文件中的信息。您是否尝试过直接导入数据库?您可以按照 link[1].
中的步骤操作
与此同时,我会尝试复制您的案例。您可能还需要检查您的安装和配置是否正确。
验证您的云 SQL 实例和连接 [2] 以及您是否 Python 安装[3]。
[1]https://cloud.google.com/sql/docs/mysql/import-export/importing#csv
[2]https://cloud.google.com/sql/docs/mysql/connect-compute-engine
[3]https://cloud.google.com/python/setup
我是 Cloud 的新手SQL,我正在尝试插入 40 多个不同列和超过 150 万行的记录。但是我无法在 Google CloudSQL 中执行此操作。我已经采取了下面列出的许多措施来解决这个问题,但我得到的主要错误是:
ERROR:
textPayload: "2019-04-12T06:10:47.348295Z 8554 [Note] Aborted connection 8554 to db: 'xxxxx_xxx' user: 'root' host: 'x.x.x.x' (Got an error reading communication packets)"
总结:
- 我正在使用 Python、
PyMySql
将 150 万行数据插入 35 列的 table - 实例、数据库、Table 已在云中创建SQL。
- 系统配置:vCPU - 4,内存 - 15 GB,SSD 存储 - 10 GB
- 我可以在我的本地系统中完全正常地加载这些数据。
- 在GoogleCloudSQl中,部署时间特别长,部署成功。
- 但是当我查看我的 table 时,它是空的。
- 实例中的
MySql
错误日志显示以上内容。
我已经尝试了以下操作:
使用
API URL
/.txt
/.json
文件上传,现在使用.csv
.以为是系统问题,把系统从8GB内存升级到15GB内存
考虑到 SQL 默认配置造成了限制,我添加了以下内容:
sql_mode : MAXDB,NO_AUTO_CREATE_USER
max_allowed_packet: 1073741824
net_read_timeout: 4294967295
wait_timeout: 31536000
插入的行数较少,可插入的最大行数 = 100
def adddata():
try:
conn = pymysql.connect(unix_socket='/cloudsql/' + 'karto-235001:asia-east1:karto', user='xxx', password='xxx', db='xxx')
cur = conn.cursor()
insert_ = "INSERT INTO data_table(a, b, c) VALUES (%s, %s, %s)"
with open('info.csv', newline='') as myFile:
reader = csv.reader(myFile)
for item in reader:
cur.execute(insert_, (item[3], item[4], item[5]))
conn.commit()
cur.close()
finally:
conn.close()
我已在线查看并实施了 CloudSQL 和其他 stack-overflow 用户推荐的解决方案。如果有人能确定我做错了什么,或者我的代码或配置是否有问题?非常感谢。
我看到您想使用 Python 上传包含在 CSV 文件中的信息。您是否尝试过直接导入数据库?您可以按照 link[1].
中的步骤操作与此同时,我会尝试复制您的案例。您可能还需要检查您的安装和配置是否正确。
验证您的云 SQL 实例和连接 [2] 以及您是否 Python 安装[3]。
[1]https://cloud.google.com/sql/docs/mysql/import-export/importing#csv
[2]https://cloud.google.com/sql/docs/mysql/connect-compute-engine
[3]https://cloud.google.com/python/setup