通过忽略SCHEMA将Big查询表追加到现有查询表中

时间:2019-07-12 14:31:06

标签: python google-bigquery

我有一个直截了当的问题,在我遇到错误时,Google是否可以通过忽略SCHEMA来将一个表追加到另一个表中?

Invalid schema update. Field XXXhas changed type from NUMERIC to FLOAT

Python代码:

def COPY():
    client = bigquery.Client()

    job_config = bigquery.QueryJobConfig()
    job_config.destination = client.dataset("DATASET").table("TABLE")
    #job_config.create_disposition = 'CREATE_IF_NEEDED'
    job_config.write_disposition = 'WRITE_APPEND'
    #job_config._properties['schemaUpdateOptions'] = ['ALLOW_FIELD_ADDITION']

    wave_sql = 'SELECT * FROM `PROJECT.DATASET.TABLE`'
    wave = client.query(wave_sql,location='EU',job_config=job_config)  
    wave.result()

3 个答案:

答案 0 :(得分:2)

wave_sql = 'SELECT * REPLACE(CAST(XXXhas AS NUMERIC) AS XXXhas) FROM `PROJECT.DATASET.TABLE`'  

注意:您将为整个表扫描付费,如果架构相同,则您宁愿使用COPY(而不是SELECT *);同样,您不能使用SELECT * EXCEPT() ...模式,因为这会改变模式的布局-因此,您应该使用SELECT * REPLACE() ...

答案 1 :(得分:1)

如果您不担心丢失精度,则可以更改代码的一行以使其起作用:

    wave_sql = 'SELECT * EXCEPT(XXXhas), CAST(XXXhas AS FLOAT64) XXXhas FROM `PROJECT.DATASET.TABLE`'

答案 2 :(得分:0)

您随时可以尝试通过相关列的changing the data type解决此错误。

例如:

SELECT column_two, column_three, CAST(column_one AS STRING) AS column_one FROM mydataset.mytable

然后选择适当的表,然后选择“覆盖”