0

我正在尝试使用以此处的 python 示例代码为模型的 python 脚本将 CSV 文件加载到 BigQuery:https ://developers.google.com/bigquery/docs/developers_guide

但是当我尝试使用 REST API 加载表时遇到以下错误:

{'status': '200', 'content-length': '1492', 'expires': 'Fri, 01 Jan 1990 00:00:00 GMT', 'server': 'HTTP Upload Server Built on Jun 14 2012 02:12:09 (1339665129)', 'etag': '"tcivyOj9QvKAbuEJ5MEMf9we85w/-mxYhUDjvvydxcebR8fXI6l_5RQ"', 'pragma': 'no-cache', 'cache-control': 'no-cache, no-store, must-revalidate', 'date': 'Fri, 06 Jul 2012 22:30:55 GMT', 'content-type': 'application/json'}

{
 "kind": "bigquery#job",
 "etag": "\"tcivyOj9QvKAbuEJ5MEMf9we85w/-mxYhUDjvvydxcebR8fXI6l_5RQ\"",
 "id": "firespotter.com:firespotter:job_d6b99265278b4c0da9c3033acf39d6b2",
 "selfLink": "https://www.googleapis.com/bigquery/v2/projects/firespotter.com:firespotter/jobs/job_d6b99265278b4c0da9c3033acf39d6b2",
 "jobReference": {
  "projectId": "firespotter.com:firespotter",
  "jobId": "job_d6b99265278b4c0da9c3033acf39d6b2"
 },
 "configuration": {
  "load": {
   "schema": {
    "fields": [
     {
      "name": "date",
      "type": "STRING"
     },
     {
      "name": "time",
      "type": "STRING"
     },
     {
      "name": "call_uuid",
      "type": "STRING"
     },
     {
      "name": "log_level",
      "type": "STRING"
     },
     {
      "name": "file_line",
      "type": "STRING"
     },
     {
      "name": "message",
      "type": "STRING"
     }
    ]
   },
   "destinationTable": {
    "projectId": "385479794093",
    "datasetId": "telephony_logs",
    "tableId": "table_name"
   },
   "createDisposition": "CREATE_IF_NEEDED",
   "writeDisposition": "WRITE_TRUNCATE",
   "encoding": "UTF-8"
  }
 },
 "status": {
  "state": "DONE",
  "errorResult": {
   "reason": "notFound",
   "message": "Not Found: Dataset 385479794093:telephony_logs"
  },
  "errors": [
   {
    "reason": "notFound",
    "message": "Not Found: Dataset 385479794093:telephony_logs"
   }
  ]
 }
}

错误“385479794093”中列出的projectId不是我传入的projectId,是“项目号”。projectId 应该是“firespotter.com:firespotter”:

{
 "kind": "bigquery#datasetList",
 "etag": "\"tcivyOj9QvKAbuEJ5MEMf9we85w/ZMa8z6LKMgWZIqLWh3ti2SsSs4g\"",
 "datasets": [
  {
   "kind": "bigquery#dataset",
   "id": "firespotter.com:firespotter:telephony_logs",
   "datasetReference": {
    "datasetId": "telephony_logs",
    "projectId": "firespotter.com:firespotter"
   }
  }
 ]
}

当我在三个不同的地方传递正确的值时,为什么 REST API 坚持提供自己不正确的 projectId?还有其他地方需要我传入或设置项目ID吗?

作为参考,这里是相关的代码片段:

PROJECT = 'firespotter.com:firespotter'
DATASET = 'telephony_logs'


FLOW = OAuth2WebServerFlow(
    client_id='385479794093.apps.googleusercontent.com',
    client_secret='<a_secret_here>',
    scope='https://www.googleapis.com/auth/bigquery',
    user_agent='firespotter-upload-script/1.0')

def loadTable(http, projectId, datasetId, tableId, file_path, replace=False):
  url = "https://www.googleapis.com/upload/bigquery/v2/projects/" + projectId + "/jobs"
  # Create the body of the request, separated by a boundary of xxx
  mime_data = ('--xxx\n' +
            'Content-Type: application/json; charset=UTF-8\n' + '\n' +
            '{\n' +
            '   "projectId": "' + projectId + '",\n' +
            '   "configuration": {\n' +
            '     "load": {\n' +
            '       "schema": {\n' +
            '         "fields": [\n' +
            '          {"name":"date", "type":"STRING"},\n' +
            '          {"name":"time", "type":"STRING"},\n' +
            '          {"name":"call_uuid", "type":"STRING"},\n' +
            '          {"name":"log_level", "type":"STRING"},\n' +
            '          {"name":"file_line", "type":"STRING"},\n' +
            '          {"name":"message", "type":"STRING"}\n' +
            '        ]\n' +
            '      },\n' +
            '      "destinationTable": {\n' +
            '        "projectId": "' + projectId + '",\n' +
            '        "datasetId": "' + datasetId + '",\n' +
            '        "tableId": "' + tableId + '"\n' +
            '      },\n' +
            '     "createDisposition": "CREATE_IF_NEEDED",\n' +
            '     "writeDisposition": "' + ('WRITE_TRUNCATE' if replace else 'WRITE_APPEND') + '",\n' +
            '     "encoding": "UTF-8"\n' +
            '    }\n' +
            '  }\n' +
            '}\n' +
            '--xxx\n' +
            'Content-Type: application/octet-stream\n' +
            '\n')
  # Append data from the specified file to the request body
  f = open(file_path, 'r')
  header_line = f.readline()  # skip header line
  mime_data += f.read()

  # Signify the end of the body
  mime_data += ('--xxx--\n')

  headers = {'Content-Type': 'multipart/related; boundary=xxx'}
  resp, content = http.request(url, method="POST", body=mime_data, headers=headers)
  print str(resp) + "\n"
  print content

# --- Main ----------------------------------------------
def main(argv):

  csv_path = args[0]

  # If the credentials don't exist or are invalid, run the native client
  # auth flow. The Storage object will ensure that if successful the good
  # credentials will get written back to a file.
  storage = Storage('bigquery2_credentials.dat') # Choose a file name to store the credentials.
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = run(FLOW, storage)

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with our good credentials.
  http = httplib2.Http()
  http = credentials.authorize(http)

  loadTable(http, PROJECT, DATASET, 'table_name', csv_path, replace=True)

if __name__ == '__main__':
  main(sys.argv)
4

2 回答 2

1

Did you recently set the project id to firespotter.com:firespotter? If the dataset was created before the project was named, there will be a mismatch between the old project id and the new. There is an automated system that updates project ids, but it is possible that it hasn't run yet or is having a problem (I'm on vacation right now, so can't check). Hopefully, if you retry again some time soon it will just work. If not, let us know.

于 2012-07-07T23:50:26.440 回答
0

这里有几个问题:

  • 为什么我的加载作业失败了?只是检查一下,这是您发送的整个请求吗?如果是这样,看起来没有要加载的数据,即是空sourceUris的。如果是这样,那就是问题所在,我们显然正在返回世界上最糟糕的错误信息。

  • 为什么是数字项目 ID?BigQuery 交替使用项目名称和相关的数字 ID,因此您所看到的只是我们倾向于在进入的过程中将项目名称转换为 ID。只是为了确认一下,如果您访问Google API 控制台并查找您的项目,您在 url 中看到相同的数字 ID 吗?

  • 为什么在多个地方指定项目 ID?首先,您似乎将项目 ID 指定为作业中的顶级属性;那不应该是必要的。(我怀疑它只是覆盖了您在工作参考本身中指定的任何项目 ID。)这留下了两个位置——一个作为工作参考的一部分,另一个作为表参考的一部分。这些实际上表示两种不同的东西——工作中的一个指定您将工作插入到哪个项目中,即谁为该工作付费,而表中的一个指定结果表所在的项目,即谁拥有结果数据。通常,它们是相同的,但 API 允许它们是不同的。(例如,如果您构建了一个需要将数据插入最终归客户所有的表中的服务,这可能会很有用。)

于 2012-07-07T05:04:22.707 回答