Sql 如何在配置单元中将字符串转换为数组?
该列的值如下所示:Sql 如何在配置单元中将字符串转换为数组?,sql,arrays,split,hive,hiveql,Sql,Arrays,Split,Hive,Hiveql,该列的值如下所示: ["a", "b", "c(d, e)"] 这里的值是字符串类型。我希望将字符串转换为数组,并尝试使用split(column_name,,')。但是,由于数组中的元素包含逗号符号(例如,“c(d,e)”),因此它不能正常工作。是否有其他方法将字符串转换为数组?在这种情况下,只能在两个配额之间用逗号分割 REGEXP”(? with your_data as ( select '["a",
["a", "b", "c(d, e)"]
这里的值是字符串类型。我希望将字符串转换为数组,并尝试使用
split(column_name,,')
。但是,由于数组中的元素包含逗号符号(例如,“c(d,e)”
),因此它不能正常工作。是否有其他方法将字符串转换为数组?在这种情况下,只能在两个配额之间用逗号分割
REGEXP”(?
with your_data as (
select '["a", "b", "c(d, e)"]' as str
)
select split(str, '(?<="), *(?=")') as splitted_array,
element,
regexp_replace(element,'^"|"$','') as element_unquotted
from (
select regexp_replace(str,'^\\[|\\]$','') as str --remove square brackets
from your_data
) d
--explode array
lateral view explode(split(str, '(?<="), *(?=")')) e as element
splitted_array element element_unquotted
["\"a\"","\"b\"","\"c(d, e)\""] "a" a
["\"a\"","\"b\"","\"c(d, e)\""] "b" b
["\"a\"","\"b\"","\"c(d, e)\""] "c(d, e)" c(d, e)
with your_data as (
select '["a", "b", "c(d, e)"]' as str
)
select split(str, '\\|\\|\\|') splitted_array
from (--replace '", ' with |||, remove all quotes, remove square brackets
select regexp_replace(regexp_replace(str,'", *"','|||'),'^\\[|\\]$|"','') as str
from your_data ) d
splitted_array
["a","b","c(d, e)"]