Smile - 模型持久化 - 如何将模型写入HDFS?

Smile - Model Persistence - How to write models to HDFS?

我正在尝试在使用 Spark 和 HDFS 的 Scala 项目中使用 Smile。为了模型的可重用性,我需要将它们写入 HDFS。

现在我正在使用 write 对象,预先检查路径是否存在,如果不存在则创建它(否则它会抛出 FileNotFoundException):

import java.nio.file.Paths

val path: String = "hdfs:/my/hdfs/path"
val outputPath: Path = Paths.get(path)
val outputFile: File = outputPath.toFile
if(!outputFile.exists()) {
  outputFile.getParentFile().mkdirs();  // This is a no-op if it exists
  outputFile.createNewFile();
}
write(mySmileModel, path)

但这会在本地创建路径“hdfs:/my/hdfs/path”并将模型写入其中,而不是实际写入 HDFS。
请注意,使用火花模型及其 save 方法有效:

mySparkModel.save("hdfs:/my/hdfs/path")

因此我的问题是:如何将 Smile 模型写入 HDFS?
同样,如果我设法将模型写入 HDFS,我可能也会想知道如何从 HDFS 读取模型。

谢谢!

最后,我通过为我的包装器编写自己的保存方法解决了我的问题class,大致相当于:

import org.apache.hadoop.fs.{FSDataInputStream, FSDataOutputStream, FileSystem, Path}
import org.apache.hadoop.conf.Configuration
import java.io.{ObjectInputStream, ObjectOutputStream}

val path: String = /my/hdfs/path
val file: Path = new Path(path)
val conf: Configuration = new Configuration()
val hdfs: FileSystem = FileSystem.get(new URI(path), conf)
val outputStream: FSDataOutputStream = hdfs.create(file)
val objectOutputStream: ObjectOutputStream = new ObjectOutputStream(outputStream)
objectOutputStream.writeObject(model)
objectOutputStream.close()

同样,为了加载保存的模型,我编写了一个大致执行以下操作的方法:

val conf: Configuration = new Configuration()
val path: String = /my/hdfs/path
val hdfs: FileSystem = FileSystem.get(new URI(path), conf)
val inputStream: FSDataInputStream = hdfs.open(new Path(path))
val objectInputStream: ObjectInputStream = new ObjectInputStream(inputStream)
val model: RandomForest = objectInputStream.readObject().asInstanceOf[RandomForest]