我的用例
我的代码在美国不同的县运行多个Scrapy spider来收集每个房产的房产数据。这是通过循环一系列的pin/parcel(100k到200k)来完成的,这些pin/parcel一次又一次地附加到相同的url中,收集每个地块或房产的销售数据,并将这些数据存储在各自的country表中,一行一行。我的用例涉及频繁地更新这些表(大约一周更新一次)以收集销售数据的趋势。在10万个楼盘中,可能只有少数几个楼盘获得了新的销售记录,但除非我把所有的记录都看一遍,否则我就不知道了。你知道吗
我现在开始通过下面的管道实现这个功能,当表是一张干净的石板时,它基本上完成了在第一次运行时获取表中的数据。但是,在重新运行以刷新数据时,我显然无法插入包含相同唯一ID的行,因此需要更新该行。每个数据点的唯一ID是其地块号。你知道吗
我的问题-
1. What is the optimal method to update a database table that
requires a full refresh(all rows) frequently?
到目前为止,根据我所做的研究,我的猜测是用一个新的临时表替换旧表。这是因为(我认为)将所有数据插入一个新表要比查询旧表中的每一项更快,查看它是否已更改,如果已更改,则修改该行。这可以通过先将所有数据插入临时表,然后用新表替换旧表来实现。你知道吗
如果我的实现方法是最优的,我将如何实现它?你知道吗
我是否应该使用某种数据迁移模块(panda?)-如果我丢弃了旧表,并且在替换新表之前程序在此时被中断,会发生什么。你知道吗
class PierceDataPipeline(object):
def __init__(self):
"""
Initializes database connection and sessionmaker.
Creates tables.
"""
engine = db_connect()
create_table(engine)
self.Session = sessionmaker(bind=engine)
def process_item(self,item,spider):
"""
This method is called for every item pipeline component
"""
session = self.Session()
propertyDataTable = PierceCountyPropertyData()
propertyDataTable.parcel = item["parcel"]
propertyDataTable.mailing_address = item["mailing_address"]
propertyDataTable.owner_name = item["owner_name"]
propertyDataTable.county = item["county"]
propertyDataTable.site_address = item["site_address"]
propertyDataTable.property_type = item["property_type"]
propertyDataTable.occupancy = item["occupancy"]
propertyDataTable.year_built = item["year_built"]
propertyDataTable.adj_year_built = item["adj_year_built"]
propertyDataTable.units = item["units"]
propertyDataTable.bedrooms = item["bedrooms"]
propertyDataTable.baths = item["baths"]
propertyDataTable.siding_type = item["siding_type"]
propertyDataTable.stories = item["stories"]
propertyDataTable.lot_square_footage = item["lot_square_footage"]
propertyDataTable.lot_acres = item["lot_acres"]
propertyDataTable.current_balance_due = item["current_balance_due"]
propertyDataTable.tax_year_1 = item["tax_year_1"]
propertyDataTable.tax_year_2 = item["tax_year_2"]
propertyDataTable.tax_year_3 = item["tax_year_3"]
propertyDataTable.tax_year_1_assessed = item["tax_year_1_assessed"]
propertyDataTable.tax_year_2_assessed = item["tax_year_2_assessed"]
propertyDataTable.tax_year_3_assessed = item["tax_year_3_assessed"]
propertyDataTable.sale1_price = item["sale1_price"]
propertyDataTable.sale1_date = item["sale1_date"]
propertyDataTable.sale2_date = item["sale2_date"]
propertyDataTable.sale2_price = item["sale2_price"]
try:
session.add(propertyDataTable)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
return item
目前没有回答
相关问题 更多 >
编程相关推荐