目的
有些简单的模型,希望直接python训练完模型,放在hdfs上,预测时,scala或者sql做特征,再用python进行预测,预测结果插入hive表。那么就涉及到如何加载hdfs的模型,遇到了些坑,只给出目前的解决方案,不求甚解~
方法
- 连接hdfs的python库有多个,用下来,hdfs安装相对方便,使用尚能满足
- 持久化模型也有多种方法,pickle、joblib、pmml,joblib加载模型报错,暂时没找到解决方案,pickle可用
from hdfs import InsecureClient
root_path = "/user/hive"
# hadoop3.x版本端口号用9870
client_hdfs = InsecureClient(url="ip:port",root=root_path)
import pickle
with client_hdfs.write('/user/hive/dt_model.pickle',overwrite=True) as writer:
# 模型保存
pickle.dump(dt_model, writer)
with client_hdfs.read('/user/hive/dt_model.pickle') as reader:
load_model =pickle.load(reader)
reader.close()
代码就这些
Ref
[1] https://pypi.org/project/hdfs/2.1.0/
2021-08-25 于南京市江宁区九龙湖