如何解决将由JSON StructTypes组成的ArrayType转换为DataFrame列
我有一个DataFrame,它的列是名为segmentationFieldValues
的ArrayType。此ArrayType由30种不同的JSON StructTypes组成,每个具有三个字段:
|-- emailAddress: string (nullable = true)
|-- subscriptionState: string (nullable = true)
|-- subscribeDate: timestamp (nullable = true)
|-- resubscribeDate: timestamp (nullable = true)
|-- subscribeMethod: string (nullable = true)
|-- unsubscribeDate: timestamp (nullable = true)
|-- unsubscribeMethod: string (nullable = true)
|-- segmentationFieldValues: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- segmentationFieldGroupId: string (nullable = true)
| | |-- segmentationFieldId: string (nullable = true)
| | |-- value: string (nullable = true)
因此,所有行都将具有一个字典对象,这些对象的segmentationFieldGroupID
和segmentationFieldID
键的值相同,而与它们相关联的value
键中存储的内容将有所不同。我试图通过获取与segmentationFieldGroupID
和segmentationFieldID
关联的值并将其设为列的名称,然后将这些JSON对象中的每个对象自己转换为列,并将该列的值存储在value
字段。
我尝试创建一个UDF函数,该函数基于segmentationFieldGroupID
和segmentationFieldID
过滤数组并返回value
:
def filterArray(segGroup,segField,array):
val = list(filter(lambda x: (x['segmentationFieldGroupId'] == segGroup) & (x['segmentationFieldId'] == segField),array))
return val[0]['value']
filterArrayPython = udf(filterArray,StringType())
df.select('segmentationFieldValues',filterArrayPython('264175','2433004','segmentationFieldValues').alias('264175_2433004')).show(5)
但是到目前为止,我遇到了这个错误:
org.apache.spark.sql.AnalysisException: cannot resolve '`264175`' given input columns: [segmentationFieldValues,unsubscribeMethod,subscribeMethod,subscriptionState,resubscribeDate,subscribeDate,emailAddress,unsubscribeDate];;
不知道为什么,该功能有效,但在PySpark上下文中无效。如何解决此问题或以其他方式做到?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。