我有json数据,如:
{
"resultid": "Success",
"userId": "abc",
"cardtype": "ag",
"computescore": [{
"values": ["654", "655", "666"]
}, {
"values": ["754", "755", "777"]
}]
}
scala> val a = input.select("computescore.values")
a: org.apache.spark.sql.DataFrame = [values: array<array<string>>]
scala> a.collect.foreach(println)
[WrappedArray(WrappedArray(654, 655, 666), WrappedArray(754, 755, 777))]
我想要实现的是具有多行和多列的数据框
例如
a1 a2 a3
654 655 666
754 755 777
答案 0 :(得分:0)
通过这样做实现
val input = sqlContext.jsonFile("D:/test.json")
val a = input.select("computescore.values")
val b=a.explode("values","mvnew"){test: WrappedArray[WrappedArray[String]] => test.asInstanceOf[WrappedArray[WrappedArray[String]]]}
val c=b.select("mvnew")
val d=c.map {case Row(col2: WrappedArray[String]) => (col2(0), col2(1), col2(2))}.toDF( "Col2", "Col3", "Col4")
有更好的方法吗
答案 1 :(得分:0)
val sortDates = udf {arr:collection.mutable.WrappedArray [Row] => arr.map { case Row(a:字符串,b:字符串,c:字符串,d:字符串,e:字符串,f:长,g:String,h:Integer)=> (a,b,c,d,e,f,g,h) } .sortBy(_._ 4) }