如何从提交的 spark 应用程序步骤中获取 AWS EMR 集群 ID 和步骤 ID [英] How to get AWS EMR cluster id and step id from inside the spark application step submitted

查看:44
本文介绍了如何从提交的 spark 应用程序步骤中获取 AWS EMR 集群 ID 和步骤 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:
我正在 AWS EMR 中运行 Spark Scala 作业.现在,我的工作转储了该应用程序独有的一些元数据.现在为了倾销,我正在写位置s3://bucket/key/"其中 ApplicationId 是 val APPLICATION_ID: String = getSparkSession.sparkContext.getConf.getAppId

现在基本上有一种方法可以在 s3 位置写入类似s3://bucket/key/<emr_cluster_id>_<emr_step_id>"的内容.我如何从 spark Scala 应用程序内部获取集群 ID 和步骤 ID.

Now basically is there a way to write at s3 location something like "s3://bucket/key/<emr_cluster_id>_<emr_step_id>". How can i get the cluster id and step id from inside the spark Scala application.

以这种方式编写将帮助我调试并帮助我到达基于集群并调试日志.

Writing in this way will help me debug and help me in reaching the cluster based and debug the logs.

除了阅读/mnt/var/lib/info/job-flow.json"之外还有什么方法吗??

Is there any way other than reading the "/mnt/var/lib/info/job-flow.json" ?

PS:我是 spark、scala 和 emr 的新手.如果这是一个明显的查询,请提前道歉.

PS: I am new to spark, scala and emr . Apologies in advance if this is an obvious query.

推荐答案

在 EMR 上使用 PySpark,EMR_CLUSTER_IDEMR_STEP_ID 可用作环境变量(在 emr-5.30 上确认.1).

With PySpark on EMR, EMR_CLUSTER_ID and EMR_STEP_ID are available as environment variables (confirmed on emr-5.30.1).

它们可以在代码中使用如下:

They can be used in code as follows:

import os
emr_cluster_id = os.environ.get('EMR_CLUSTER_ID')
emr_step_id = os.environ.get('EMR_STEP_ID')

我无法测试,但以下类似的代码应该可以在 Scala 中使用.

I can't test but the following similar code should work in Scala.

val emr_cluster_id = sys.env.get("EMR_CLUSTER_ID")
val emr_step_id = sys.env.get("EMR_STEP_ID")

由于 sys.env 只是一个 Map[String, String],它的 get 方法返回一个 Option[String],如果这些环境变量不存在,它不会失败.如果您想引发异常,您可以使用 sys.env("EMR_x_ID")

Since sys.env is simply a Map[String, String] its get method returns an Option[String], which doesn't fail if these environment variables don't exist. If you want to raise an Exception you could use sys.env("EMR_x_ID")

EMR_CLUSTER_IDEMR_STEP_ID 变量在环境选项卡下的 Spark History Server UI 中可见,以及其他可能感兴趣的变量.

The EMR_CLUSTER_ID and EMR_STEP_ID variables are visible in the Spark History Server UI under the Environment tab, alongside with other variables that may be of interest.

这篇关于如何从提交的 spark 应用程序步骤中获取 AWS EMR 集群 ID 和步骤 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆