-1

我有一个包含数组的列。每行的结构如下所示:

+---------------+------------+-----------+---------+-----------+-------------+
|table_name     |table_schema|column_name|data_type|column_size|column_tokens|
+---------------+------------+-----------+---------+-----------+-------------+
|sales          |demo        |sales_id   |int      |100        |[sales, id]  |
+---------------+------------+-----------+---------+-----------+-------------+

现在我想查询是否有任何行的 column_tokens 包含完全 [sales, id] ,返回给我。

我使用这样的查询来获得上面的 DF ,

selectedTableInfo.filter(array_contains(col("column_tokens"),"id")).show(100,false)

有什么办法可以做 Array to Array match 吗?

喜欢 :

 selectedTableInfo.filter(array_equals(col("column_tokens"),Seq("sales","id"))).show(100,false)
4

2 回答 2

0

您也可以使用isin()

val df = spark.createDataFrame(
    Seq(
        ("1", Seq("a", "b")), 
        ("2", Seq("c", "d"))
    )
).toDF(
    "id", 
    "sales"
)
df.show()

df.filter(
    col("sales").isin(array(lit("a"), lit("b")))
).show()
于 2020-08-18T15:18:12.180 回答
0

只需将相等条件与array函数一起使用。这是一个示例。

val df = spark.createDataFrame(Seq(("1", Seq("a", "b")))).toDF("id", "array")
df.show

+---+------+
| id| array|
+---+------+
|  1|[a, b]|
+---+------+

df.filter(col("array") === array(lit("a"), lit("b"))).show

+---+------+
| id| array|
+---+------+
|  1|[a, b]|
+---+------+
于 2020-08-18T14:30:40.600 回答