在插入流时更新表模式

时间:2016-08-22 18:45:24

标签: google-bigquery

我有一个表不断接收流媒体插入(可能每秒数千)。

我对使用Update功能(通过API调用)添加列感兴趣。我是否可以调用Update将列添加到现有表中,同时仍在插入数据,而不用担心数据丢失?

作为参考,这里是我计划用于向表中添加列的代码:

func addColumnToTable(service *bigquery.Service, project, dataset, table string, newCols map[string]string) bool {
    resp, err := service.Tables.Get(project, dataset, table).Do()
    if err != nil {
        fmt.Println(err)
        return false
    }

    for col, col_type := range newCols {
        this_col := &bigquery.TableFieldSchema{}
        this_col.Name = col
        this_col.Type = col_type
        resp.Schema.Fields = append(resp.Schema.Fields, this_col)
    }

    _, err = service.Tables.Update(project, dataset, table, resp).Do()
    if err != nil {
        fmt.Println(err)
        return false
    }

    return true
}

1 个答案:

答案 0 :(得分:1)

您可以使用tables.update更新表的架构(唯一允许的更新是添加列并放宽列的必要性)。

稍微会延迟架构更新对流式传输的可见性。因此,在流式传输包含新列的数据之前,您需要等待少量时间(最多5分钟)(假设您的更新是添加了一列)。旧数据不会丢失。