""" 生活配套相关信息同步至ES206 """ import logging import pandas as pd from goojia_utils import ElasticSearchClient, commutils, dateutils, poiutils from elasticsearch import helpers from concurrent.futures import ThreadPoolExecutor from threading import Lock import sys log = logging.getLogger(__name__) es206_client = ElasticSearchClient(host='192.168.0.206') index_name = "community_summary" path = "/home/airflow/data/community" m_date = dateutils.iso_format() count = 0 mutex = Lock() def deal(actions, item): global count curr_city = item['city'] lng = commutils.format_lat_lng(item['lng']) lat = commutils.format_lat_lng(item['lat']) if pd.isna(lng) or pd.isna(lat): return doc_info = dict() # 商场 mall = poiutils.find_around_by_location(curr_city, 1000, [lng, lat], "生活配套", "商场") doc_info['mall_num'] = len(mall) if len(mall) > 0: doc_info['mall'] = ",".join(mall) # 超市 supermarket = poiutils.find_around_by_location(curr_city, 1000, [lng, lat], "生活配套", "超市") doc_info['supermarket_num'] = len(supermarket) if len(supermarket) > 0: doc_info['supermarket'] = ",".join(supermarket) # 公园 park = poiutils.find_around_by_location(curr_city, 1000, [lng, lat], "生活配套", "公园") doc_info['park_num'] = len(park) if len(park) > 0: doc_info['park'] = ",".join(park) # 餐厅 catering = poiutils.find_around_by_location(curr_city, 1000, [lng, lat], "生活配套", "餐厅") doc_info['catering_num'] = len(catering) if len(catering) > 0: doc_info['catering'] = ",".join(catering) action = { "_op_type": "update", "_index": index_name, "_id": item['_id'], "doc": doc_info } mutex.acquire(timeout=60) actions.append(action) if len(actions) == 5000: try: helpers.bulk(es206_client.elastic_client, actions) count += len(actions) log.info("更新:" + str(count)) except Exception as e: log.error(e) actions.clear() mutex.release() 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) actions = list() with ThreadPoolExecutor(max_workers=30) as pool: for index, item in seaweed_df.iterrows(): pool.submit(deal, actions, item) 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)