Google BQ - how to upsert existing data in tables?

后端 未结 4 643
攒了一身酷
攒了一身酷 2020-12-05 01:17

I\'m using Python client library for loading data in BigQuery tables. I need to update some changed rows in those tables. But I couldn\'t figure out how to correctly update

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 01:35

    BigQuery does not support UPSERT directly, but if you really need it - you can use UPDATE and INSERT one after another to achieve the same. See below simplified example

    Assume you have two tables as below - one that holds your data (yourproject.yourdadtaset.table_data) and another (yourproject.yourdadtaset.table_changes) that contains your changes that you want to apply to first table

    table_data

    table_changes

    Now below queries run one after another do the trick:

    Update Query:

    #standardSQL
    UPDATE `yourproject.yourdadtaset.table_data` t
    SET t.value = s.value
    FROM `yourproject.yourdadtaset.table_changes` s
    WHERE t.id = s.id
    

    result will be

    And now - INSERT Query

    #standardSQL
    INSERT `yourproject.yourdadtaset.table_data` (id, value)
    SELECT id, value
    FROM `yourproject.yourdadtaset.table_changes`
    WHERE NOT id IN (SELECT id FROM `yourproject.yourdadtaset.table_data`)
    

    with result as (and we are done here)

    Hope above example simple and clear, so you can apply it in your case

提交回复
热议问题