2017-10-20 111 views
4

我知道我可以通过可以用命名参数spark-submit?

spark-submit com.xxx.test 1 2 

传递参数主功能得到论证:

def main(args: Array[String]): Unit = { 
    // 读取参数 
    var city = args(0) 
    var num = args(1) 

,但我想知道有没有通过命名参数一样的路径:

spark-submit com.xxx.test --citys=1 --num=2 

以及如何在main.scala中获取这个命名参数?

回答

3

,你可以写一个解析输入参数的基础上,重点类似下面你自己的自定义类:

object CommandLineUtil { 

    def getOpts(args: Array[String], usage: String): collection.mutable.Map[String, String] = { 
    if (args.length == 0) { 
     log.warn(usage) 
     System.exit(1) 
    } 

    val (opts, vals) = args.partition { 
     _.startsWith("-") 
    } 

    val optsMap = collection.mutable.Map[String, String]() 
    opts.map { x => 
     val pair = x.split("=") 
     if (pair.length == 2) { 
     optsMap += (pair(0).split("-{1,2}")(1) -> pair(1)) 
     } else { 
     log.warn(usage) 
     System.exit(1) 
     } 
    } 

    optsMap 
    } 
} 

然后你可以用与这些方法在你的火花应用

你可即兴创作CommandLineUtil按照您的要求

0

编号

正如你可以在Documentation中看到的,你只需传递你的应用程序的参数,然后你就可以处理它们。所以,如果你想拥有“命名参数”,那么你应该在你的代码中实现它(我的意思是它将是自定义的)。

相关问题