从包含嵌套值的 Spark 列中提取值

big*_*ong 5 apache-spark apache-spark-sql pyspark

这是我的 mongodb 集合的架构的一部分:

|-- variables: struct (nullable = true)  
|    |-- actives: struct (nullable = true)  
|    |    |-- data: struct (nullable = true)  
|    |    |    |-- 0: struct (nullable = true)  
|    |    |    |    |--active: integer (nullable = true)  
|    |    |    |    |-- inactive: integer (nullable = true)
Run Code Online (Sandbox Code Playgroud)

我已获取该集合并将其存储在 Spark 数据框中,现在尝试提取变量列中最里面的值。

df_temp = df1.select(df1.variables.actives.data)
Run Code Online (Sandbox Code Playgroud)

这工作得很好,我能够获得数据结构的内部结构。

+----------------------+  
|variables.actives.data|  
+----------------------+  
|  [[1,32,0.516165...|  
|  [[1,30,1.173139...|  
|  [[4,18,0.160088...|
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试进一步深入时:

df_temp = df1.select(df1.variables.actives.data.0.active)
Run Code Online (Sandbox Code Playgroud)

我收到无效语法错误。

df_temp = df1.select(df1.variables.actives.data.0.active)
^
SyntaxError:语法无效

问题是我的内部字段键的名称是数字,并且我找不到内部字段键的名称是数字的示例。

实现从数据帧检索最里面的值( activeinactive )的目标的最佳方法是什么?

mto*_*oto 6

你可以试试:

df_temp = df1.select(df1.variables.actives.data["0"].active)
Run Code Online (Sandbox Code Playgroud)