2016-12-19 65 views
0

我有一個像這樣的字符串的列表:List [(String,String,String)]。我如何使用Scala將其轉換爲數據框?如何將元組列表轉換爲數據框在斯卡拉

+1

請提供input'和'output'的'的例子。例如:'input':'List(「a」,「b」)'output:'List(「ab」)' –

回答

0

您可以只使用toDF方法:

scala> val myList = List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3")) 
myList: List[(String, String, String)] = List((a1,a2,a3), (b1,b2,b3), (c1,c2,c3)) 

scala> myList.toDF("col1", "col2", "col3").show 
+----+----+----+ 
|col1|col2|col3| 
+----+----+----+ 
| a1| a2| a3| 
| b1| b2| b3| 
| c1| c2| c3| 
+----+----+----+ 

根據您的配置,你可能需要運行import sqlContext.implicits._

4

您創建一個SparkSession(如星火2.0.0)或SQLContext,和那麼你可以使用隱式toDF()

星火1.6或更早版本:

val sc = new SparkContext("local", "test") 
val sqlContext = new SQLContext(sc) 
import sqlContext.implicits._ 

val df: DataFrame = list.toDF() // with default column names: _1, _2, _3 
val dfWithColNames: DataFrame = list.toDF("col1", "col2", "col3") 

火花2.0.0或更新的版本

val sparkSession: SparkSession = SparkSession.builder().appName("test").master("local").getOrCreate() 
import sparkSession.implicits._ 

val df: DataFrame = list.toDF() // with default column names: _1, _2, _3 
val dfWithColNames: DataFrame = list.toDF("col1", "col2", "col3")