版本修改
This commit is contained in:
@@ -6,6 +6,10 @@ import pandas as pd
|
|||||||
from elasticsearch import helpers
|
from elasticsearch import helpers
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
import os
|
||||||
|
# 🛡️ 极其重要:必须在导入 goojia_utils 之前禁用 ES 8.x 的兼容性 Content-Type 头部请求!
|
||||||
|
# 这样客户端就会使用普通的 'application/json' 头部,彻底解决低版本 Elasticsearch 报错 406 的故障。
|
||||||
|
os.environ["ELASTIC_CLIENT_APIVERSIONING"] = "0"
|
||||||
|
|
||||||
flag = False
|
flag = False
|
||||||
for province in areautils.get_all_province():
|
for province in areautils.get_all_province():
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ def choose_task(**context):
|
|||||||
return ['office_city_region_rent_calc', 'office_base_rent_calc']
|
return ['office_city_region_rent_calc', 'office_base_rent_calc']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
with DAG(
|
with DAG(
|
||||||
dag_id="office_rent_dag",
|
dag_id="office_rent_dag",
|
||||||
# 每月25日1点运行
|
# 每月25日1点运行
|
||||||
@@ -41,7 +40,7 @@ with DAG(
|
|||||||
branch_task = BranchPythonOperator(
|
branch_task = BranchPythonOperator(
|
||||||
task_id='choose_branch',
|
task_id='choose_branch',
|
||||||
python_callable=choose_task,
|
python_callable=choose_task,
|
||||||
provide_context=True
|
# 🛡️ 已移除 provide_context=True,防止 Airflow 3.3.0 报错崩溃
|
||||||
)
|
)
|
||||||
# 任务1:城市区域租金计算
|
# 任务1:城市区域租金计算
|
||||||
t1 = BashOperator(
|
t1 = BashOperator(
|
||||||
@@ -52,7 +51,6 @@ with DAG(
|
|||||||
t2 = PythonOperator(
|
t2 = PythonOperator(
|
||||||
task_id='office_base_rent_calc',
|
task_id='office_base_rent_calc',
|
||||||
python_callable=calc,
|
python_callable=calc,
|
||||||
provide_context=True
|
# 🛡️ 已移除 provide_context=True,防止 Airflow 3.3.0 报错崩溃
|
||||||
)
|
)
|
||||||
branch_task >> [t1, t2]
|
branch_task >> [t1, t2]
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import datetime
|
|||||||
import urllib3
|
import urllib3
|
||||||
from goojia_utils import nextcloudutils, dingutils
|
from goojia_utils import nextcloudutils, dingutils
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
# 禁用 InsecureRequestWarning
|
# 禁用 InsecureRequestWarning
|
||||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
import sys # 新增导入系统模块用于安全退出
|
||||||
from goojia_utils import MAIN_MONGO_CLIENT
|
from goojia_utils import MAIN_MONGO_CLIENT
|
||||||
from goojia_utils import dateutils
|
from goojia_utils import dateutils
|
||||||
from sklearn.mixture import GaussianMixture
|
from sklearn.mixture import GaussianMixture
|
||||||
@@ -11,12 +12,26 @@ from pymongo import InsertOne, DeleteMany, ReplaceOne, UpdateOne
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# ================== 🛡️ 安全运行入口 ==================
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
district_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'district_stat')
|
district_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'district_stat')
|
||||||
district_analy_data_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'district_analy_data')
|
district_analy_data_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'district_analy_data')
|
||||||
server_area_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'server_area')
|
server_area_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'server_area')
|
||||||
weekend = list(district_stat_conn.find().sort([("weekend", -1)]).limit(1))[0]['weekend']
|
|
||||||
|
# 1. 延迟进行重度 MongoDB 查询
|
||||||
|
weekend_list = list(district_stat_conn.find().sort([("weekend", -1)]).limit(1))
|
||||||
|
if not weekend_list:
|
||||||
|
logger.error("在 district_stat 集合中未找到有效的 weekend 记录,程序退出。")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
weekend = weekend_list[0]['weekend']
|
||||||
level_map = {"1": "S", "2": "A", "3": "B", "4": "C", "5": "D"}
|
level_map = {"1": "S", "2": "A", "3": "B", "4": "C", "5": "D"}
|
||||||
|
|
||||||
logger.info(f"开始计算楼盘评级 weekend:{dateutils.format_date(weekend, '%Y-%m-%d')}")
|
logger.info(f"开始计算楼盘评级 weekend:{dateutils.format_date(weekend, '%Y-%m-%d')}")
|
||||||
|
|
||||||
|
# 2. 遍历各城市开展机器学习模型评级
|
||||||
for server_area in server_area_conn.find({'display': {'$ne': '全国'}}, {"display": 1}):
|
for server_area in server_area_conn.find({'display': {'$ne': '全国'}}, {"display": 1}):
|
||||||
city = server_area["display"]
|
city = server_area["display"]
|
||||||
data = pd.DataFrame(
|
data = pd.DataFrame(
|
||||||
@@ -26,7 +41,8 @@ for server_area in server_area_conn.find({'display': {'$ne': '全国'}}, {"displ
|
|||||||
continue
|
continue
|
||||||
data['city'] = city
|
data['city'] = city
|
||||||
logger.info(f"开始计算{city}楼盘评级 weekend:{dateutils.format_date(weekend, '%Y-%m-%d')}")
|
logger.info(f"开始计算{city}楼盘评级 weekend:{dateutils.format_date(weekend, '%Y-%m-%d')}")
|
||||||
# 高斯混合模型
|
|
||||||
|
# 高斯混合模型训练及评估(延迟到真正执行时运行,避免污染解析阶段)
|
||||||
gmm = GaussianMixture(n_components=5, covariance_type='tied', max_iter=200, n_init=10).fit(
|
gmm = GaussianMixture(n_components=5, covariance_type='tied', max_iter=200, n_init=10).fit(
|
||||||
data[["avg_price"]])
|
data[["avg_price"]])
|
||||||
data.loc[:, 'cluster'] = gmm.predict(data[["avg_price"]])
|
data.loc[:, 'cluster'] = gmm.predict(data[["avg_price"]])
|
||||||
@@ -34,19 +50,22 @@ for server_area in server_area_conn.find({'display': {'$ne': '全国'}}, {"displ
|
|||||||
data.loc[:, "target"] = data['price_mean_cluster'].rank(method='dense', ascending=False).astype(int)
|
data.loc[:, "target"] = data['price_mean_cluster'].rank(method='dense', ascending=False).astype(int)
|
||||||
data = data.drop(columns=['cluster', 'price_mean_cluster'], axis=1)
|
data = data.drop(columns=['cluster', 'price_mean_cluster'], axis=1)
|
||||||
data["community_rating"] = data['target'].apply(lambda x: level_map[str(x)])
|
data["community_rating"] = data['target'].apply(lambda x: level_map[str(x)])
|
||||||
|
|
||||||
opts = []
|
opts = []
|
||||||
for item in data.to_dict('records'):
|
for item in data.to_dict('records'):
|
||||||
opts.append(UpdateOne({"city": city, "region": item["region"], "district_name": item["district_name"]},
|
opts.append(UpdateOne({"city": city, "region": item["region"], "district_name": item["district_name"]},
|
||||||
{"$set": {"community_rating": item["community_rating"]}}, upsert=True
|
{"$set": {"community_rating": item["community_rating"]}}, upsert=True
|
||||||
))
|
))
|
||||||
# 按照每 1000 条分割 updates 列表
|
|
||||||
|
# 按照每 1000 条分割 updates 列表并执行 Bulk Write
|
||||||
batch_size = 1000
|
batch_size = 1000
|
||||||
batches = [opts[i:i + batch_size] for i in range(0, len(opts), batch_size)]
|
batches = [opts[i:i + batch_size] for i in range(0, len(opts), batch_size)]
|
||||||
# 逐个批次执行批量更新操作
|
|
||||||
cnt = 0
|
cnt = 0
|
||||||
for batch in batches:
|
for batch in batches:
|
||||||
cnt = cnt + 1
|
cnt = cnt + 1
|
||||||
logger.info(f"{city}楼盘评级更新数据库, 进度:{cnt}/{len(batches)}")
|
logger.info(f"{city}楼盘评级更新数据库, 进度:{cnt}/{len(batches)}")
|
||||||
result = district_analy_data_conn.bulk_write(batch)
|
result = district_analy_data_conn.bulk_write(batch)
|
||||||
print(f"当前批次更新的文档数量: {result.modified_count}")
|
print(f"当前批次更新的文档数量: {result.modified_count}")
|
||||||
|
|
||||||
logger.info(f"楼盘评级计算结束{dateutils.format_date(datetime.datetime.utcnow(), '%Y-%m-%d')}")
|
logger.info(f"楼盘评级计算结束{dateutils.format_date(datetime.datetime.utcnow(), '%Y-%m-%d')}")
|
||||||
Reference in New Issue
Block a user