apache-spark - 在 Spark SQL 查询中获取键的值
问题描述
我有以下 DF 模式:
scala> hotelsDF.printSchema()
root
|-- id: long (nullable = true)
|-- version: integer (nullable = true)
|-- timestamp: long (nullable = true)
|-- changeset: long (nullable = true)
|-- uid: integer (nullable = true)
|-- user_sid: binary (nullable = true)
|-- tags: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- key: binary (nullable = true)
| | |-- value: binary (nullable = true)
|-- latitude: double (nullable = true)
|-- longitude: double (nullable = true)
我需要过滤key
等于tourism
和value
等于的记录hotel
。我使用以下 SQL 查询来执行此操作:
sqlContext.sql("select * from nodes where array_contains(tags.key, binary('tourism')) and array_contains(tags.value, binary('hotel'))").show()
到目前为止,一切都很好。
现在,我的问题是如何选择给定标签键的值?伪查询将类似于:
sqlContext.sql("select tags.tourism from nodes where array_contains(tags.key, binary('tourism')) and array_contains(tags.value, binary('hotel'))").show()
并返回hotel
所有条目。
解决方案
你可以爆炸数组然后过滤:
hotelsDF.withColumn(
"tags1",
explode(col("tags"))
).drop(
"tags"
).filter(
(col("tags1.key") == "tourism") & (col("tags1.value") == "hotel")
).show()
推荐阅读
- django - AJAX Post 抛出 Broken Pipe 并导致 GET
- python - 从 Odoo 中的自定义视图更新另一个模型的字段
- spring-mvc - 如何使用 Spring Web 应用程序显示 PDF 文件
- go - 如何将 map[string]interface{} 转换为字符串
- keras - 相当于 PyTorch 中 Keras 的 binary_crossentropy?
- java - java.lang.RuntimeException:更新 Firestore 版本后 Firestore (0.6.6-dev) 出现内部错误
- image-processing - 带编程的图像处理
- odoo - 无效的模块名称,忽略:multi_company_account
- video - 2个视频之间的FFmpeg匹配编码输出
- python - 删除对象为 NaN 的行