112 lines
3.5 KiB
Python
112 lines
3.5 KiB
Python
"""
|
|
同步户均总价至206
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, commutils, dateutils
|
|
from elasticsearch import helpers
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import re
|
|
import sys
|
|
|
|
log = logging.getLogger(__name__)
|
|
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
|
|
index_name = "community_summary"
|
|
path = "/home/airflow/data/community"
|
|
m_date = dateutils.iso_format()
|
|
avg_price_map = dict()
|
|
|
|
|
|
def build_doc(item):
|
|
doc_dict = dict()
|
|
if 'total_price' in item and pd.notna(item['total_price']):
|
|
doc_dict['total_price'] = int(item['total_price'])
|
|
return doc_dict
|
|
|
|
|
|
# def avg_price_map_deal(x):
|
|
# avg_price_map[x['city'] + x['region'] + x['name']] = x['avg_price']
|
|
# return x
|
|
|
|
|
|
def deal(seaweed_df, index, item):
|
|
if index % 1000 == 0:
|
|
log.info("已处理" + str(index))
|
|
curr_city = item['city']
|
|
curr_region = item['region']
|
|
curr_name = item['name']
|
|
names = [curr_name]
|
|
|
|
if pd.notna(item['alias']) and '[]' != item['alias']:
|
|
alias_list = re.sub("\[|\]|'", '', item['alias']).split(',')
|
|
if len(alias_list) > 0:
|
|
for alias_item in alias_list:
|
|
names.append(alias_item)
|
|
|
|
# 户均总价
|
|
# if (curr_city + curr_region + curr_name) in avg_price_map:
|
|
# avg_price = avg_price_map[curr_city + curr_region + curr_name]
|
|
# if pd.notna(avg_price):
|
|
# 由于覆盖率较低,暂不过滤
|
|
# min_avg_price = 0.8 * avg_price
|
|
# max_avg_rice = 1.2 * avg_price
|
|
total_price = commutils.get_total_price(curr_city, curr_region, names, day_before=366)
|
|
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(drop=True)
|
|
|
|
# 加载库中均价数据
|
|
# 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():
|
|
pool.submit(deal, seaweed_df, index, item)
|
|
|
|
actions = list()
|
|
count = 0
|
|
for index, item in seaweed_df.iterrows():
|
|
doc_info = build_doc(item)
|
|
|
|
action = {
|
|
"_op_type": "update",
|
|
"_index": index_name,
|
|
"_id": item['_id'],
|
|
"doc": doc_info,
|
|
"_retry_on_conflict": 5
|
|
}
|
|
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
try:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
logging.info("已更新:" + str(count))
|
|
except Exception as e:
|
|
log.error(e)
|
|
actions.clear()
|
|
if len(actions) > 0:
|
|
try:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
except Exception as e:
|
|
log.error(e)
|
|
actions.clear()
|
|
log.info(f"完成同步户均总价信息至206 {index_name}")
|
|
|
|
|
|
city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
sync(city)
|