【发布时间】:2018-08-29 22:20:44
【问题描述】:
根据pyspark collect_set or collect_list with groupby 中的接受的答案,当您在某个列上执行collect_list 时,该列中的null 值将被删除。我查过了,这是真的。
但在我的情况下,我需要保留空列——我怎样才能做到这一点?
我没有找到关于这种collect_list 函数变体的任何信息。
背景背景来解释我为什么想要空值:
我有一个数据框df 如下:
cId | eId | amount | city
1 | 2 | 20.0 | Paris
1 | 2 | 30.0 | Seoul
1 | 3 | 10.0 | Phoenix
1 | 3 | 5.0 | null
我想通过以下映射将其写入 Elasticsearch 索引:
"mappings": {
"doc": {
"properties": {
"eId": { "type": "keyword" },
"cId": { "type": "keyword" },
"transactions": {
"type": "nested",
"properties": {
"amount": { "type": "keyword" },
"city": { "type": "keyword" }
}
}
}
}
}
为了符合上面的嵌套映射,我转换了我的 df,以便对于 eId 和 cId 的每个组合,我都有一个这样的事务数组:
df_nested = df.groupBy('eId','cId').agg(collect_list(struct('amount','city')).alias("transactions"))
df_nested.printSchema()
root
|-- cId: integer (nullable = true)
|-- eId: integer (nullable = true)
|-- transactions: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- amount: float (nullable = true)
| | |-- city: string (nullable = true)
将df_nested保存为json文件,有我得到的json记录:
{"cId":1,"eId":2,"transactions":[{"amount":20.0,"city":"Paris"},{"amount":30.0,"city":"Seoul"}]}
{"cId":1,"eId":3,"transactions":[{"amount":10.0,"city":"Phoenix"},{"amount":30.0}]}
如您所见-当cId=1 和eId=3 时,我的数组元素之一amount=30.0 没有city 属性,因为这是我原始数据中的null (df) .当我使用 collect_list 函数时,空值被删除。
但是,当我尝试使用上述索引将 df_nested 写入 elasticsearch 时,由于架构不匹配而出错。这基本上就是为什么我想在应用 collect_list 函数后保留我的空值的原因。
【问题讨论】:
-
为什么需要空值?你能提供一个示例 DataFrame 和所需的输出吗?
-
@pault 我需要空值,因为我正在尝试创建嵌套数据帧并将其写入弹性搜索。因此数据框的架构必须与我设置的弹性搜索映射完全匹配。更新我的问题以显示示例。
-
@pault - 更新了我的问题以提供更好的上下文。
-
是否可以将
null值替换为其他值,例如字符串'null'?
标签: nested pyspark-sql collect elasticsearch-hadoop elasticsearch-mapping