Scala 如何定义对象文件[类型](路径)



我想读取HDFS数据,但数据可能会保存为saveAsObject[(String,Int,SparseVector)]saveAsObject[(Int,String,Int)]等。

所以我想通过spark-submit"String,Int,SparseVector"等命令行参数传递给我的作业。

如何从方法saveAsObject[type]的命令行参数中获取enter code heretype

object Test2 {
def main(args: Array[String]): Unit = {
val conf=new org.apache.spark.SparkConf()
val sc = new org.apache.spark.SparkContext(conf)
var fmt = "Int,String,SparseVector"
if(args.size!=0){fmt=args(0)}
var fmt_arr=fmt.split(",")
type data_type=(matchClass(fmt_arr(0)),matchClass(fmt_arr(1)),matchClass(fmt_arr(2)))
val data = sc.objectFile[data_type]("")
}
def matchClass(str:String)={
str match {
case "String" =>  String
case "Int"    =>  Int
case "SparseVector" => SparseVector
case _ => throw new RuntimeException("unsupported type")
}
}
}

所有的配置条目,你可以把它们放在一个所谓的application.conf文件中!

https://github.com/lightbend/config

然后,您可以在执行 Spark 提交时读取此配置文件!查看此处有关如何将 application.conf 文件加载到应用程序中的一些示例。对于 Spark 应用程序,机制也应该是相同的!

https://github.com/joesan/plant-simulator/blob/master/app/com/inland24/plantsim/config/AppConfig.scala

最新更新