我有一个具有以下格式的日志文件,我想从中提取 ip、datetime 和 uri
并加载到表中。
64.242.88.10 - - [07/Mar/2004:17:09:01 -0800] "GET /twiki/bin/search/Main/SearchResult?scope=text&search=Joris%20*Benschop[^A-Za-z] HTTP/1.1" 200 4284
我可以通过将日志文件行作为单个字符串加载到表中并使用 regexp_extract
来做到这一点。
创建表日志(行字符串);
将数据本地 inpath '.../mylog.log' 加载到表日志中;
选择
regexp_extract(line, '(.*) (- -)\\[(.*) -.*\\]\\"GET (.*)\\?',1),--ip
regexp_extract(line, '(.*) (- -)\\[(.*) -.*\\]\\"GET (.*)\\?',3),--日期时间
regexp_extract(line, '(.*) (- -)\\[(.*) -.*\\]\\"GET (.*)\\?',4) --uri
来自日志限制 10;
+---------------+-----------------------+--------------------------------------------+--+
| _c0 | _c1 | _c2 |
+---------------+-----------------------+--------------------------------------------+--+
| 64.242.88.10 | 07/Mar/2004:17:09:01 | /twiki/bin/search/Main/SearchResult |
| 64.242.88.10 | 07/Mar/2004:17:10:20 | /twiki/bin/oops/TWiki/TextFormattingRules |
+---------------+-----------------------+--------------------------------------------+--+
我想做的是创建一个指定 SerDe 属性的表,并在不使用 regexp_extract
函数的情况下加载它。我尝试了以下但它不起作用。
create table logs (
ip string,
day timestamp,
url string)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties ("input.regex" =
"(.*) [^- - \[](.*) [^-.*\]] \"([^GET].*\?)");
load data local inpath ".../mylog.log" into table logs;
我感谢一些帮助和指导。
最佳答案
day
不能是时间戳,因为它不是 ISO 格式 (yyyy-MM-dd HH:mm:ss
)- 转义应使用双反斜杠 (
\\
) - 正则表达式应该覆盖整个记录(在本例中以
.*
结尾)
create external table logs
(
ip string
,day string
,url string
)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties ("input.regex" = "(\\S+).*?\\[(.*?)\\s.*?(/.*?)\\?.*")
;
select * from logs
;
+--------------+----------------------+-------------------------------------+
| ip | day | url |
+--------------+----------------------+-------------------------------------+
| 64.242.88.10 | 07/Mar/2004:17:09:01 | /twiki/bin/search/Main/SearchResult |
+--------------+----------------------+-------------------------------------+
关于regex - 如何使用 RegexSerDe 将日志文件加载到 Hive 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44487298/