版本修改

This commit is contained in:
王文艳
2026-07-22 11:10:14 +08:00
parent aed37e0759
commit 327b36e5a0
2 changed files with 95 additions and 95 deletions
+82 -82
View File
@@ -1,82 +1,82 @@
import logging # import logging
import sys # import sys
from goojia_utils import MAIN_MONGO_CLIENT, DIDATA_MONGO_CLIENT, areautils # from goojia_utils import MAIN_MONGO_CLIENT, DIDATA_MONGO_CLIENT, areautils
from pymongo import UpdateOne # from pymongo import UpdateOne
from bson.objectid import ObjectId # from bson.objectid import ObjectId
import pandas as pd # import pandas as pd
import time # import time
#
log = logging.getLogger(__name__) # log = logging.getLogger(__name__)
#
type = sys.argv[1] if len(sys.argv) > 1 else 'all' # type = sys.argv[1] if len(sys.argv) > 1 else 'all'
log.info("同步类型:" + type) # log.info("同步类型:" + type)
#
#
def sync_district_office(type): # def sync_district_office(type):
if type != 'district' and type != 'office': # if type != 'district' and type != 'office':
return # return
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed') # seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
seaweed_didata_conn = DIDATA_MONGO_CLIENT.get_collection('fangjia', 'seaweed') # seaweed_didata_conn = DIDATA_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
#
cities = areautils.get_all_cities() # cities = areautils.get_all_cities()
update_date = int(round(time.time() * 1000)) # update_date = int(round(time.time() * 1000))
for city in cities: # for city in cities:
actions = list() # actions = list()
id_list = list() # id_list = list()
for doc in seaweed_conn.find({'city': city, 'cat': type, 'status': 0}, # for doc in seaweed_conn.find({'city': city, 'cat': type, 'status': 0},
{'city': 1, 'region': 1, 'block': 1, 'cat': 1, 'name': 1, 'address': 1, # {'city': 1, 'region': 1, 'block': 1, 'cat': 1, 'name': 1, 'address': 1,
'estate_type2': 1, 'township': 1, 'alias': 1, 'addr_alias': 1, 'lng': 1, 'lat': 1, # 'estate_type2': 1, 'township': 1, 'alias': 1, 'addr_alias': 1, 'lng': 1, 'lat': 1,
'status': 1}) \ # 'status': 1}) \
.batch_size(10000): # .batch_size(10000):
updateDict = {'city': doc['city'], 'region': doc['region'], 'category': doc['cat'], 'name': doc['name'], # updateDict = {'city': doc['city'], 'region': doc['region'], 'category': doc['cat'], 'name': doc['name'],
'status': doc['status']} # 'status': doc['status']}
if 'estate_type2' in doc: # if 'estate_type2' in doc:
updateDict['estate_type'] = doc['estate_type2'] # updateDict['estate_type'] = doc['estate_type2']
#
id_list.append({'_id': doc['_id']}) # id_list.append({'_id': doc['_id']})
if 'block' in doc: # if 'block' in doc:
updateDict['block'] = doc['block'] # updateDict['block'] = doc['block']
if 'address' in doc: # if 'address' in doc:
updateDict['address'] = doc['address'] # updateDict['address'] = doc['address']
if 'township' in doc: # if 'township' in doc:
updateDict['township'] = doc['township'] # updateDict['township'] = doc['township']
if 'alias' in doc: # if 'alias' in doc:
updateDict['alias'] = doc['alias'] # updateDict['alias'] = doc['alias']
if 'addr_alias' in doc: # if 'addr_alias' in doc:
updateDict['addr_alias'] = doc['addr_alias'] # updateDict['addr_alias'] = doc['addr_alias']
if 'lng' in doc and doc['lng'] is not None and str(doc['lng']) != 'nan': # if 'lng' in doc and doc['lng'] is not None and str(doc['lng']) != 'nan':
updateDict['location'] = {'lon': doc['lng'] / 10 ** 10, 'lat': doc['lat'] / 10 ** 10} # updateDict['location'] = {'lon': doc['lng'] / 10 ** 10, 'lat': doc['lat'] / 10 ** 10}
updateDict['update_date'] = update_date # updateDict['update_date'] = update_date
action = UpdateOne({'_id': doc['_id']}, {'$set': updateDict}, upsert=True) # action = UpdateOne({'_id': doc['_id']}, {'$set': updateDict}, upsert=True)
#
actions.append(action) # actions.append(action)
if len(actions) == 10000: # if len(actions) == 10000:
seaweed_didata_conn.bulk_write(actions) # seaweed_didata_conn.bulk_write(actions)
actions.clear() # actions.clear()
#
didata_id_df = pd.DataFrame( # didata_id_df = pd.DataFrame(
list(seaweed_didata_conn.find({'city': city, 'category': type}, {'_id': 1}).batch_size(10000))) # list(seaweed_didata_conn.find({'city': city, 'category': type}, {'_id': 1}).batch_size(10000)))
if len(didata_id_df) > 0: # if len(didata_id_df) > 0:
didata_id_df = didata_id_df[['_id']] # didata_id_df = didata_id_df[['_id']]
id_df = pd.DataFrame(id_list) # id_df = pd.DataFrame(id_list)
deleted_seaweed_df = pd.concat([didata_id_df, id_df, id_df]).drop_duplicates(keep=False) # deleted_seaweed_df = pd.concat([didata_id_df, id_df, id_df]).drop_duplicates(keep=False)
#
for deleted_seaweed_index, deleted_seaweed_item in deleted_seaweed_df.iterrows(): # for deleted_seaweed_index, deleted_seaweed_item in deleted_seaweed_df.iterrows():
action = UpdateOne({'_id': ObjectId(deleted_seaweed_item['_id'])}, # action = UpdateOne({'_id': ObjectId(deleted_seaweed_item['_id'])},
{'$set': {'status': 1, 'update_date': update_date}}, upsert=False) # {'$set': {'status': 1, 'update_date': update_date}}, upsert=False)
actions.append(action) # actions.append(action)
if len(actions) == 10000: # if len(actions) == 10000:
seaweed_didata_conn.bulk_write(actions) # seaweed_didata_conn.bulk_write(actions)
actions.clear() # actions.clear()
#
if len(actions) > 0: # if len(actions) > 0:
seaweed_didata_conn.bulk_write(actions) # seaweed_didata_conn.bulk_write(actions)
actions.clear() # actions.clear()
log.info(city + "-" + type + "-同步完成") # log.info(city + "-" + type + "-同步完成")
#
#
if type == 'all': # if type == 'all':
sync_district_office("district") # sync_district_office("district")
sync_district_office("office") # sync_district_office("office")
else: # else:
sync_district_office(type) # sync_district_office(type)
+13 -13
View File
@@ -2,16 +2,16 @@ from airflow.operators.bash import BashOperator
import pendulum import pendulum
from airflow import DAG from airflow import DAG
with DAG( # with DAG(
dag_id="didata_sync_dag", # dag_id="didata_sync_dag",
# 每月28日3点整分运行 # # 每月28日3点整分运行
schedule="0 3 28 * *", # schedule="0 3 28 * *",
start_date=pendulum.datetime(2023, 3, 17, tz="Asia/Shanghai"), # start_date=pendulum.datetime(2023, 3, 17, tz="Asia/Shanghai"),
catchup=True, # catchup=True,
description="地数相关数据同步type: district或office, 不填默认同步全部" # description="地数相关数据同步type: district或office, 不填默认同步全部"
) as dag: # ) as dag:
t1 = BashOperator( # t1 = BashOperator(
task_id='didata_sync_dag', # task_id='didata_sync_dag',
bash_command='python /opt/airflow/dags/repo/dags/business/business/didata_sync.py {{dag_run.conf["type"] if "type" in dag_run.conf}}', # bash_command='python /opt/airflow/dags/repo/dags/business/business/didata_sync.py {{dag_run.conf["type"] if "type" in dag_run.conf}}',
) # )
t1 # t1