113 lines
3.6 KiB
Python
113 lines
3.6 KiB
Python
"""
|
|
同步删除小区基本信息至ES206
|
|
"""
|
|
# 兼容性补丁:解决 Elasticsearch 8.x/9.x 客户端在老版本 ES 集群上因 compatible-with 头部导致的 ApiError(406)
|
|
try:
|
|
import elasticsearch
|
|
ver = getattr(elasticsearch, '__version__', None)
|
|
if ver and not str(ver).startswith('7.'):
|
|
original_init = elasticsearch.Elasticsearch.__init__
|
|
def patched_init(self, *args, **kwargs):
|
|
headers = kwargs.get('headers', {})
|
|
headers['Accept'] = 'application/json'
|
|
headers['Content-Type'] = 'application/json'
|
|
kwargs['headers'] = headers
|
|
original_init(self, *args, **kwargs)
|
|
elasticsearch.Elasticsearch.__init__ = patched_init
|
|
except Exception:
|
|
pass
|
|
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, commutils, dateutils
|
|
from elasticsearch import helpers
|
|
import os
|
|
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"
|
|
m_date = dateutils.iso_format()
|
|
|
|
path = "/home/airflow/data/community"
|
|
if not os.path.exists(path):
|
|
os.makedirs(path)
|
|
|
|
def deal_comm(community_summary_df, seaweed_df):
|
|
"""
|
|
删除额外小区信息
|
|
"""
|
|
actions = list()
|
|
diff_ids = set(community_summary_df['_id']).difference(set(seaweed_df['_id']))
|
|
for _id in diff_ids:
|
|
action = {
|
|
"_op_type": "delete",
|
|
"_index": index_name,
|
|
"_id": _id,
|
|
}
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
if len(actions) > 0:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
|
|
|
|
def sync(city=None):
|
|
log.info(f"开始删除已删除小区至206 {index_name}...")
|
|
seaweed_query = {'status': 1, 'cat': 'district'}
|
|
if pd.notna(city):
|
|
seaweed_query['city'] = city
|
|
seaweed_df = pd.DataFrame(
|
|
list(seaweed_conn.find(seaweed_query, {'city': 1, 'region': 1, 'name': 1, 'status': 1}).batch_size(100000)))
|
|
seaweed_df['_id'] = seaweed_df['_id'].astype(str)
|
|
|
|
sql = f"select id from {index_name} "
|
|
|
|
if pd.notna(city):
|
|
sql += f" where city='{city}'"
|
|
community_summary_df = es206_client.scan_by_sql_to_data_frame(sql)
|
|
|
|
if len(community_summary_df) > 0:
|
|
community_summary_df['flag'] = 1
|
|
seaweed_new_df = pd.merge(seaweed_df, community_summary_df, how='left', on=['_id'])
|
|
seaweed_existed_df = seaweed_new_df[seaweed_new_df['flag'] == 1]
|
|
else:
|
|
seaweed_existed_df = pd.DataFrame()
|
|
|
|
actions = list()
|
|
# 对于存在的删除
|
|
count = 0
|
|
for index, item in seaweed_existed_df.iterrows():
|
|
action = {
|
|
"_op_type": "delete",
|
|
"_index": index_name,
|
|
"_id": str(item['_id'])
|
|
}
|
|
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
log.info("删除:" + str(count))
|
|
actions.clear()
|
|
if len(actions) > 0:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
log.info("删除:" + str(count))
|
|
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)
|