有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数组中的java 2值与我的数据帧中的2列对应

使用Java中的Apache Spark,我有:

root
 |-- datasetid: string (nullable = true)
 |-- fields: struct (nullable = true)
...
 |    |-- latlon: array (nullable = true)
 |    |    |-- element: double (containsNull = true)

这基于此JSON片段:

"fields":{
  "latlon":[
    35.9543748,
    -78.9944911
  ],

我试图使用以下方法将数据提取到列中:

df = df.withColumn("lat", df.col("fields.latlon[0]"));
df = df.withColumn("lon", df.col("fields.latlon[1]"));

(我希望您能欣赏语法的简洁)。然而,我必须承认,这并没有真正起作用:

No such struct field latlon[1] in 

我尝试了其他一些事情,但运气不好


共 (1) 个答案

  1. # 1 楼答案

    使用代码

    df.withColumn("lat", $"fields.latlon".getItem(0))
      .withColumn("lon", $"fields.latlon".getItem(1))
    

    使用sql

    df.registerTempTable("geo")
    latlon = sqlContext.sql("select fields.latlon[0] as lat, fields.latlon[1] as lon from geo")