0

I have ArrayList in Java which holds objects of type Row such as following

List<Row> Table = new ArrayList<Row>();

I need to convert this Table to Scala Seq in order to later convert it to data-frame. I tried the following without success

var TableScala:Seq[Row] = 
            Table.asScalaJavaConverters.asScalaBufferConverter(getTable).asScala

val newList = TableScala.map(row => new Tuple5(row(0), row(1), row(2), row(3), row(4)))
spark.createDataFrame(newList).toDF("userID")
0

1 Answer 1

1

From Scala 2.8.1, these conversions are made explicit using “scala.collection.JavaConverters._” api. The following code shows same conversion using this api.

First import

import scala.collection.JavaConverters._

Java list

List<Row> javaList = new ArrayList<Row>();

Use “asScala “ to convert Java list to Scala list

val scalaList = javaList.asScala
val squareList = scalaList.map(value => value*value)
println("square list is" + squareList)

reference : http://blog.madhukaraphatak.com/converting-java-collections-to-scala/

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

1 Comment

Thanks but I still can not convert squareList to data frame

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.