45

I would like to display the entire Apache Spark SQL DataFrame with the Scala API. I can use the show() method:

myDataFrame.show(Int.MaxValue)

Is there a better way to display an entire DataFrame than using Int.MaxValue?

2
  • 5
    Try myDataFrame.show(false). Not sure if thats what you are looking for. Commented Sep 12, 2016 at 19:52
  • Use RDD.toLocalIterator(), as discussed in this SO post: stackoverflow.com/questions/21698443/… Commented Sep 28, 2016 at 18:13

7 Answers 7

78

It is generally not advisable to display an entire DataFrame to stdout, because that means you need to pull the entire DataFrame (all of its values) to the driver (unless DataFrame is already local, which you can check with df.isLocal).

Unless you know ahead of time that the size of your dataset is sufficiently small so that driver JVM process has enough memory available to accommodate all values, it is not safe to do this. That's why DataFrame API's show() by default shows you only the first 20 rows.

You could use the df.collect which returns Array[T] and then iterate over each line and print it:

df.collect.foreach(println)

but you lose all formatting implemented in df.showString(numRows: Int) (that show() internally uses).

So no, I guess there is no better way.

Sign up to request clarification or add additional context in comments.

2 Comments

df.toJSON.collect.foreach(println) is better
but doesn't collect also pull the entire DataFrame to the driver?
6

One way is using count() function to get the total number of records and use show(rdd.count()) .

Comments

4

Try with,

df.show(35, false)

It will display 35 rows and 35 column values with full values name.

Comments

2

As others suggested, printing out entire DF is bad idea. However, you can use df.rdd.foreachPartition(f) to print out partition-by-partition without flooding driver JVM (y using collect)

1 Comment

Can you please provide some sample code? Won't the print statements inside the f() function print to the workers' stdout, and not the driver/your shell session's stdout? See also stackoverflow.com/a/28804763/215945
1

Nothing more succinct than that, but if you want to avoid the Int.MaxValue, then you could use a collect and process it, or foreach. But, for a tabular format without much manual code, show is the best you can do.

Comments

0

In java I have tried it with two ways. This is working perfectly for me:

1.

data.show(SomeNo);

2.

data.foreach(new ForeachFunction<Row>() {
                public void call(Row arg0) throws Exception {
                    System.out.println(arg0);
                }
            });

Comments

-3

I've tried show() and it seems working sometimes. But sometimes not working, just give it a try:

println(df.show())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.