diff --git a/dags/poi/business/district_bus.py b/dags/poi/business/district_bus.py new file mode 100644 index 0000000..aae1ec7 --- /dev/null +++ b/dags/poi/business/district_bus.py @@ -0,0 +1,104 @@ +""" +小区周边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 +bus_typecode = ["150700","150500"] + +log.info("开始执行周边交通配套入库") + +def find_around_by_location(city, location): + """ + 查找周边poi + :param city: + :param location: + :return: + """ + bus_df = elastic_client.scan_to_data_frame('amap_poi', { + "_source": ["name"], + "query": { + "bool": { + "must": [ + {"terms": { + "typecode": bus_typecode + }}, + { + "term": { + "cityname": { + "value": city + } + } + }, + { + "term": { + "status": { + "value": 0 + } + } + } + ], + "filter": [ + { + "geo_distance": { + "distance": 1000, + "location": location + } + } + ] + } + } + }); + if len(bus_df) > 0: + return bus_df['name'].tolist() + return [] + + +def deal(city, lng, lat): + if lng != None: + location = [lng, lat] + bus_df = find_around_by_location(city, location) + return bus_df + return [] + + +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}, + {'city': 1, 'region': 1, 'name': 1, 'lng': 1, 'lat': 1}).batch_size( + 10000))) + seaweed_df['lng'] = seaweed_df['lng'] / 10 ** 10 + seaweed_df['lat'] = seaweed_df['lat'] / 10 ** 10 + seaweed_df['bus'] = 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': {'bus': item['bus'], 'city': item['city'], 'region': item['region'], + 'district_name': item['name'], 'lng': item['lng'], 'lat': item['lat']}}, 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 + '周边交通入库完毕') diff --git a/dags/poi/district_bank_dag.py b/dags/poi/district_bank_dag.py index db9fbd7..8ecf6b8 100644 --- a/dags/poi/district_bank_dag.py +++ b/dags/poi/district_bank_dag.py @@ -1,5 +1,5 @@ """ -小区周边配套DAG +小区周边银行DAG """ from airflow.operators.bash import BashOperator diff --git a/dags/poi/district_bus_dag.py b/dags/poi/district_bus_dag.py new file mode 100644 index 0000000..53ea5a0 --- /dev/null +++ b/dags/poi/district_bus_dag.py @@ -0,0 +1,21 @@ +""" +小区周边公交DAG +""" +from airflow.operators.bash import BashOperator + +import pendulum + +from airflow import DAG + +with DAG( + "district_bus_dag", + schedule_interval="@monthly", + start_date=pendulum.datetime(2022, 6, 20, tz="Asia/Shanghai"), + catchup=True, +) as dag: + t1 = BashOperator( + task_id='district_bus', + bash_command='python /opt/airflow/dags/repo/dags/poi/business/district_bus.py', + ) + t1 +