本文介绍了如何在scala中将元组列表转换为数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串元组列表:List[(String, String, String)]
.
I have a list of tuples of strings: List[(String, String, String)]
.
如何使用 Scala 将其转换为数据帧?
How can I convert it into dataframe with Scala?
推荐答案
您创建一个 SparkSession
(从 Spark 2.0.0 开始)或一个 SQLContext
,然后您可以使用隐式的toDF()
:
You create a SparkSession
(as of Spark 2.0.0) or a SQLContext
, and then you can use the implicit toDF()
:
Spark 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")
Spark 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")
这篇关于如何在scala中将元组列表转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!