66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
"""
|
|
物业费等级
|
|
"""
|
|
from goojia_utils import ElasticSearchClient, dateutils
|
|
import logging
|
|
import pandas as pd
|
|
from elasticsearch import helpers
|
|
import sys
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.89', username='elastic', password='elasticsearch')
|
|
|
|
index_name = "community_summary"
|
|
path = "/home/airflow/data/community"
|
|
m_date = dateutils.iso_format()
|
|
|
|
count = 0
|
|
|
|
|
|
def deal(actions, x):
|
|
global count
|
|
doc_info = dict()
|
|
if 'rent_price' in x and pd.notna(x['avg_price']) and pd.notna(x['rent_price']):
|
|
doc_info['sell_to_rent'] = int(x['avg_price'] / x['rent_price'])
|
|
if pd.notna(x['household_count']):
|
|
doc_info['person_num'] = int(x['household_count'] * 2.3)
|
|
|
|
action_doc = {
|
|
"_op_type": "update",
|
|
"_index": index_name,
|
|
"_id": x['_id'],
|
|
"doc": doc_info
|
|
}
|
|
if len(doc_info) > 0:
|
|
actions.append(action_doc)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
log.info(f"售租比、人口数已更新{count}")
|
|
actions.clear()
|
|
|
|
|
|
def sync(city=None):
|
|
log.info(f"开始同步售租比、人口数至206 {index_name}")
|
|
|
|
sql = "select city, region, name, finish_building_date, build_type, avg_price, rent_price, household_count from community_summary"
|
|
if pd.notna(city):
|
|
sql += " where city='" + city + "'"
|
|
community_summary_df = es206_client.scan_by_sql_to_data_frame(sql)
|
|
|
|
actions = list()
|
|
community_summary_df.apply(lambda x: deal(actions, x), axis=1)
|
|
|
|
if len(actions) > 0:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
|
|
log.info(f"完成同步售租比、人口数至206 {index_name}")
|
|
|
|
|
|
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
if pd.notna(sys_city) and "," in sys_city:
|
|
cities = sys_city.split(",")
|
|
for city in cities:
|
|
sync(city)
|
|
else:
|
|
sync(sys_city) |