如何在 Spark 中获取地图任务的 ID? [英] How to get ID of a map task in Spark?
本文介绍了如何在 Spark 中获取地图任务的 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 Spark 中获取地图任务的 ID?例如,如果每个地图任务调用一个用户定义的函数,我是否可以从该用户定义的函数中获取该地图任务的 ID?
Is there a way to get ID of a map task in Spark? For example if each map task calls a user defined function, can I get the ID of that map task from whithin that user defined function?
推荐答案
我不知道你说的地图任务 ID 是什么意思,但你可以使用 TaskContext
访问任务信息:
I am not sure what you mean by ID of map task but you can access task information using TaskContext
:
import org.apache.spark.TaskContext
sc.parallelize(Seq[Int](), 4).mapPartitions(_ => {
val ctx = TaskContext.get
val stageId = ctx.stageId
val partId = ctx.partitionId
val hostname = java.net.InetAddress.getLocalHost().getHostName()
Iterator(s"Stage: $stageId, Partition: $partId, Host: $hostname")
}).collect.foreach(println)
Spark 2.2.0 中的 PySpark 中添加了类似的功能(SPARK-18576):
A similar functionality has been added to PySpark in Spark 2.2.0 (SPARK-18576):
from pyspark import TaskContext
import socket
def task_info(*_):
ctx = TaskContext()
return ["Stage: {0}, Partition: {1}, Host: {2}".format(
ctx.stageId(), ctx.partitionId(), socket.gethostname())]
for x in sc.parallelize([], 4).mapPartitions(task_info).collect():
print(x)
这篇关于如何在 Spark 中获取地图任务的 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文