76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
"""
|
|
同步户均总价至206
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, dateutils
|
|
from elasticsearch import helpers
|
|
import sys
|
|
|
|
log = logging.getLogger(__name__)
|
|
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
|
|
es206_client = ElasticSearchClient(host='192.168.0.89', username='elastic', password='elasticsearch')
|
|
|
|
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 sync(city=None):
|
|
log.info(f"开始同步户均总价信息至206 {index_name}")
|
|
|
|
# 加载库中均价数据
|
|
query_sql = "SELECT city, region, name, avg_price, house_avg_area FROM community_summary WHERE avg_price > 0 and house_avg_area > 0 "
|
|
if pd.notna(city):
|
|
query_sql += " AND city='" + city + "' "
|
|
community_summary_df = es206_client.scan_by_sql_to_data_frame(query_sql)
|
|
|
|
actions = list()
|
|
count = 0
|
|
community_summary_df['total_price'] = community_summary_df['avg_price'] * community_summary_df['house_avg_area']
|
|
community_summary_df['total_price'] = community_summary_df['total_price'].astype(int)
|
|
for index, item in community_summary_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}")
|
|
|
|
|
|
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
if pd.notna(sys_city) and "," in sys_city:
|
|
cities = sys_city.split(",")
|
|
for city in cities:
|
|
sync(city)
|
|
else:
|
|
sync(sys_city)
|