如何在 Node.js 中使用 Cloud Functions 在 CloudSQL 中加载 CSV 数据?
How to load CSV data in CloudSQL using Cloud Functions in Node.js?
我正在接收特定存储桶中 CSV 文件中的数据。我必须将该数据加载到相应的 CloudSQL table 中。我对此有以下疑问:
- 我是否可以选择在我的云函数代码中调用任何 api 或 gcloud 命令或 CURL 命令,将 CSV 作为输入并在数据库中执行插入?
- 如果上述选项不可行,那么是否有任何性能优化的方法可用于准备查询并在 CloudSQL 中触发这些查询,因为数据会很大,我想加载这些数据会花费很多时间。
documentation on importing a CSV into CloudSQL 包含此 curl
调用示例:
ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{"importContext":
{"fileType": "CSV",
"uri": "gs://<BUCKET_NAME>/<PATH_TO_DUMP_FILE>",
"database": "<DATABASE_NAME>",
"csvImportOptions":
{"table": "<TABLE_NAME>"}}}' \
-X POST \
https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/import
需要注意的一些重要事项:
curl
只是一种从命令行执行 HTTP 调用的方法。在 Cloud Functions 中,您将从代码执行等效的 HTTP 调用,例如 Node.js. 中的 fetch()
- 要导入的 CSV 文件必须在 Google 云存储桶中。
- Cloud Functions run a Google environment under the so-called application default credentials, so the
ACCESS_TOKEN
might not be needed. If they are needed after all, have a look here: https://www.npmjs.com/package/google-auth-library#application-default-credentials
我正在接收特定存储桶中 CSV 文件中的数据。我必须将该数据加载到相应的 CloudSQL table 中。我对此有以下疑问:
- 我是否可以选择在我的云函数代码中调用任何 api 或 gcloud 命令或 CURL 命令,将 CSV 作为输入并在数据库中执行插入?
- 如果上述选项不可行,那么是否有任何性能优化的方法可用于准备查询并在 CloudSQL 中触发这些查询,因为数据会很大,我想加载这些数据会花费很多时间。
documentation on importing a CSV into CloudSQL 包含此 curl
调用示例:
ACCESS_TOKEN="$(gcloud auth application-default print-access-token)" curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header 'Content-Type: application/json' \ --data '{"importContext": {"fileType": "CSV", "uri": "gs://<BUCKET_NAME>/<PATH_TO_DUMP_FILE>", "database": "<DATABASE_NAME>", "csvImportOptions": {"table": "<TABLE_NAME>"}}}' \ -X POST \ https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/import
需要注意的一些重要事项:
curl
只是一种从命令行执行 HTTP 调用的方法。在 Cloud Functions 中,您将从代码执行等效的 HTTP 调用,例如 Node.js. 中的 - 要导入的 CSV 文件必须在 Google 云存储桶中。
- Cloud Functions run a Google environment under the so-called application default credentials, so the
ACCESS_TOKEN
might not be needed. If they are needed after all, have a look here: https://www.npmjs.com/package/google-auth-library#application-default-credentials
fetch()