---
title: "Discussion on \"Spark RDD如何获取TaskId\" | Hashnode"
description: "Discussion on \"Spark RDD如何获取TaskId\". 利用TaskContext.getPartitionId()获取分区Id。\nrdd.partitionBy(partitioner)\n   .foreachPartition(iter->{\n      int id=TaskContext.getPartitionId();\n      StringBuilder sb = new StringBuilder();\n      sb.append(\"Pid=\").append(id);\n      while(iter.hasNext()){\n..."
url: "https://hashnode.com/posts/spark-rddtaskid/669cb43e6234faa99432c61b"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Spark RDD如何获取TaskId](https://xiaohan.hashnode.dev/spark-rddtaskid?utm_source=hashnode&utm_medium=feed)

利用TaskContext.getPartitionId()获取分区Id。 rdd.partitionBy(partitioner) .foreachPartition(iter->{ int id=TaskContext.getPartitionId(); StringBuilder sb = new StringBuilder(); sb.append("Pid=").append(id); while(iter.hasNext()){ ...

Jul 21, 2024 · 1 min read · xiaohan.hashnode.dev  
Read the article

-   [#spark](https://hashnode.com/tag/spark)

## Written by

[xiaohan](https://hashnode.com/@xiaohan)

@xiaohan

  
[View profile](https://hashnode.com/@xiaohan)

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
