19. Parse Application Log Lines
Difficulty: Medium · Topics: Strings, Filtering & Selection
Each line in logs looks like 2024-03-01 12:00:01 ERROR [payment] Card declined: date, time, level, service in square brackets, then a free-text message.
Return ts (date and time), level, service and message for every line that matches this format; skip malformed lines.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
logs
| line |
|---|
| 2024-03-01 12:00:01 ERROR [payment] Card declined |
| 2024-03-01 12:00:05 INFO [auth] User 42 logged in |
| garbage line |
Expected output
| ts | level | service | message |
|---|---|---|---|
| 2024-03-01 12:00:01 | ERROR | payment | Card declined |
| 2024-03-01 12:00:05 | INFO | auth | User 42 logged in |
Hints
Hint 1
One regular expression with capture groups:^(\S+ \S+) (\w+) \[([^\]]+)\] (.*)$.Hint 2
F.regexp_extract(col, pattern, n) returns group n, or "" when the line doesn't match.Hint 3
Filter out rows where the extracted level is empty.Learn the concepts
- regexp_extract · 8 min read. Extract, filter, replace and collect with a regular expression, and what you get when nothing matches.
- select · 6 min read. Choose, rename and compute columns, and why select beats a chain of withColumn calls.
- filter / where · 5 min read. Keep the rows that match a condition, and the null and operator traps that silently drop data.
PySpark functions you'll practise
- filter
- select
- regexp_extract
Related problems
- Parse UTM Parameters · Medium · Strings, Conditional Logic, Filtering & Selection
- Employees Earning More than Their Manager · Medium · Joins, Filtering & Selection
- Latest Order per Customer · Medium · Window Functions, Filtering & Selection
- Top Two Salary Levels per Department · Medium · Window Functions, Filtering & Selection
- Inventory a Partitioned Folder · Medium · Data Lake, Aggregations, Strings
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice