Files
airflow/dags/company/company/fill_company_dev.py
T
2025-06-20 13:30:38 +08:00

79 lines
3.0 KiB
Python

from goojia_utils import DIDATA_DEV_ELASTIC_CLIENT, areautils
import pandas as pd
from elasticsearch import helpers
import numpy as np
import re
import logging
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
"""
通过开发区围栏更新企业开发区 TODO
"""
def sync(province):
logger.info(f"开始同步{province}企业数据的开发区数据")
# 先临时禁用索引刷新
DIDATA_DEV_ELASTIC_CLIENT.elastic_client.indices.put_settings(
index="didata_company",
body={"index": {"refresh_interval": "-1"}}
)
shape_df = DIDATA_DEV_ELASTIC_CLIENT.scan_by_sql_to_data_frame(f"select name, shape from development_zone where province='{province}'")
if len(shape_df) == 0 or 'shape' not in shape_df.columns or len(shape_df[shape_df['shape'].notna()]) == 0:
logger.info(f"{province}无围栏数据")
return
actions = list()
count = 0
for index, item in shape_df[shape_df['shape'].notna()].iterrows():
logger.info(f"开始查找{item['name']}下企业数据")
ret = DIDATA_DEV_ELASTIC_CLIENT.fetch_all_geo_points('didata_company', item['shape']['coordinates'][0],
field='location')
com_count = 0
for info in ret:
com_count += 1
doc_info = {'dev': item['name']}
action = {
"_op_type": "update",
"_index": "didata_company",
"_id": info['_id'],
"doc": doc_info,
"_retry_on_conflict": 5
}
actions.append(action)
logger.info(f"已查找到该围栏下{com_count}个企业数据")
if len(actions) > 0:
for success, info in helpers.parallel_bulk(
DIDATA_DEV_ELASTIC_CLIENT.elastic_client,
actions,
thread_count=5, # 并行线程数
chunk_size=10000 # 批量大小
):
if not success:
print(f"索引失败: {info}")
count += len(actions)
print("更新:" + str(count))
actions.clear()
if len(actions) > 0:
for success, info in helpers.parallel_bulk(
DIDATA_DEV_ELASTIC_CLIENT.elastic_client,
actions,
thread_count=5, # 并行线程数
chunk_size=10000 # 批量大小
):
if not success:
print(f"索引失败: {info}")
count += len(actions)
print("更新:" + str(count))
actions.clear()
logger.info(f"完成同步{province}企业数据的开发区数据")
# 恢复索引刷新
DIDATA_DEV_ELASTIC_CLIENT.elastic_client.indices.put_settings(
index="didata_company",
body={"index": {"refresh_interval": "5s"}}
)
if __name__ == '__main__':
for province in areautils.get_all_province():
sync(province)