【发布时间】:2021-10-02 23:32:21
【问题描述】:
更新:
我在云函数上测试了这个函数,得到了这个错误Error: function terminated。当我查看日志时:in open_connection raise errors.InterfaceError( mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '111.111.111:3306' (110 Connection timed out)
我正在尝试创建一个从 mysql 中提取数据并将其加载到大查询中的函数,但该函数不适用于大查询。这是我的代码。
import mysql.connector
from google.cloud import bigquery
import pandas as pd
def test(request):
client = bigquery.Client()
mydb = mysql.connector.connect(
host='host',
user='user',
password='password',
database="db")
mycursor = mydb.cursor()
mycursor.execute("my_query")
myresult = mycursor.fetchall()
df=pd.DataFrame(myresult,columns=mycursor.column_names)
dataset_ref = client.dataset('dataset_name')
job_config = bigquery.LoadJobConfig()
job_config.autodetect = True
job_config.write_disposition = "WRITE_TRUNCATE"
load_job = client.load_table_from_dataframe(
df, 'datasetname.tablename', job_config=job_config
)
print("Starting job {}".format(load_job))
return ("Done!", 200)
这里是 requirements.txt:
mysql-connector==2.2.9
google-cloud-bigquery==2.3.1
google-cloud-storage==1.32.0
pandas==1.3.1
当我在没有请求参数的情况下使用 spyder 运行该函数时,它工作正常。
【问题讨论】:
-
“不工作”是什么意思?
-
更新问题。我解决了我提到的问题,但是当我在云函数上测试这个函数时得到这个错误错误:
function terminated。当我查看日志时:in open_connection raise errors.InterfaceError( mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '111.111.111:3306' (110 Connection timed out) -
您的 Cloud SQL 实例上的授权网络是什么?
-
很遗憾,我没有访问此页面的权限。你对下面的答案有什么看法?顺便感谢您的反馈。
-
您需要提供更多关于您的云函数和云sql实例的信息。您使用 Cloud SQL 的公共 IP 还是私有 IP?如果是私有的,您是否使用无服务器 VPC 连接器?云 SQL 实例是在同一个项目中还是在另一个项目中?
标签: python mysql google-bigquery google-cloud-functions