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

Extracting values from a Spark column containing nested values

这是我的 mongodb collection:

架构的一部分
|-- variables: struct (nullable = true)  
|    |-- actives: struct (nullable = true)  
|    |    |-- data: struct (nullable = true)  
|    |    |    |-- 0: struct (nullable = true)  
|    |    |    |    |--active: integer (nullable = true)  
|    |    |    |    |-- inactive: integer (nullable = true)

我已获取 collection 并将其存储在 Spark 数据框中,现在正尝试提取 variables 列中的最里面的值。

df_temp = df1.select(df1.variables.actives.data)

这工作得很好,我能够得到 data 结构的内部结构。

+----------------------+  
|variables.actives.data|  
+----------------------+  
|  [[1,32,0.516165...|  
|  [[1,30,1.173139...|  
|  [[4,18,0.160088...|

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

df_temp = df1.select(df1.variables.actives.data.0.active)

我收到 无效语法 错误。

df_temp = df1.select(df1.variables.actives.data.0.active)
^
SyntaxError: invalid syntax

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

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

你可以试试:

df_temp = df1.select(df1.variables.actives.data["0"].active)