Skip to main content
Seaport
Gold ⭐️
May 31, 2019
Solved

Create a hive table upon a fixed-width log file but the last column width is not fixed

Basically, I am trying to analyze SQL server log files using Hive.

The layout is the log file is

  1. char(23) - for timestamp
  2. char(1) - space
  3. char(12) - source
  4. the rest of the row, and the length varies.

The row delimiter is CR+LF.

Below are some entries in the log.

2019-05-28 07:29:55.03 Server      UTC adjustment: -7:00
2019-05-28 07:29:55.03 Server      (c) Microsoft Corporation.
2019-05-28 07:29:55.03 Server      All rights reserved.
2019-05-28 07:29:55.03 Server      Server process ID is 3368.

There are several posts here regarding fixed-width column layout. But in my case, the last column is identified not by the width but by the row delimiter.


Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
June 1, 2019

@Haijin Li

Use hive Regex serde and your matching regex will be

(.{22})(.{1})(.{12})(.*)

(.{22}) -> 1st capture group for 22 characters

(.{1}) -> 2nd for 1 character

(.{12}) -> 3rd for 12 characters

(.*) -> 4th capture group matches for rest of the row.


4 replies

Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
June 1, 2019

@Haijin Li

Use hive Regex serde and your matching regex will be

(.{22})(.{1})(.{12})(.*)

(.{22}) -> 1st capture group for 22 characters

(.{1}) -> 2nd for 1 character

(.{12}) -> 3rd for 12 characters

(.*) -> 4th capture group matches for rest of the row.


Translate
Seaport
SeaportAuthor
Gold ⭐️
June 2, 2019

@Shu

Thanks for your help. I am currently on the road and will test your solution once I am back in office.

Translate