spark-submit,如何指定log4j.properties

use*_*666 9 apache-spark

在spark-submit中,如何指定log4j.properties?

这是我的剧本。我已经尝试了所有组合,甚至只使用一个本地节点。但似乎未加载log4j.properties,所有调试级别的信息都已转储。

current_dir=/tmp
DRIVER_JAVA_OPTIONS="-Dlog4j.configuration=file://${current_dir}/log4j.properties "

spark-submit \
--conf "spark.driver.extraClassPath=$current_dir/lib/*"  \
--conf "spark.driver.extraJavaOptions=-Djava.security.krb5.conf=${current_dir}/config/krb5.conf -Djava.security.auth.login.config=${current_dir}/config/mssqldriver.conf" \
--conf "spark.executor.extraJavaOptions=-Dlog4j.configuration=file://${curent_dir}/log4j.properties " \
--class "my.AppMain" \
--files ${current_dir}/log4j.properties \
--master local[1] \
--driver-java-options "$DRIVER_JAVA_OPTIONS" \
--num-executors 4 \
--driver-memory 16g \
--executor-cores 10 \
--executor-memory 6g \
$current_dir/my-app-SNAPSHOT-assembly.jar
Run Code Online (Sandbox Code Playgroud)

log4j属性:

log4j.rootCategory=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.additivity.org=false

log4j.logger.org=WARN
parquet.hadoop=WARN
log4j.logger.com.barcap.eddi=WARN
log4j.logger.com.barcap.mercury=WARN
log4j.logger.yarn=WARN
log4j.logger.io.netty=WARN
log4j.logger.Remoting=WARN   
log4j.logger.org.apache.hadoop=ERROR

# this disables the table creation logging which is so verbose
log4j.logger.hive.ql.parse.ParseDriver=WARN

# this disables pagination nonsense when running in combined mode
log4j.logger.com.barcap.risk.webservice.servlet.PaginationFactory=WARN
Run Code Online (Sandbox Code Playgroud)

Tho*_*aux 11

注意 Spark worker 不是您的 Java 应用程序,因此您不能使用类路径中的 log4j.properties 文件。

要了解 YARN 上的 Spark 如何读取log4j.properties文件,您可以使用以下log4j.debug=true标志:

spark.executor.extraJavaOptions=-Dlog4j.debug=true
Run Code Online (Sandbox Code Playgroud)

大多数情况下,错误是在工作线程 YARN 容器中找不到/无法使用该文件。有一个非常有用的 Spark 指令允许共享文件:--files.

--files "./log4j.properties"
Run Code Online (Sandbox Code Playgroud)

这将使您的所有驱动程序/工人都可以使用此文件。添加 Java 额外选项:

-Dlog4j.configuration=log4j.properties
Run Code Online (Sandbox Code Playgroud)

等等!

log4j: Using URL [file:/var/log/ambari-server/hadoop/yarn/local/usercache/hdfs/appcache/application_1524817715596_3370/container_e52_1524817715596_3370_01_000002/log4j.properties] for automatic log4j configuration.
Run Code Online (Sandbox Code Playgroud)


Ata*_*ais 10

如何传递本地log4j.properties文件

正如我从你的脚本中看到的,你想要:

  1. 将本地传递log4j.properties给执行者
  2. 使用此文件进行节点配置。

请注意有关--files设置的两点:

  1. 上传到 spark-cluster 的文件--files将在 executor 工作区的根目录下可用,因此不需要在file:log4j.properties.
  2. 中列出的文件--files必须提供绝对路径!

现在修复您的代码段非常容易:

current_dir=/tmp
log4j_setting="-Dlog4j.configuration=file:log4j.properties"

spark-submit \
...
--conf "spark.driver.extraJavaOptions=${log4j_setting}" \
--conf "spark.executor.extraJavaOptions=${log4j_setting}" \
--class "my.AppMain" \
--files ${current_dir}/log4j.properties \
...
$current_dir/my-app-SNAPSHOT-assembly.jar
Run Code Online (Sandbox Code Playgroud)

需要更多?

如果您想了解在使用时配置日志记录的其他方式spark-submit,请访问我的其他详细答案:https : //stackoverflow.com/a/55596389/1549135


小智 5

补充一下,可以直接通过spark-submit传递conf,不需要修改默认的conf文件

--conf spark.driver.extraJavaOptions=-Dlog4j.configuration=file:///export/home/siva/log4j.properties
Run Code Online (Sandbox Code Playgroud)

我运行了以下命令,效果很好

/usr/hdp/latest/spark2/bin/spark-submit --master local[*] --files ~/log4j.properties --conf spark.sql.catalogImplementation=hive --conf spark.driver.extraJavaOptions=-Dlog4j.configuration=file:///export/home/siva/log4j.properties ~/SCD/spark-scd-assembly-1.0.jar test_run
Run Code Online (Sandbox Code Playgroud)

注意:如果您在conf文件中配置了额外的Java选项,只需追加并提交


Ehu*_*Lev 3

  1. 将spark-defaults.conf复制到新的app-spark-defaults.conf
  2. 将 -Dlog4j.configuration=file://log4j.properties 添加到 app-spark-defaults.conf 中的 spark.driver.extraJavaOptions 中。例如:

    Spark.driver.extraJavaOptions -XXOther_flag -Dlog4j.configuration=file://log4j.properties

  3. 使用 --properties-file 将 Spark 运行到新的 conf 文件中。例如:
    spark-submit --properties-file app-spark-defaults.conf --class my.app.class --master yarn --deploy-mode client ~/my-jar.jar