Thursday, 17 September 2015

Config File and CliArguments Parser in Scala

1. Read Config files

application.conf supports nested structure and list.

JobConfig {

  inputPath : "xxx",
  outputPath: "xxx"


  TaskConfig {
     inputPath : "yyy",
  outputPath: "yyy"
  }

To parse the configs in application.conf file,


import scala.collection.JavaConverters._

val jobConfig = config.getConfig(jobName)
val taskConfig =  jobConfig.getConfig(taskName)
val inputList = taskConfig.getStringList("inputPath").asScala
//if application.con doesn't contain the tag, find from java system properties
val prop: Option[String] = Try(job.getString("auth.username")).toOption
    .orElse(sys.props.get("spark.cassandra.auth.username"))


2. Parse Cli Arguments


case class Config(foo: Int = -1, configFile: String = null)

trait CliArgParser{

  def parseCli(
                appClassPath: String,
                appHeader: String
                ) = new OptionParser[CliArgs](appClassPath) {
    head(appHeader)
  opt[Int]('f', "foo")
   .action { (x, c) =>
    c.copy(foo = x) } text("foo is an integer property")
  opt[String]('c', "configFile")
      .action { (x, c) => c.copy(configFile = x) }
      .text("configuration file")
    }

// parser.parse returns Option[C]

def getCliArgs(args: Array[String], cliDefaultParams: CliArgs = CliArgs()) =
    parseCli("appClassPath", "app").parse(args, cliDefaultParams) match {
      case Some(c) => c
      case None => throw Exception("Invalid CLI args")
    }


def main(args: Array[String]) {
 val cliArgs: CliArgs = getCliArgs(args)

    val config: Config = cliArgs.configFile match {
      case c:String => ConfigFactory.parseFile(new java.io.File(c))
      case _ => ConfigFactory.load()
    }
}

Reference:
https://github.com/typesafehub/config
https://github.com/scopt/scopt

No comments:

Post a Comment