使用Spark SQL行访问Java中的多维WrappedArray元素
我有以下模式:使用Spark SQL行访问Java中的多维WrappedArray元素,java,apache-spark,apache-spark-sql,geojson,Java,Apache Spark,Apache Spark Sql,Geojson,我有以下模式: geometry: struct (nullable = true) -- coordinates: array (nullable = true) -- element: array (containsNull = true) -- element: array (containsNull = true) -- element: double (containsNull = true) 在Ja
geometry: struct (nullable = true)
-- coordinates: array (nullable = true)
-- element: array (containsNull = true)
-- element: array (containsNull = true)
-- element: double (containsNull = true)
在Java中,如何使用Spark SQL行访问double元素
我能得到的最远的结果是:row.getStruct(0).getList(0)
谢谢 在Scala中,这是可行的,我让您将其转换为java:
import org.apache.spark.sql.{Row, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import scala.collection.mutable.WrappedArray
object Demo {
case class MyStruct(coordinates:Array[Array[Array[Double]]])
case class MyRow(struct:MyStruct)
def main(args: Array[String]): Unit = {
val sc = new SparkContext(new SparkConf().setAppName("Demo").setMaster("local[*]"))
val sqlContext = new SQLContext(sc)
import sqlContext.implicits._
val data = MyRow(MyStruct(Array(Array(Array(1.0)))))
val df= sc.parallelize(Seq(data)).toDF()
// get first entry (row)
val row = df.collect()(0)
val arr = row.getAs[Row](0).getAs[WrappedArray[WrappedArray[WrappedArray[Double]]]](0)
//access an element
val res = arr(0)(0)(0)
println(res) // 1.0
}
}
最好避免直接访问行。你可以:
df.selectExpr("geometry[0][0][0]")
或
然后使用结果。不幸的是,我看不到在Java中如何使用这一行:val res=arr(0)(0)@MichaelJ.Perry
arr(0)
意味着只需在索引0处获取元素,也许这一行在Java中的工作方式是这样的:arr.get(0).get(0).get(0)
?最终使其工作,必须执行row.apply(0).apply(0).apply(0).doubleValue()。谢谢你的帮助!!这给了我一个java.lang.ClassCastException:scala.collection.mutable.ArrayBuffer不能强制转换为scala.collection.mutable.WrappedArray
,但我将WrappedArray的转换为.map(row=>{row.getAs[row](0).getAs[Seq[Seq[Seq[Double]]]](0)}
,它成功了。
df.select(col("geometry").getItem(0).getItem(0).getItem(0))