DEV Community

Discussion on: Spark : Replace collect()[][]

Collapse
 
dazfuller profile image
Darren Fuller

So that looks like you're after the value in the first column of the first row. If that's the case then you could use the following.

spark.sql("SELECT col1, col2 FROM table1 WHERE id=2").first()[0]

That will return the first row as a Row object which you can then access via index. You should find it works better as well as collect will pull all of the data to the driver before you attempt to access a single row