Files
airflow/dags/business/business/community/business_district.py
T
2023-02-20 18:29:04 +08:00

109 lines
2.9 KiB
Python

"""
小区最近商圈信息同步至ES206
"""
import logging
import pandas as pd
from goojia_utils import ElasticSearchClient, commutils, dateutils
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 find_nearest_by_location(city, location):
try:
scan = es206_client.scan('business_district', {
"size": 1,
"query": {
"bool": {
"filter": [
{
"term": {
"city": city
}
}
]
}
},
"sort": [
{
"_geo_distance": {
"location": location,
"order": "asc"
}
}
]
}, preserve_order=True)
for doc in scan:
return doc
return None
except Exception as e:
logging.error(e)
return None
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()
# 商圈
business_district_doc = find_nearest_by_location(curr_city, [lng, lat])
if pd.isna(business_district_doc):
return
doc_info['nearest_business_district_distance'] = int(business_district_doc['sort'][0])
doc_info['nearest_business_district'] = business_district_doc['_source']['name']
action = {
"_op_type": "update",
"_index": index_name,
"_id": item['_id'],
"doc": doc_info,
"_retry_on_conflict": 5
}
mutex.acquire(timeout=60)
actions.append(action)
if len(actions) == 5000:
helpers.bulk(es206_client.elastic_client, actions)
count += len(actions)
log.info("更新:" + str(count))
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:
helpers.bulk(es206_client.elastic_client, actions)
actions.clear()
log.info(f"完成同步商圈至206 {index_name}")
city = sys.argv[1] if len(sys.argv) > 1 else None
sync(city)