val initDFStruct = spark.sql("select 'blue' as color, 'john' as name, 'M' as size union select 'red' as color, 'jim' as name, 'L'")
.selectExpr("(color, name, size) as col1")
initDFStruct.show(false)
with your_data as
(
select '{color=blue, name=john, size=M}' str
)
select str as original_string,
m['color'] as color,
m['name'] as name,
m['size'] as size
from
(
select str, str_to_map(regexp_replace(regexp_replace(str,'\\{|\\}',''),', *',','),',','=') m
from your_data --use your table
)s;
结果:
original_string color name size
{color=blue, name=john, size=M} blue john M
2条答案
按热度按时间a7qyws3x1#
使用与配置单元兼容的sparksql。
如果col1是字符串,则可以是一个解决方案:
它显示:
如果只想得到color=blue的行
显示预期结果:
如果col1是一个结构:
它显示:
显示想要的结果:
总之,如果将其作为字符串列,则可以在where子句中使用
where col1 like '%color=blue%'
如果将其作为结构,则where子句应为:"col1.color = 'blue'
c7rzv4ha2#
您可以将字符串转换为map(删除逗号后的大括号和空格,并使用str\u to\u map函数)。配置单元示例:
结果: