【问题标题】:How to update datatype in Big query table?如何更新 Bigquery 表中的数据类型?
【发布时间】:2017-10-02 14:15:54
【问题描述】:

我刚刚在 Big Query 中开始了我的 POC。我想知道如何更新/修改现有表中的列定义/模式。

bq mk -t market.cust custid:integer,grp:integer,odate:string bq update -t​​ market.cust custid:string,grp:integer,odate:string

**Error:
spanda2040@instance-3:~/data$ bq update -t market.cust custid:string,grp:integer,odate:string
BigQuery error in update operation: Provided Schema does not match Table arboreal-height-175822:market.cust. Field custid has changed type from INTEGER to STRING**

表架构:

最后修改的架构 Total Rows Total Bytes Expiration Time 分区标签


02 Oct 13:38:29 |- custid: 整数 0 0 |- grp: 整数 |- 日期:字符串

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    使用 SELECT 会产生一些成本,因为您需要扫描整个表

    下面是超级简单的方法,只需 0.00 美元

    1. 将现有表导出到 GCS 中
    2. 从 GCS 加载到与原始表具有相同架构的新表,但该列是固定类型的除外
    3. 你完成了。 0元的费用!!

    我这样做只是为了进行健全性检查,它可以作为一种魅力

    当您对这种方法感到满意时 - 您甚至可以加载到同一个表中 - 将 WRITE_TRUNCATE 用于 writeDisposition 属性

    【讨论】:

    • 这是一种方法。但是cloud.google.com/bigquery/docs/tables#append-overwrite 说发出 bq update 命令并提供架构定义:bq update [DATASET].[TABLE] [SCHEMA]。我想知道为什么它不起作用。
    • 您可以通过向现有表添加新字段来更新其架构。新字段必须为 NULLABLE 或 REPEATED。您不能将 REQUIRED 字段添加到现有表模式。 - 因此,当文档中提到更新架构时,它们看起来意味着添加新字段。 - cloud.google.com/bigquery/docs/tables#update-schema
    • 我看到了.bq mk -t market.cust1 custid:string,grp:integer,odate:string bq update market.cust1 custid:string,grp:integer,odate:string,custid1:string 工作对我来说,但 bq update market.cust3 custid:string,grp:string,odate:string,custid1:string 抛出错误。我相信它应该允许。再次非常感谢。
    【解决方案2】:

    运行一个查询,将现有表替换为根据需要转换数据的查询结果:

    #standardSQL
    SELECT a, b, c, CAST(d as INT64) AS d
    FROM `p.t.d`
    

    根据评论更新 - 如果我想将整数更改为字符串:

    #standardSQL
    SELECT a, b, c, CAST(d as STRING) AS d
    FROM `p.t.d`
    

    【讨论】:

    • 我想更新表结构。让我的列是整数,但我想将其更改为字符串。在普通 sql 中,我们编写了 alter table..
    • 我添加了一个关于如何在 BigQuery 中将整数更改为字符串的示例。只需用此查询的结果覆盖您的表即可。
    • 参见 market.cust custid:integer。我无法将字符串值插入客户 ID(值为 CU101)。所以我必须改变表的结构。传统 SQL:ALTER TABLE 表名。 ALTER COLUMN column_name 数据类型;大查询:bq update -t​​ market.cust custid:string,grp:integer,odate:string.. 它抛出错误
    • 是的。不要那样做。 bq update 不适用于这种情况。相反,如答案所示执行SELECT ...,并实现这些结果。那会奏效的。
    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 2018-07-10
    • 2021-02-14
    • 2019-02-25
    • 2021-04-07
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    相关资源
    最近更新 更多