【问题标题】:Does scala has "Options" to parse command line arguments in spark-submit just like in Java? [duplicate]scala 是否有“选项”来解析 spark-submit 中的命令行参数,就像在 Java 中一样? [复制]
【发布时间】:2019-10-10 22:20:04
【问题描述】:

为了在使用 spark-submit 时解析命令行参数:

SPARK_MAJOR_VERSION=2 spark-submit --class com.partition.source.Pickup --master=yarn --conf spark.ui.port=0000 --driver-class-path /home/hdpusr/jars/postgresql-42.1.4.jar --conf spark.jars=/home/hdpusr/jars/postgresql-42.1.4.jar,/home/hdpusr/jars/postgresql-42.1.4.jar --executor-cores 4 --executor-memory 4G --keytab /home/hdpusr/hdpusr.keytab --principal hdpusr@DEVUSR.COM --files /usr/hdp/current/spark2-client/conf/hive-site.xml,testconnection.properties --name Spark_APP --conf spark.executor.extraClassPath=/home/hdpusr/jars/greenplum.jar sparkload_2.11-0.1.jar ORACLE

我正在传递一个数据库名称:ORACLE,我在代码中将其解析为

  def main(args: Array[String]): Unit = {
    val dbtype   = args(0).toString
    .....
  }

有没有一种方法可以给它起一个类似“--dbname”的名称,然后在 spark-submit 中检查该选项以获取该选项的值? 例如:

SPARK_MAJOR_VERSION=2 spark-submit --class com.partition.source.Pickup --master=yarn --conf spark.ui.port=0000 --driver-class-path /home/hdpusr/jars/postgresql-42.1.4.jar --conf spark.jars=/home/hdpusr/jars/postgresql-42.1.4.jar,/home/hdpusr/jars/postgresql-42.1.4.jar --executor-cores 4 --executor-memory 4G --keytab /home/hdpusr/hdpusr.keytab --principal hdpusr@DEVUSR.COM --files /usr/hdp/current/spark2-client/conf/hive-site.xml,testconnection.properties --name Spark_APP --conf spark.executor.extraClassPath=/home/hdpusr/jars/greenplum.jar sparkload_2.11-0.1.jar --dbname ORACLE

在 Java 中有两个包可以用来做同样的事情:

    import org.apache.commons.cli.Option;
    import org.apache.commons.cli.Options;
    public static void main(String[] args) {
       Options options = new Options();
       Option input = new Option("s", "ssn", true, "source system names");
       input.setRequired(false);
       options.addOption(input);
       CommandLineParser parser = new DefaultParser();
       HelpFormatter formatter  = new HelpFormatter();
       CommandLine cmd       = null;
       try {
            cmd = parser.parse(options, args);
            if(cmd.hasOption("s")) {            // Checks if there is an argument '--s' in the CLI. Runs the Recon only for the received SSNs.
            }
       } catch(ParseException e) {
          formatter.printHelp("utility-name", options);
          e.printStackTrace();
          System.exit(1);
       } catch(Exception e) {
         e.printStackTrace();
       }
    }

谁能告诉我是否可以命名命令行参数并相应地解析它们?

【问题讨论】:

  • 只需将commons-cli 添加到您的依赖项中,它应该可以正常工作。您可以毫无问题地在 Scala 中使用 Java 依赖项。不过,在旁注中,您为什么不能使用 sparkConf 作为您的选项?
  • 知道了..将使用该依赖项并将其发布回结果。

标签: scala apache-spark


【解决方案1】:

例如,如果您使用--dbname=ORACLE

val pattern = """--dbname=(.*)""".r
  val params = args.map {
    case pattern(pair, _) => pair
    case arg => throw new ConfigException.Generic(s"""unable to parse command-line argument "$arg"""")
}

\s 匹配空格,你可以使用它来创建--dbname ORACLE,但是如果你只使用一个字符串会更容易。

Here你可以看到所有的可能性。

【讨论】:

    【解决方案2】:

    如果我们不指定密钥名称,我们可以在密钥名称前面加上spark.,在这种情况下为spark.dbname,并传递一个类似spark-submit --conf spark.dbname=<> ....的conf参数或将其添加到spark-defaults.conf
    在用户代码中,我们可以访问密钥为sparkContext.getConf.get("spark.dbname")

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 1970-01-01
      • 2014-02-03
      • 2013-11-08
      • 2017-08-31
      • 2012-11-16
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多