Spark - extracting single value from DataFrame

2019-03-09 17:33发布

问题:

I have a Spark DataFrame query that is guaranteed to return single column with single Int value. What is the best way to extract this value as Int from the resulting DataFrame?

回答1:

You can use head

df.head().getInt(0)

or first

df.first().getInt(0)

Check DataFrame scala docs for more details



回答2:

This could solve your problem.

df.map{
    row => row.getInt(0)
}.first()