【发布时间】:2015-01-14 04:24:30
【问题描述】:
spark sql 是否提供任何方法来自动加载 csv 数据? 我找到了以下 Jira:https://issues.apache.org/jira/browse/SPARK-2360 但它已关闭....
目前我会按如下方式加载 csv 文件:
case class Record(id: String, val1: String, val2: String, ....)
sc.textFile("Data.csv")
.map(_.split(","))
.map { r =>
Record(r(0),r(1), .....)
}.registerAsTable("table1")
关于从 csv 文件中自动推断模式的任何提示?特别是 a) 我如何生成一个表示模式的类和 b) 我如何自动填充它(即 Record(r(0),r(1), .....))?
更新: 我在这里找到了模式生成的部分答案: http://spark.apache.org/docs/1.1.0/sql-programming-guide.html#data-sources
// The schema is encoded in a string
val schemaString = "name age"
// Generate the schema based on the string of schema
val schema =
StructType(
schemaString.split(" ").map(fieldName => StructField(fieldName, StringType, true)))
// Convert records of the RDD (people) to Rows.
val rowRDD = people.map(_.split(",")).map(p => Row(p(0), p(1).trim))
// Apply the schema to the RDD.
val peopleSchemaRDD = sqlContext.applySchema(rowRDD, schema)
所以剩下的唯一问题就是如何执行该步骤
map(p => Row(p(0), p(1).trim)) 动态为给定数量的属性?
感谢您的支持! 约尔格
【问题讨论】:
标签: scala csv apache-spark apache-spark-sql