Can fix-represent with a named argument?

I know that I can pass an argument to the main function

spark-submit com.xxx.test 1 2

and get the argument:

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

but I want to know if there is a way to pass a named argument, for example:

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

and how to get this named argument in main.scala?

+4
source share
2 answers

you can write your own custom class that parses key-based input arguments, as shown below:

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
  }
}

Then you can use the methods in your spark application

val usage = "Usage:  [--citys] [--num]"
val optsMap = CommandLineUtil.getOpts(args, usage)
val citysValue = optsMap("citys")
val numValue = optsMap("num")

You can improvise CommandLineUtilaccording to your requirements.

+3
source

No.

, , .

, " ", ( , ).

0

Source: https://habr.com/ru/post/1687861/


All Articles