新增地铁入库

This commit is contained in:
Jian Shen
2022-06-27 13:53:28 +08:00
parent b020ad3561
commit 21d0eb48f6
2 changed files with 111 additions and 1 deletions
+106
View File
@@ -0,0 +1,106 @@
"""
小区周边1公里地铁地铁入库业务逻辑
"""
import logging
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient
import pandas as pd
from pymongo import UpdateOne
log = logging.getLogger(__name__)
elastic_client = ElasticSearchClient(host='192.168.0.206')
district_facilitys_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'district_facilitys')
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
server_area_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'server_area')
city_dict = list(server_area_conn.find({'city_grade': {'$ne': 0}}, {'display': 1}))
# 地铁code
subway_typecode = ["150500"]
log.info("开始执行周边地铁配套入库")
def find_around_by_location(city, location):
"""
查找周边poi
:param city:
:param location:
:return:
"""
try:
subway_df = elastic_client.scan_to_data_frame('amap_poi', {
"_source": ["name"],
"query": {
"bool": {
"must_not": [
{
"term": {
"status": {
"value": 1
}
}
}
],
"filter": [
{"terms": {
"typecode": subway_typecode
}},
{
"term": {
"cityname": {
"value": city
}
}
},
{
"geo_distance": {
"distance": 1000,
"location": location
}
}
]
}
}
});
if len(subway_df) > 0:
return subway_df['name'].tolist()
return []
except Exception as e:
print(e)
return []
def deal(city, lng, lat):
location = [lng, lat]
subway_df = find_around_by_location(city, location)
return subway_df
for city_item in city_dict:
city = city_item['display']
log.info('开始查找' + city + '周边地铁')
seaweed_df = pd.DataFrame(list(seaweed_conn.find({'city': city, 'cat': 'district', 'status': 0, 'lng': {'$ne': None}},
{'city': 1, 'region': 1, 'name': 1, 'lng': 1, 'lat': 1}).batch_size(
10000)))
if len(seaweed_df) <= 0:
continue
seaweed_df['lng'] = seaweed_df['lng'] / 10 ** 10
seaweed_df['lat'] = seaweed_df['lat'] / 10 ** 10
seaweed_df['subway'] = seaweed_df.apply(lambda x: deal(x['city'], x['lng'], x['lat']), axis=1)
actions = list()
log.info(city + '周边地铁开始入库')
for index, item in seaweed_df.iterrows():
try:
# 周边配套库中历史原因,id可能不为小区id
action = UpdateOne({'city': item['city'], 'region': item['region'],
'district_name': item['name']}, {'$set': {'subway': item['subway']}}, upsert=True)
actions.append(action)
except Exception as e:
print(e)
if len(actions) == 5000:
district_facilitys_conn.bulk_write(actions)
actions.clear()
if len(actions) > 0:
district_facilitys_conn.bulk_write(actions)
actions.clear()
log.info(city + '周边地铁入库完毕')
+5 -1
View File
@@ -15,8 +15,12 @@ with DAG(
catchup=True,
) as dag:
t1 = BashOperator(
task_id='district_subway',
bash_command='python /opt/airflow/dags/repo/dags/poi/business/district_subway.py',
)
t2 = BashOperator(
task_id='district_bus',
bash_command='python /opt/airflow/dags/repo/dags/poi/business/district_bus.py',
)
t1
t1 > t2