从 Google 驱动器读取文件时,BigQuery 外部 table 创建失败,架构为 "autodetect"

BigQuery external table creation failed with "autodetect" schema while reading a file from Google Drive

我正在尝试通过从 Google Drive 读取文件来创建外部 BigQuery table - 它适用于内联方案,但因 autodetect 标志而失败。

引用的文档:
https://cloud.google.com/bigquery/external-data-drive

架构文件:

$ bq mkdef --autodetect --source_format=CSV "https://drive.google.com/open?id=<file-id>" > schema.json

schema.json:

{
  "autodetect": true,
  "csvOptions": {
    "encoding": "UTF-8",
    "quote": "\""
  },
  "sourceFormat": "CSV",
  "sourceUris": [
    "https://drive.google.com/open?id=<file-id>"
  ]
}

外部 Table:

$ bq mk --external_table_definition=schema.json mydataset.mytable
BigQuery error in mk operation: Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.

有效,内联模式:

$ bq mk --external_table_definition=col1:INTEGER,col2:STRING@CSV=https://drive.google.com/open?id=<file-id> mydataset.mytable
Table 'myproject:mydataset.mytable' successfully created.

Note: I have enabled Google Drive access by using gcloud auth login --enable-gdrive-access

显然,罪魁祸首是 "autodetect": true parameter , being specified in table definition file --external_table_definition when creating Bigquery external table 来自驻留在 Google 驱动器中的源数据。

实际上 bq command-line 工具是一个 Python 脚本,它与 Biqquery REST API, that means we trigger tables.insert API method to create a permanent external table, supplying appropriate ExternalDataConfiguration in Table json 请求主体交互。

您可以检查它在整个 API Explorer 期间执行对 Bigquery API 的相关 API 调用,使用来自 ExternalDataConfiguration 的 table 定义参数:

curl --request POST \
  'https://bigquery.googleapis.com/bigquery/v2/projects/<projectid>/datasets/<datasetid>/tables?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"tableReference":{"datasetId":"datasetId","projectId":"projectId","tableId":"tableId"},"externalDataConfiguration":{"autodetect":true,"csvOptions":{"encoding":"UTF-8","quote":"\""},"sourceFormat":"CSV","sourceUris":["https://drive.google.com/open?id=<file-id>"]}}' \
  --compressed

我在响应消息中收到了同样的错误:

 "error": {
    "code": 403,
    "message": "Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.",
    "errors": [
      {
        "message": "Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.",
        "domain": "global",
        "reason": "accessDenied"
      }
    ],
    "status": "PERMISSION_DENIED"
  }

现在,您可以提供内联模式(在命令行上),或者您可以提供一个包含模式定义的 JSON 文件来让事情正常进行。

为了让开发人员更清楚地看到此问题的证据,我鼓励您通过 Public Issue tracker, thus we can keep track of any updates occurred or try to reach out Google support 提交错误报告。

这个问题实际上似乎与身份验证有关。就其价值而言,gcloud 使用与 bq.

不同的 OAuth 令牌

我认为此时最好的行动方案是:

  1. 查找$HOME/.bigqueryrc,有credential_file = 行,
  2. 删除上一步中引用的 credential_file(在 Linux/macOS 上可能类似于 .config/gcloud/...),
  3. 运行 gcloud auth --enable-gdrive-access --force,OAuth window 也应该要求您获得使用 GDrive 的许可,
  4. 重试创建外部 table 定义。

如果它仍然不起作用,您可以通过预览 credential_file 中引用的文件来查找您的令牌使用的范围。这是一个简单的 JSON 文件,范围只是一个 URI 列表,应该有 drivedrive.read.