BigQuery:从CSV加载,跳过列

2024-04-29 14:37:14 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个包含现有数据的表,其模式如下:

{ 'name' : 'Field1', 'type' : 'STRING' },
{ 'name' : 'Field2', 'type' : 'STRING' }

我们的数据是CSV:

^{pr2}$

我们通过创建一个新作业来加载数据,直接从Google云存储(GCS)加载CSV。我们的数据文件现在有一个额外的列和不同的顺序,因此数据现在是结构化的:

Field1,Field3,Field2
Value1,Value3,Value2
...

有没有一种方法可以在加载作业中指定跳过第二列,只加载列1和列3(名为Field1和Field2)?在

我正在使用Python API,例如。,服务.工作().插入(作业体)

基本上我想这样做:

job_body = {
  'projectId': projectId,
  'configuration': {
      'load': {
        'sourceUris': [sourceCSV],
        'schema': {
          'fields': [
            {
              'name': 'Field1',
              'type': 'STRING'
            },
            { # this would be the skipped field
              'name': None
              'skip': True
            },
            {
              'name': 'Field2',
              'type': 'String'
            },
          ]
        },
        'destinationTable': {
          'projectId': projectId,
          'datasetId': datasetId,
          'tableId': targetTableId
        },
      }
    }
  }

谢谢!在


Tags: csv数据namestring数据文件typegoogle作业
2条回答

目前还不可能做到这一点,但这可能是一个有趣的特性请求。请随意将其添加到https://code.google.com/p/google-bigquery/issues/list。在

同时,我将执行两步导入:

  1. 导入为具有3列的新表。在
  2. 将“SELECT column1,column2 FROM[newtable]”追加到现有表中。在

菲利佩的建议应该行得通。另一种可能性是,如果您能够修改要加载到BigQuery中的CSV,则在加载作业上使用ignoreUnknownValues标志:

[Optional] Accept rows that contain values that do not match the schema. The unknown values are ignored. Default is false which treats unknown values as errors. For CSV this ignores extra values at the end of a line. For JSON this ignores named values that do not match any column name.

但是,使用此标志将需要重新排序CSV中的列或将数据格式化为JSON。在

相关问题 更多 >