【发布时间】:2016-05-27 14:06:12
【问题描述】:
我有一个从 JSON 对象创建的数据框。我可以查询这个数据框并将其写入 parquet。
由于我推断架构,我不一定知道数据框中的内容。
有没有办法将列名取出或使用自己的架构映射数据框?
// The results of SQL queries are DataFrames and support all the normal RDD operations.
// The columns of a row in the result can be accessed by field index:
df.map(t => "Name: " + t(0)).collect().foreach(println)
// or by field name:
df.map(t => "Name: " + t.getAs[String]("name")).collect().foreach(println)
// row.getValuesMap[T] retrieves multiple columns at once into a Map[String, T]
df.map(_.getValuesMap[Any](List("name", "age"))).collect().foreach(println)
// Map("name" -> "Justin", "age" -> 19)
我想做类似的事情
df.map (_.getValuesMap[Any](ListAll())).collect().foreach(println)
// Map ("name" -> "Justin", "age" -> 19, "color" -> "red")
不知道列的实际数量或名称。
【问题讨论】:
标签: scala apache-spark apache-spark-sql