加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – Spark:以ORC格式保存Dataframe

发布时间:2020-12-16 18:53:51 所属栏目:安全 来源:网络整理
导读:在之前的版本中,我们曾经在RDD上使用’saveAsOrcFile()’方法.现在已经不见了!如何以ORC文件格式保存DataFrame中的数据? def main(args: Array[String]) {println("Creating Orc File!")val sparkConf = new SparkConf().setAppName("orcfile")val sc = ne
在之前的版本中,我们曾经在RDD上使用’saveAsOrcFile()’方法.现在已经不见了!如何以ORC文件格式保存DataFrame中的数据?

def main(args: Array[String]) {
println("Creating Orc File!")
val sparkConf = new SparkConf().setAppName("orcfile")
val sc = new SparkContext(sparkConf)
val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)

val people = sc.textFile("/apps/testdata/people.txt")
val schemaString = "name age"
val schema = StructType(schemaString.split(" ").map(fieldName => {if(fieldName == "name") StructField(fieldName,StringType,true) else StructField(fieldName,IntegerType,true)}))
val rowRDD = people.map(_.split(",")).map(p => Row(p(0),new Integer(p(1).trim)))

//# Infer table schema from RDD**
val peopleSchemaRDD = hiveContext.createDataFrame(rowRDD,schema)

//# Create a table from schema**
peopleSchemaRDD.registerTempTable("people")
val results = hiveContext.sql("SELECT * FROM people")
results.map(t => "Name: " + t.toString).collect().foreach(println)

// Now I want to save this Dataframe(peopleSchemaRDD) in ORC Format. How do I do that?

}

解决方法

从Spark 1.4开始,您只需使用DataFrameWriter并将格式设置为orc:

peopleSchemaRDD.write.format("orc").save("people")

要么

peopleSchemaRDD.write.orc("people")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读