【问题标题】:BigQuery Partitioned Table (on DAY) it does not partition - pythonBigQuery 分区表(在 DAY)它不分区 - python
【发布时间】:2022-02-22 17:54:40
【问题描述】:

我在 BigQuery 的分区表中创建和插入数据时遇到问题。

我的脚本每天都会下载过去 7 天的一些数据。我将它们保存在数据框中,然后将它们加载到 Big Query 分区表中。

昨天脚本将前 7 天的数据保存到 BigQuery 表中(2022-02-14 和 2022-02-20 之间)。 今天它保存了 7 天前(2022-02-15 和 2022-02-21 之间)的数据,但不再有昨天保存的前 7 天的数据(例如,没有更多数据2022-02-14 那天)。

这是我的代码:

 schema = [
            bigquery.SchemaField("Date", "DATE", "REQUIRED"),
            bigquery.SchemaField("Advertiser", "STRING", "REQUIRED"),
            bigquery.SchemaField("AdvertiserId", "INTEGER", "REQUIRED"),
            bigquery.SchemaField("Campaign", "STRING", "REQUIRED"),
            bigquery.SchemaField("CampaignId", "INTEGER", "REQUIRED")
    ]

    job_config = bigquery.LoadJobConfig(
        schema=schema,
        write_disposition="WRITE_TRUNCATE",
        time_partitioning=bigquery.TimePartitioning(
            type_=bigquery.TimePartitioningType.DAY,
            field="Date",  # Name of the column to use for partitioning.
        ),
    )

    job = client.load_table_from_dataframe(df, MY_PROJECT_ID.MY_DATASET.MY_TABLE_NAME), job_config=job_config)  # Make an API request.
    job.result()  # Wait for the job to complete.

为什么分区不工作? 谢谢!

【问题讨论】:

    标签: python google-cloud-platform google-bigquery


    【解决方案1】:

    如果我对您的理解正确,则表没有分区,而是以前执行的数据不像您所期望的那样存在。

    您的 write_disposition 当前设置为 WRITE_TRUNCATE,这意味着每次加载作业运行时表都会被清空。因此,您将拥有的唯一数据是最新运行的数据。

    你有几个选择:

    1. 将此更改为 DML 语句并根据日期和可能的其他条件进行合并。
    2. 添加一个附加列,例如 LoadDateTime,它是当前时间戳,并将您的 write_disposition 更改为 WRITE_APPEND。这样您就可以在以后的视图中区分出每个日期的最新负载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多