0

My dataframe which was created by reading a RDBMS table, has one column and only one value in it:

val sourceCols = spark.read.format("jdbc").option("url", hiveMetaConURL)
                                               .option("dbtable", "(select source_columns from base.spe_tab where tablename='base.forecast') as sCols")
                                               .option("user", metaUserName)
                                               .option("password", metaPassword)
                                               .load()

I tried it convert it to a String in the below way:

val sourceColDataTypes = sourceCols.rdd.map(_.mkString(",")).collect.foreach(println)

When I try to print it as:

sourceColDataTypes.foreach(println)

I don't see the content, instead I see:

[Ljava.lang.String;@1e489957

Is there a way I can use yield of Scala to get the value. Could anyone let me know how can I convert a row in a DataFrame to a String ?

2
  • you should be doing sourceCols.rdd.map(_.toSeq.mkString(",")).collect.foreach(println) Commented Jul 28, 2018 at 9:39
  • The foreach(println) is printing the value. But if I assign it to val data:String = sourceCols.rdd.map(_.toSeq.mkString(",")).collect.toString() and print "data", it still prints java.lang.String@30a6984c Commented Jul 28, 2018 at 9:52

2 Answers 2

1

To get the value you can use one of the following

sourceCols.map(_.getString(0)).collect.foreach(println)
sourceCols.map(_.toSeq.mkString(",")).collect.foreach(println)

If you just want to see the data you can use

sourceCols.show(false)

If you want a single string then you can get it as

println(sourceCols.map(_.getString(0)).collect.mkString(","))

Hope this helps!

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

Comments

0

You can try this directly on dataframe, There is no need to covert to rdd at all like this

df.select("value").collect.mkString(",").replaceAll("[\\[\\]]","")

Or

df.map(row=>row.getAs("value").toString).collect.mkString(",")

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.