66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
"""
|
|
户均总价根据房源面积补充至206
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import ElasticSearchClient, dateutils
|
|
from elasticsearch import helpers
|
|
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
|
|
index_name = "community_summary"
|
|
path = "/opt/airflow/dags/repo/dags/business/business/community/data"
|
|
m_date = dateutils.iso_format()
|
|
|
|
|
|
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 sync():
|
|
log.info(f"开始补全户均总价信息至206 {index_name}")
|
|
|
|
factor_df = pd.read_csv(path + '/小区户均总价面积因子已去极值.csv', sep='@', encoding='UTF-8')
|
|
community_df = es206_client.scan_by_sql_to_data_frame("select city, region, name, avg_price from community_summary where avg_price is not null and total_price is null")
|
|
|
|
community_factor_df = pd.merge(community_df, factor_df, how='left', on=['_id', 'city', 'region', 'name'])
|
|
community_factor_df['total_price'] = community_factor_df['avg_price'] * community_factor_df['factor']
|
|
community_factor_df = community_factor_df[community_factor_df['total_price'].notna()]
|
|
community_factor_df['total_price'] = community_factor_df['total_price'].astype(int)
|
|
|
|
actions = list()
|
|
count = 0
|
|
for index, item in community_factor_df.iterrows():
|
|
doc_info = build_doc(item)
|
|
|
|
action = {
|
|
"_op_type": "update",
|
|
"_index": index_name,
|
|
"_id": item['_id'],
|
|
"doc": doc_info
|
|
}
|
|
|
|
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}")
|
|
|
|
|
|
sync()
|