私はScalaが初めてです。スカラ リスト (ソース DataFrame で計算されたデータの結果を保持している) を Dataframe または Dataset に変換しようとしています。私はそれを行うための直接的な方法を見つけていません。ただし、リストを DataSet に変換するために次のプロセスを試みましたが、機能していないようです。以下の3つの状況を提供しています。
この変換を行う方法を教えてください。ありがとう。
import org.apache.spark.sql.{DataFrame, Row, SQLContext, DataFrameReader}
import java.sql.{Connection, DriverManager, ResultSet, Timestamp}
import scala.collection._
case class TestPerson(name: String, age: Long, salary: Double)
var tom = new TestPerson("Tom Hanks",37,35.5)
var sam = new TestPerson("Sam Smith",40,40.5)
val PersonList = mutable.MutableList[TestPerson]()
//Adding data in list
PersonList += tom
PersonList += sam
//Situation 1: Trying to create dataset from List of objects:- Result:Error
//Throwing error
var personDS = Seq(PersonList).toDS()
/*
ERROR:
error: Unable to find encoder for type stored in a Dataset. Primitive types
(Int, String, etc) and Product types (case classes) are supported by
importing sqlContext.implicits._ Support for serializing other types will
be added in future releases.
var personDS = Seq(PersonList).toDS()
*/
//Situation 2: Trying to add data 1-by-1 :- Result: not working as desired.
the last record overwriting any existing data in the DS
var personDS = Seq(tom).toDS()
personDS = Seq(sam).toDS()
personDS += sam //not working. throwing error
//Situation 3: Working. However, I am having consolidated data in the list
which I want to convert to DS; if I loop the results of the list in comma
separated values and then pass that here, it will work but will create an
extra loop in the code, which I want to avoid.
var personDS = Seq(tom,sam).toDS()
scala> personDS.show()
+---------+---+------+
| name|age|salary|
+---------+---+------+
|Tom Hanks| 37| 35.5|
|Sam Smith| 40| 40.5|
+---------+---+------+