# Printing elements of an RDD

Another common idiom is attempting to print out the elements of an RDD using&#x20;

rdd.foreach(println)&#x20;

or&#x20;

rdd.map(println).

On a single machine, this will generate the expected output and print all the RDD's elements. However, in cluster mode, the output to stdout being called by the executors is now writing to the executor’s stdout instead, not the one on the driver, so stdout on the driver won't show these!

To print all elements on the driver, one can use the collect() method to first bring the RDD to the driver node thus:&#x20;

rdd.collect().foreach(println).&#x20;

This can cause the driver to run out of memory, though, because collect() fetches the entire RDD to a single machine; It can also take long time for collect to complete.  if you only need to print a few elements of the RDD, a safer approach is to use the take():&#x20;

rdd.take(100).foreach(println).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://george-jen.gitbook.io/data-science-and-apache-spark/printing-elements-of-an-rdd.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
