新增小区基本属性同步
This commit is contained in:
@@ -6,38 +6,15 @@ import logging
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, commutils, dateutils
|
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, commutils, dateutils
|
||||||
from elasticsearch import helpers
|
from elasticsearch import helpers
|
||||||
import datetime
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
|
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
|
||||||
es206_client = ElasticSearchClient(host='192.168.0.206')
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
||||||
|
|
||||||
index_name = "community_summary"
|
index_name = "community_summary"
|
||||||
|
|
||||||
log.info(f"开始同步小区基本属性至206 {index_name}...")
|
|
||||||
seaweed_df = pd.DataFrame(
|
|
||||||
list(seaweed_conn.find({'cat': 'district', 'status': 0}, {'city': 1, 'region': 1, 'name': 1, 'lng': 1, 'lat': 1,
|
|
||||||
'estate_type2': 1, 'build_type': 1, 'household_count': 1,
|
|
||||||
'estate_charge': 1, 'finish_building_date': 1})
|
|
||||||
.batch_size(100000)))
|
|
||||||
|
|
||||||
|
|
||||||
community_summary_df = es206_client.scan_by_sql_to_data_frame(f"select id from {index_name}")
|
|
||||||
seaweed_df['_id'] = seaweed_df['_id'].astype(str)
|
|
||||||
seaweed_df = seaweed_df.rename(columns={"_id": "id"})
|
|
||||||
|
|
||||||
if len(community_summary_df) > 0:
|
|
||||||
community_summary_df['flag'] = 1
|
|
||||||
|
|
||||||
seaweed_new_df = pd.merge(seaweed_df, community_summary_df, how='left', on=['id'])
|
|
||||||
seaweed_existed_df = seaweed_new_df[seaweed_new_df['flag'] == 1]
|
|
||||||
seaweed_not_existed_df = seaweed_new_df[seaweed_new_df['flag'] != 1]
|
|
||||||
else:
|
|
||||||
seaweed_existed_df = pd.DataFrame()
|
|
||||||
seaweed_not_existed_df = seaweed_df
|
|
||||||
|
|
||||||
m_date = dateutils.iso_format()
|
m_date = dateutils.iso_format()
|
||||||
|
|
||||||
|
|
||||||
def build_doc(item):
|
def build_doc(item):
|
||||||
"""
|
"""
|
||||||
构建字典
|
构建字典
|
||||||
@@ -75,39 +52,62 @@ def build_doc(item):
|
|||||||
return doc_dict
|
return doc_dict
|
||||||
|
|
||||||
|
|
||||||
actions = list()
|
def sync():
|
||||||
# 对于存在的更新
|
log.info(f"开始同步小区基本属性至206 {index_name}...")
|
||||||
for index, item in seaweed_existed_df.iterrows():
|
seaweed_df = pd.DataFrame(
|
||||||
doc_info = build_doc(item)
|
list(seaweed_conn.find({'cat': 'district', 'status': 0}, {'city': 1, 'region': 1, 'name': 1, 'lng': 1, 'lat': 1,
|
||||||
|
'estate_type2': 1, 'build_type': 1,
|
||||||
|
'household_count': 1,
|
||||||
|
'estate_charge': 1, 'finish_building_date': 1})
|
||||||
|
.batch_size(100000)))
|
||||||
|
|
||||||
action = {
|
community_summary_df = es206_client.scan_by_sql_to_data_frame(f"select id from {index_name}")
|
||||||
"_op_type": "update",
|
seaweed_df['_id'] = seaweed_df['_id'].astype(str)
|
||||||
"_index": index_name,
|
seaweed_df = seaweed_df.rename(columns={"_id": "id"})
|
||||||
"_id": item['id'],
|
|
||||||
"doc": doc_info
|
|
||||||
}
|
|
||||||
|
|
||||||
actions.append(action)
|
if len(community_summary_df) > 0:
|
||||||
if len(actions) == 1000:
|
community_summary_df['flag'] = 1
|
||||||
|
|
||||||
|
seaweed_new_df = pd.merge(seaweed_df, community_summary_df, how='left', on=['id'])
|
||||||
|
seaweed_existed_df = seaweed_new_df[seaweed_new_df['flag'] == 1]
|
||||||
|
seaweed_not_existed_df = seaweed_new_df[seaweed_new_df['flag'] != 1]
|
||||||
|
else:
|
||||||
|
seaweed_existed_df = pd.DataFrame()
|
||||||
|
seaweed_not_existed_df = seaweed_df
|
||||||
|
|
||||||
|
actions = list()
|
||||||
|
# 对于存在的更新
|
||||||
|
for index, item in seaweed_existed_df.iterrows():
|
||||||
|
doc_info = build_doc(item)
|
||||||
|
|
||||||
|
action = {
|
||||||
|
"_op_type": "update",
|
||||||
|
"_index": index_name,
|
||||||
|
"_id": item['id'],
|
||||||
|
"doc": doc_info
|
||||||
|
}
|
||||||
|
|
||||||
|
actions.append(action)
|
||||||
|
if len(actions) == 1000:
|
||||||
|
helpers.bulk(es206_client.elastic_client, actions)
|
||||||
|
actions.clear()
|
||||||
|
|
||||||
|
# 对于不存在的插入
|
||||||
|
for index, item in seaweed_not_existed_df.iterrows():
|
||||||
|
doc_info = build_doc(item)
|
||||||
|
|
||||||
|
action = {
|
||||||
|
"_op_type": "index",
|
||||||
|
"_index": index_name,
|
||||||
|
"_id": item['id'],
|
||||||
|
"_source": doc_info
|
||||||
|
}
|
||||||
|
|
||||||
|
actions.append(action)
|
||||||
|
if len(actions) == 1000:
|
||||||
|
helpers.bulk(es206_client.elastic_client, actions)
|
||||||
|
actions.clear()
|
||||||
|
if len(actions) > 0:
|
||||||
helpers.bulk(es206_client.elastic_client, actions)
|
helpers.bulk(es206_client.elastic_client, actions)
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
log.info(f"完成同步小区基本属性至206 {index_name}")
|
||||||
# 对于不存在的插入
|
|
||||||
for index, item in seaweed_not_existed_df.iterrows():
|
|
||||||
doc_info = build_doc(item)
|
|
||||||
|
|
||||||
action = {
|
|
||||||
"_op_type": "index",
|
|
||||||
"_index": index_name,
|
|
||||||
"_id": item['id'],
|
|
||||||
"_source": doc_info
|
|
||||||
}
|
|
||||||
|
|
||||||
actions.append(action)
|
|
||||||
if len(actions) == 1000:
|
|
||||||
helpers.bulk(es206_client.elastic_client, actions)
|
|
||||||
actions.clear()
|
|
||||||
if len(actions) > 0:
|
|
||||||
helpers.bulk(es206_client.elastic_client, actions)
|
|
||||||
actions.clear()
|
|
||||||
log.info(f"完成同步小区基本属性至206 {index_name}")
|
|
||||||
|
|||||||
+2
@@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
import comm
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -8,4 +9,5 @@ log.info("同步类型:" + type)
|
|||||||
|
|
||||||
|
|
||||||
if type == 'all':
|
if type == 'all':
|
||||||
|
comm.sync()
|
||||||
pass
|
pass
|
||||||
@@ -12,6 +12,6 @@ with DAG(
|
|||||||
) as dag:
|
) as dag:
|
||||||
t1 = BashOperator(
|
t1 = BashOperator(
|
||||||
task_id='community_summary_dag',
|
task_id='community_summary_dag',
|
||||||
bash_command='python /opt/airflow/dags/repo/dags/business/business/community_summary.py {{dag_run.conf["type"] if "type" in dag_run.conf}}',
|
bash_command='python /opt/airflow/dags/repo/dags/business/business/community/community_summary.py {{dag_run.conf["type"] if "type" in dag_run.conf}}',
|
||||||
)
|
)
|
||||||
t1
|
t1
|
||||||
Reference in New Issue
Block a user