spark-defaults.conf 里的参数被加载的时机

本文探讨了Spark中spark-defaults.conf配置文件的加载时机与过程。通过深入分析源代码,揭示了该配置文件如何被Spark应用程序读取并应用于实际运行环境中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在调试Spark参数时,发现里的参数是默认值,把saprk/conf目录加到source也没有用。我查了一年,spark-shell命令执行时只加载spark-env.sh文件,那么spark-defaults.conf里的内容是什么时候加载的。


在org.apache.spark.launcher.CommandBuilderUtils.java 里有spark-defaults.conf的一个常量DEFAULT_PROPERTIES_FILE等于default-defaults.conf

 static final String DEFAULT_PROPERTIES_FILE = "spark-defaults.conf";
在org.apache.spark.launcher.AbstractCommandBuilder.java 里的loadPropertiesFile()里读取DEFAULT_PROPERTIES_FILE文件

/**
   * Loads the configuration file for the application, if it exists. This is either the
   * user-specified properties file, or the spark-defaults.conf file under the Spark configuration
   * directory.
   */
  private Properties loadPropertiesFile() throws IOException {
    Properties props = new Properties();
    File propsFile;
    if (propertiesFile != null) {
      propsFile = new File(propertiesFile);
      checkArgument(propsFile.isFile(), "Invalid properties file '%s'.", propertiesFile);
    } else {
      propsFile = new File(getConfDir(), DEFAULT_PROPERTIES_FILE);
    }

    if (propsFile.isFile()) {
      FileInputStream fd = null;
      try {
        fd = new FileInputStream(propsFile);
        props.load(new InputStreamReader(fd, "UTF-8"));
        for (Map.Entry<Object, Object> e : props.entrySet()) {
          e.setValue(e.getValue().toString().trim());
        }
      } finally {
        if (fd != null) {
          try {
            fd.close();
          } catch (IOException e) {
            // Ignore.
          }
        }
      }
    }

    return props;
  }

然后laodPropertiesFile()方法被getEffectiveConfig方法使用。

Map<String, String> getEffectiveConfig() throws IOException {
    if (effectiveConfig == null) {
      effectiveConfig = new HashMap<>(conf);
      Properties p = loadPropertiesFile();
      for (String key : p.stringPropertyNames()) {
        if (!effectiveConfig.containsKey(key)) {
          effectiveConfig.put(key, p.getProperty(key));
        }
      }
    }
    return effectiveConfig;
  }


AbstractCommandBuilder 被以下地方使用。

./launcher/src/main/java/org/apache/spark/launcher/SparkSubmitCommandBuilder.java:class SparkSubmitCommandBuilder extends AbstractCommandBuilder {
./launcher/src/main/java/org/apache/spark/launcher/SparkClassCommandBuilder.java:class SparkClassCommandBuilder extends AbstractCommandBuilder {
./launcher/src/main/java/org/apache/spark/launcher/Main.java:    AbstractCommandBuilder builder;








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值