Scala Spark:以ORC格式保存数据帧

Scala Spark:以ORC格式保存数据帧,scala,apache-spark,apache-spark-sql,orc,Scala,Apache Spark,Apache Spark Sql,Orc,在以前的版本中,我们曾经在RDD上有一个“saveAsOrcFile()”方法。这已经不见了!如何以ORC文件格式在数据帧中保存数据 def main(args: Array[String]) { println("Creating Orc File!") val sparkConf = new SparkConf().setAppName("orcfile") val sc = new SparkContext(sparkConf) val hiveContext = new org.apac

在以前的版本中,我们曾经在RDD上有一个“saveAsOrcFile()”方法。这已经不见了!如何以ORC文件格式在数据帧中保存数据

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")