使用 Scala 将 RDD 转换为 Spark 中的 DataFrame [英] Convert an RDD to a DataFrame in Spark using Scala
本文介绍了使用 Scala 将 RDD 转换为 Spark 中的 DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 textRDD: org.apache.spark.rdd.RDD[(String, String)]
我想将其转换为 DataFrame.列对应每页(行)的标题和内容.
I would like to convert it to a DataFrame. The columns correspond to the title and content of each page(row).
推荐答案
使用 toDF()
,如果有列名,请提供.
Use toDF()
, provide the column names if you have them.
val textDF = textRDD.toDF("title": String, "content": String)
textDF: org.apache.spark.sql.DataFrame = [title: string, content: string]
或
val textDF = textRDD.toDF()
textDF: org.apache.spark.sql.DataFrame = [_1: string, _2: string]
shell 自动导入(我使用的是 1.5 版),但您可能需要在应用程序中 import sqlContext.implicits._
.
The shell auto-imports (I am using version 1.5), but you may need import sqlContext.implicits._
in an application.
这篇关于使用 Scala 将 RDD 转换为 Spark 中的 DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文