Spark使用技巧备忘记录

Spark 历史记录

404X 端口只能看到正在运行App的情况,并不能看到 App 运行结束后的各项数据, 此时就需要使用到 Spark History Server 工具

Spark History Server 通过配置,可以在Spark应用程序运行完成后,将应用程序的运行信息写入指定目录,并且Spark History Server可以将这些信息装在并以Web形式供用户浏览.

HistoryServer 临时配置

HistoryServer记录

如果是临时的使用,可以直接使用下面配置进行设置

spark.eventLog.enabled          true                    # 是否采用日志记录
spark.eventLog.dir              hdfs://tmp/spark-logs   # 日志记录位置
spark.eventLog.compress         true                    # 是否启动压缩记录, 默认使用snappy

如下即为示例

spark-submit \
--master yarn \
--deploy-mode cluster \
--executor-memory 1G \
--total-executor-cores 2 \
--class org.apache.spark.examples.SparkPi \
--conf spark.eventLog.enabled=true \
--conf spark.eventLog.dir=hdfs://tmp/spark-logs \
${SPARK_HOME}/examples/jars/spark-examples_2.11-2.4.6.jar \
100

HistoryServer读取

export SPARK_HISTORY_OPTS="-Dspark.history.ui.port=2929 -Dspark.history.retainedApplications=3 -Dspark.history.fs.logDirectory=hdfs://tmp/SparkHistoryLog"       # 配置日志位置等信息
${SPARK_HOME}/sbin/start-history-server.sh          # 启动 HistoryServer
${SPARK_HOME}/sbin/stop-history-server.sh           # 关闭 HistoryServer

如上启动的实际命令为

java \
-cp ${SPARK_HOME}/conf/:/${SPARK_HOME}/jars/* \
-Dspark.history.ui.port=18080 \
-Dspark.history.retainedApplications=3 \
-Dspark.history.fs.logDirectory=hdfs://tmp/spark-logs \
-Xmx1g org.apache.spark.deploy.history.HistoryServer

此时访问 http://<server-url>:18080 即可查看

HistoryServer 常驻配置

  1. 配置 spark-defaults.conf

${SPARK_HOME}/conf 目录下的 spark-defaults.conf 配置文件中新增如下配置:

spark.eventLog.enabled              true
spark.eventLog.dir                  hdfs://HadoopServer1:8020/tmp/spark-logs
spark.yarn.historyServer.address    HadoopServer1:18080
  1. 配置 spark-env.sh

${SPARK_HOME}/conf 目录下的 spark-env.sh 配置文件中新增如下配置:

export SPARK_HISTORY_OPTS="-Dspark.history.ui.port=18080 -Dspark.history.fs.logDirectory=hdfs://hadoopSvr1:8020/tmp/spark-logs -Dspark.history.fs.cleaner.enabled=true"

pyspark 操作 hdfs

## 利用 hadoop 命令进行文件获取
hadoop_cmd = f"""
hadoop fs -get {hdfs_url}/{hdfs_path}
"""
## 利用 hadoop 命令进行文件保存
hadoop_cmd = f"""
hadoop fs -put -f {local_path} {hdfs_url}/{hdfs_path}
"""
os.system(hadoop_cmd)
  1. 利用 py4j 调用hadoop相关包操作

FileSystem API

URI           = sc._gateway.jvm.java.net.URI
Path          = sc._gateway.jvm.org.apache.hadoop.fs.Path
FileSystem    = sc._gateway.jvm.org.apache.hadoop.fs.FileSystem
Configuration = sc._gateway.jvm.org.apache.hadoop.conf.Configuration
fs = FileSystem.get(URI(hdfs_url), Configuration())
fs.copyToLocalFile(Path(hdfs_path), Path(local_path))
fs.copyFromLocalFile(Path(local_path), Path(hdfs_path))

参考资料


Spark使用技巧备忘记录
https://www.windism.cn/2035267275.html
作者
windism
发布于
2020年10月22日
许可协议