2017-03-09 120 views
1

我目前正在对行如何devide一个字符串,每个空格REGEXP_EXTRACT(SQL-雅典娜)

例如从我们webserverlog拆分消息: 我的消息(数据类型的字符串)看起来是这样的:

at=info method=GET path="/v1/..." host=web.com request_id=a3d71fa9-9501-4bfe-8462-54301a976d74 fwd="xxx.xx" dyno=web.1 connect=1ms service=167ms status=200 bytes=1114

,我想切到这些行:

path | service | connect | method | status | fwd  | dyno | 
------ | ------- | -------- | ------ | ------ | ------- | ------ | 
/v1/...| 167  | 1  | GET | 200 | xxx.xxx | web.1 | 

我与REGEXP_EXTRACT发挥各地函数(第一次)在标准SQL中的亚马逊雅典娜上,并且已经从字符串中得到了几行,但是我正在挣扎着几行。

当我尝试获得例如切到了极致出字符串即时得到比我需要

REGEXP_EXTRACT (message,'dyno=[^,]+[a-z]')AS dyno 
-> dyno=web.2 connect=0ms service=192ms status=200 bytes 

我想有dyno=web.1结果&然后再提取

它会更多资讯如果我从开始(“dyno =”)将字符串剪切到“connect =”之前的空白处,但我无法在我读取的网站中找到正确的选项,那就好了。

我如何编写选项以获取正确的字符串?

+1

你为什么用'[^,] +'在这里吗?从你的示例中,如果你的目标字符串不能包含空格或者'。*?(?= connect =)',我宁愿使用'\ S +'。 –

+0

[^,] +在我之前制作的以前的行上工作过,所以我认为它可以在其他东西上工作 –

+0

字符串中根本没有逗号,所以'[^,]'是毫无意义的。 –

回答

1

对塞巴斯蒂安的评论Piggybagging,我同意\S+应该是前进的解决方案。因此,查询应该是这样的:

select REGEXP_EXTRACT (message,'dyno=(\S+)',1) AS dyno 
from (
    select 
    'at=info method=GET path="/v1/..." host=web.com request_id=a3d71fa9-9501-4bfe-8462-54301a976d74 fwd="xxx.xx" dyno=web.1 connect=1ms service=167ms status=200 bytes=1114' message 
) 
0

如果你没有自己的价值观内空间(如键值对),然后有一个简单的解决方案。

select msg['at']   as "at" 
     ,msg['method']  as "method" 
     ,msg['path']  as "path" 
     ,msg['host']  as "host" 
     ,msg['request_id'] as "request_id" 
     ,msg['fwd']  as "fwd" 
     ,msg['dyno']  as "dyno" 
     ,msg['connect'] as "connect" 
     ,msg['service'] as "service" 
     ,msg['status']  as "status" 
     ,msg['bytes']  as "bytes" 

from (select split_to_map (message,' ','=') as msg  
     from mytable 
     ) 
; 

at | method | path | host |    request_id    | fwd | dyno | connect | service | status | bytes 
------+--------+-----------+---------+--------------------------------------+----------+-------+---------+---------+--------+------- 
info | GET | "/v1/..." | web.com | a3d71fa9-9501-4bfe-8462-54301a976d74 | "xxx.xx" | web.1 | 1ms  | 167ms | 200 | 1114 
相关问题