如何查询具有复杂类型(如地图/数组)的RDD?例如,当我写这个测试代码时:
case class Test(name: String, map: Map[String, String])
val map = Map("hello" -> "world", "hey" -> "there")
val map2 = Map("hello" -> "people", "hey" -> "you")
val rdd = sc.parallelize(Array(Test("first", map), Test("second", map2)))
Run Code Online (Sandbox Code Playgroud)
我虽然语法如下:
sqlContext.sql("SELECT * FROM rdd WHERE map.hello = world")
Run Code Online (Sandbox Code Playgroud)
要么
sqlContext.sql("SELECT * FROM rdd WHERE map[hello] = world")
Run Code Online (Sandbox Code Playgroud)
但我明白了
无法访问MapType类型中的嵌套字段(StringType,StringType,true)
和
org.apache.spark.sql.catalyst.errors.package $ TreeNodeException:未解析的属性
分别.
我有一个像这样的火花数据框:
+-------------+------------------------------------------+
|a |destination |
+-------------+------------------------------------------+
|[a,Alice,1] |[[b,Bob,0], [e,Esther,0], [h,Fraudster,1]]|
|[e,Esther,0] |[[f,Fanny,0], [d,David,0]] |
|[c,Charlie,0]|[[b,Bob,0]] |
|[b,Bob,0] |[[c,Charlie,0]] |
|[f,Fanny,0] |[[c,Charlie,0], [h,Fraudster,1]] |
|[d,David,0] |[[a,Alice,1], [e,Esther,0]] |
+-------------+------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
其架构为
|-- destination: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- id: string (nullable = true)
| | |-- name: string (nullable = true)
| | |-- var_only_0_and_1: integer (nullable = false)
Run Code Online (Sandbox Code Playgroud)
如何构造一个对列进行操作的 UDF destination
,即由 Spark 的 UDF 创建的包装数组collect_list
来计算变量的平均值var_only_0_and_1
?