优化户均总价

This commit is contained in:
shenjian@fangjia.com
2022-10-28 16:26:49 +08:00
parent b1de7f5219
commit 12387cb2ef
+24 -23
View File
@@ -15,6 +15,7 @@ es206_client = ElasticSearchClient(host='192.168.0.206')
index_name = "community_summary" index_name = "community_summary"
path = "/home/airflow/data/community" path = "/home/airflow/data/community"
m_date = dateutils.iso_format() m_date = dateutils.iso_format()
avg_price_map = dict()
def build_doc(item): def build_doc(item):
@@ -24,30 +25,12 @@ def build_doc(item):
return doc_dict return doc_dict
def sync(city=None):
log.info(f"开始同步户均总价信息至206 {index_name}")
seaweed_query = {'cat': 'district', 'status': 0}
if pd.notna(city):
seaweed_query['city'] = city
seaweed_df = pd.read_csv(path + '/seaweed.csv', sep='@', encoding='UTF-8')
# 加载库中均价数据
query_sql = "SELECT city, region, name, avg_price FROM community_summary "
if pd.notna(city):
query_sql += " WHERE city='" + city + "' "
community_summary_df = es206_client.scan_by_sql_to_data_frame(query_sql)
avg_price_map = dict()
def avg_price_map_deal(x): def avg_price_map_deal(x):
avg_price_map[x['city'] + x['region'] + x['name']] = x['avg_price'] avg_price_map[x['city'] + x['region'] + x['name']] = x['avg_price']
return x return x
community_summary_df.apply(lambda x: avg_price_map_deal(x), axis=1)
with ThreadPoolExecutor(50) as pool: def deal(seaweed_df, index, item):
def deal(index, item):
if index % 1000 == 0: if index % 1000 == 0:
log.info("已处理" + str(index)) log.info("已处理" + str(index))
curr_city = item['city'] curr_city = item['city']
@@ -71,8 +54,26 @@ def sync(city=None):
total_price = commutils.get_total_price(curr_city, curr_region, names, day_before=366) total_price = commutils.get_total_price(curr_city, curr_region, names, day_before=366)
seaweed_df.loc[index, 'total_price'] = total_price seaweed_df.loc[index, 'total_price'] = total_price
def sync(city=None):
log.info(f"开始同步户均总价信息至206 {index_name}")
seaweed_df = pd.read_csv(path + '/seaweed.csv', sep='@', encoding='UTF-8')
if pd.notna(city):
seaweed_df = seaweed_df[seaweed_df['city'] == city].copy()
seaweed_df = seaweed_df.reset_index()
# 加载库中均价数据
query_sql = "SELECT city, region, name, avg_price FROM community_summary "
if pd.notna(city):
query_sql += " WHERE city='" + city + "' "
community_summary_df = es206_client.scan_by_sql_to_data_frame(query_sql)
community_summary_df.apply(lambda x: avg_price_map_deal(x), axis=1)
with ThreadPoolExecutor(30) as pool:
for index, item in seaweed_df.iterrows(): for index, item in seaweed_df.iterrows():
pool.submit(deal, index, item) pool.submit(deal, seaweed_df, index, item)
actions = list() actions = list()
count = 0 count = 0
@@ -91,15 +92,15 @@ def sync(city=None):
try: try:
helpers.bulk(es206_client.elastic_client, actions) helpers.bulk(es206_client.elastic_client, actions)
count += len(actions) count += len(actions)
print("已更新:" + str(count)) logging.info("已更新:" + str(count))
except Exception as e: except Exception as e:
print(e) log.error(e)
actions.clear() actions.clear()
if len(actions) > 0: if len(actions) > 0:
try: try:
helpers.bulk(es206_client.elastic_client, actions) helpers.bulk(es206_client.elastic_client, actions)
except Exception as e: except Exception as e:
print(e) log.error(e)
actions.clear() actions.clear()
log.info(f"完成同步户均总价信息至206 {index_name}") log.info(f"完成同步户均总价信息至206 {index_name}")