scalaapache-sparkapache-spark-sqluser-defined-functionsqubole

How do I get the value without the square brackets


I have created a dataframe using Scala and Spark SQL. I wanted the first value from the table but I am getting it inside of square brackets []. Can I just get the value without the brackets?

Code:

val sigh = sqlContext.sql("""SELECT DISTINCT accountId FROM table""")
sigh.show()
sigh.select("accountId").first()

Results:

sigh: org.apache.spark.sql.DataFrame = [accountId: string]
+----------+
| accountId|
+----------+
|1370244502|
+----------+
res54: org.apache.spark.sql.Row = [1370244502]

Expected Results

sigh: org.apache.spark.sql.DataFrame = [accountId: string]
+----------+
| accountId|
+----------+
|1370244502|
+----------+
res54: org.apache.spark.sql.Row = 1370244502

Solution

  • first returns a Row object, and you can use getString method to extract elements from the row as string:

    sigh.select("accountId").first.getString(0)