简体   繁体   English

如何使用 Apache Spark SQL 查询在字符串数组中搜索字符串?

[英]How to search a string in an array of strings using Apache Spark SQL query?

I have an array of strings like this:我有一个这样的字符串数组:

SELECT ARRAY('item_1', 'item_2', 'item_3') AS items

Result:结果:

items
Type : ARRAY<STRING>
["item_1","item_2","item_3"]

I would like to search for an item inside of it, but if I try the regular way:我想在其中搜索一个项目,但如果我尝试常规方式:

SELECT * FROM items WHERE items = 'item_1'

I'll get this error:我会收到这个错误:

Cannot resolve '(items. items = 'item_1')' due to data type mismatch differing types in '(items. items = 'item_1')' (array and string).由于“( items = 'item_1')”(数组和字符串)中的不同类型的数据类型不匹配,无法解析“( items = 'item_1')”。 line 1 pos 26第 1 行 26 号线

So, what can I do to search a string value inside of an array of strings using a Spark SQL query?那么,如何使用 Spark SQL 查询在字符串数组中搜索字符串值?

Thanks in advance =)提前感谢=)

Use array_contains function:使用array_contains function:

SELECT * FROM items WHERE array_contains(items, 'item_1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM