67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
"""
|
|
同步成交信息至206
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import ElasticSearchClient, commutils, dateutils
|
|
from elasticsearch import helpers
|
|
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
|
|
index_name = "community_summary"
|
|
m_date = dateutils.iso_format()
|
|
|
|
|
|
def build_doc(item):
|
|
doc_dict = dict()
|
|
if pd.notna(item['three_month_trade_avg_price']):
|
|
doc_dict['three_month_trade_avg_price'] = int(item['three_month_trade_avg_price'])
|
|
return doc_dict
|
|
|
|
|
|
def sync(city=None):
|
|
log.info(f"开始同步成交信息至206 {index_name}")
|
|
|
|
seaweed_query = {'cat': 'district', 'status': 0}
|
|
cities = None
|
|
if pd.notna(city):
|
|
seaweed_query['city'] = city
|
|
cities = [city]
|
|
seaweed_df = pd.read_csv('/opt/airflow/dags/repo/dags/business/business/community/data/seaweed.csv', sep='@', encoding='UTF-8')
|
|
|
|
deal_price_df = commutils.get_deal_avg_price(cities, 3)
|
|
deal_price_df = deal_price_df[['match_city', 'match_region', 'match_name', 'avg_price']]
|
|
deal_price_df = deal_price_df.rename(columns={'match_city': 'city', 'match_region': 'region', 'match_name': 'name', 'avg_price': 'three_month_trade_avg_price'})
|
|
|
|
seaweed_df = pd.merge(seaweed_df, deal_price_df, how='left', on=['city', 'region', 'name'])
|
|
|
|
|
|
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
|
|
}
|
|
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
try:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
print("更新:" + str(count))
|
|
except Exception as e:
|
|
print(e)
|
|
actions.clear()
|
|
if len(actions) > 0:
|
|
try:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
except Exception as e:
|
|
print(e)
|
|
actions.clear()
|
|
log.info(f"完成同步成交信息至206 {index_name}") |