【问题标题】:Spark: Save Dataframe in ORC formatSpark:以 ORC 格式保存数据框
【发布时间】:2015-12-13 12:38:17
【问题描述】:

在之前的版本中,我们曾经在 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?

}

【问题讨论】:

    标签: scala apache-spark apache-spark-sql orc


    【解决方案1】:

    从 Spark 1.4 开始,您可以简单地使用 DataFrameWriter 并将 format 设置为 orc

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

    peopleSchemaRDD.write.orc("people")
    

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 2016-08-20
      • 2015-11-11
      • 2018-07-04
      • 2016-11-26
      • 1970-01-01
      • 2021-09-29
      • 2019-09-23
      • 2021-09-26
      相关资源
      最近更新 更多