""" 同步成交信息至206 """ import logging import pandas as pd import sys from goojia_utils import ElasticSearchClient, commutils, dateutils, ClickHouseClient from elasticsearch import helpers log = logging.getLogger(__name__) es206_client = ElasticSearchClient(host='192.168.0.206') tmp_ck = ClickHouseClient(database='tmp') index_name = "community_summary" path = "/home/airflow/data/community" 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}") cities = None tmp_sql = f"select * from community_summary " if pd.notna(city): cities = [city] tmp_sql += f" where city='{city}'" seaweed_df = tmp_ck.read_sql_query(tmp_sql) deal_price_df = commutils.get_deal_avg_price(cities, 3) if len(deal_price_df) <= 0: log.info(f"无成交,无需同步") return if 'match_city' not in deal_price_df.columns or 'match_region' not in deal_price_df.columns or 'match_name' not in deal_price_df.columns: log.info(f"无成交,无需同步") return 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}") city = sys.argv[1] if len(sys.argv) > 1 else None sync(city)