gpt4 book ai didi

validation - 在插入 BigQuery 表之前检查数据是否已经存在(使用 Python)

转载 作者:行者123 更新时间:2023-12-04 16:11:26 27 4
gpt4 key购买 nike

我正在设置一个每日 cron 作业,将一行附加到 BigQuery 表(使用 Python),但是,插入了重复数据。我在网上搜索过,我知道有一种方法可以手动 remove duplicate数据,但我想看看我是否可以首先避免这种重复。

有没有办法首先检查 BigQuery 表以查看数据记录是否已经存在,以避免插入重复数据?谢谢。

代码片段:

import webapp2
import logging
from googleapiclient import discovery
from oath2client.client import GoogleCredentials

PROJECT_ID = 'foo'
DATASET_ID = 'bar'
TABLE_ID = 'foo_bar_table’

class UpdateTableHandler(webapp2.RequestHandler):
def get(self):
credentials = GoogleCredentials.get_application_default()
service = discovery.build('bigquery', 'v2', credentials=credentials)

try:

the_fruits = Stuff.query(Stuff.fruitTotal >= 5).filter(Stuff.fruitColor == 'orange').fetch();

for fruit in the_fruits:
#some code here

basket = dict()
basket['id'] = fruit.fruitId
basket['Total'] = fruit.fruitTotal
basket['PrimaryVitamin'] = fruit.fruitVitamin
basket['SafeRaw'] = fruit.fruitEdibleRaw
basket['Color'] = fruit.fruitColor
basket['Country'] = fruit.fruitCountry

body = {
'rows': [
{
'json': basket,
'insertId': str(uuid.uuid4())
}
]
}

response = bigquery_service.tabledata().insertAll(projectId=PROJECT_ID,
datasetId=DATASET_ID,
tableId=TABLE_ID,
body=body).execute(num_retries=5)
logging.info(response)

except Exception, e:
logging.error(e)

app = webapp2.WSGIApplication([
('/update_table', UpdateTableHandler),
], debug=True)

最佳答案

测试数据是否已经存在的唯一方法是运行查询。

如果表中有大量数据,该查询的开销可能很大,因此在大多数情况下,我们建议您先插入重复项,然后再合并重复项。

正如 Zig Mandel 在评论中建议的那样,如果您知道希望看到记录的日期,则可以查询日期分区,但与插入和删除重复项相比,这可能仍然很昂贵。

关于validation - 在插入 BigQuery 表之前检查数据是否已经存在(使用 Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com