使用Google BigQuery客户端API在BigQuery中加载JSON文件

6
有没有一种方法可以使用Google BigQuery客户端API从本地文件系统加载JSON文件到BigQuery中?
我找到的所有选项包括:
1- 逐个流式传输记录。
2- 从GCS加载JSON数据。
3- 使用原始POST请求加载JSON(即不通过Google Client API)。
1个回答

3
我假设您想使用Python完成此操作。这里有一个示例链接,可以从本地文件加载数据(它使用CSV格式,但很容易改为JSON...同一目录下还有另一个JSON示例)。
基本流程如下:
# Load configuration with the destination specified.
load_config = {
  'destinationTable': {
    'projectId': PROJECT_ID,
    'datasetId': DATASET_ID,
    'tableId': TABLE_ID
  }
}

load_config['schema'] = {
  'fields': [
    {'name':'string_f', 'type':'STRING'},
    {'name':'boolean_f', 'type':'BOOLEAN'},
    {'name':'integer_f', 'type':'INTEGER'},
    {'name':'float_f', 'type':'FLOAT'},
    {'name':'timestamp_f', 'type':'TIMESTAMP'}
  ]
}
load_config['sourceFormat'] = 'NEWLINE_DELIMITED_JSON'

# This tells it to perform a resumable upload of a local file
# called 'foo.json' 
upload = MediaFileUpload('foo.json',
                         mimetype='application/octet-stream',
                         # This enables resumable uploads.
                         resumable=True)

start = time.time()
job_id = 'job_%d' % start
# Create the job.
result = jobs.insert(
  projectId=project_id,
  body={
    'jobReference': {
      'jobId': job_id
    },
    'configuration': {
      'load': load
    }
  },
  media_body=upload).execute()

 # Then you'd also want to wait for the result and check the status. (check out
 # the example at the link for more info).

谢谢,这个方法可行!我在文档中漏掉了 media_body 参数。它在所有 JSON 配置选项之后,相当靠下 :) - iBrAaAa
如果您有JSON对象(100000个JSON),它将被导入到内存中,如果我不使用流式传输,上传数据的最佳选项是什么?从GCS加载JSON数据还是使用原始POST请求? - RCalaf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接