新增删除逻辑

This commit is contained in:
沈健
2024-08-12 10:19:13 +08:00
parent 10f2e97cd2
commit 3bb8da4722
2 changed files with 95 additions and 1 deletions
@@ -0,0 +1,92 @@
"""
同步删除小区基本信息至ES206
"""
import logging
import pandas as pd
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, commutils, dateutils, ClickHouseClient
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.206')
tmp_ck = ClickHouseClient(database='tmp')
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}")
city = sys.argv[1] if len(sys.argv) > 1 else None
sync(city)
+3 -1
View File
@@ -2,6 +2,8 @@ from airflow.operators.bash import BashOperator
import pendulum
from airflow import DAG
from dags.business.business.community import del_comm
with DAG(
dag_id="community_summary_dag",
# 每月2日1点整分运行
@@ -116,5 +118,5 @@ with DAG(
bash_command='python /opt/airflow/dags/repo/dags/business/business/community/full_name.py {{dag_run.conf["city"] if "city" in dag_run.conf}}',
)
# 人均消费并行,值为空
comm >> price >> [avg_cost, total_price, relative_price_ranking, estate_charge_complete] >> total_price_complete >> consumption >> city_level_and_price_rating >> credit >> [estate_charge_ranking, price_rating, person_rating, percentile] >> sell >> trade >> [traffic, business_district, medical, life, education] >> education2 >> extra >> other \
del_comm >> comm >> price >> [avg_cost, total_price, relative_price_ranking, estate_charge_complete] >> total_price_complete >> consumption >> city_level_and_price_rating >> credit >> [estate_charge_ranking, price_rating, person_rating, percentile] >> sell >> trade >> [traffic, business_district, medical, life, education] >> education2 >> extra >> other \
>> full_name