Dil*_*eam 7 scala apache-spark apache-spark-sql orc
在之前的版本中,我们曾经在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?
Run Code Online (Sandbox Code Playgroud)
}
从Spark 1.4开始,您只需使用DataFrameWriter并设置format为orc:
peopleSchemaRDD.write.format("orc").save("people")
Run Code Online (Sandbox Code Playgroud)
要么
peopleSchemaRDD.write.orc("people")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8957 次 |
| 最近记录: |