Watch the NameNode log file live on a Hadoop node.
Hadoop
tail -f /var/log/hadoop/hadoop-hdfs-namenode.log
Sample Program
This code fetches logs for a Hadoop YARN application using its ID. It then counts how many lines contain the word 'ERROR' to help find problems quickly.
Hadoop
# This example shows how to fetch and analyze logs for a failed Hadoop jobimport subprocess
# Replace with your actual application ID
app_id = 'application_1680000000000_0001'# Run yarn logs command to get logs
result = subprocess.run(['yarn', 'logs', '-applicationId', app_id], capture_output=True, text=True)
logs = result.stdout
# Simple analysis: count error lines
error_lines = [line for line in logs.splitlines() if'ERROR'in line]
print(f'Total log lines: {len(logs.splitlines())}')
print(f'Error lines found: {len(error_lines)}')
# Show first 5 error linesprint('\nFirst 5 error lines:')
for line in error_lines[:5]:
print(line)
OutputSuccess
Important Notes
Logs can be very large; use filters or search tools to find relevant parts.
Check both stdout and stderr logs for full error details.
Keep Hadoop log levels configurable to control verbosity.
Summary
Logs show what happens inside Hadoop and help find problems.
Use yarn logs or hdfs dfs -cat to access logs.
Look for 'ERROR' lines to quickly spot issues.
Practice
(1/5)
1. What is the main purpose of Hadoop logs in managing a cluster?
easy
A. To speed up data processing
B. To show what happens inside Hadoop and help find problems
C. To store user data permanently
D. To backup Hadoop configuration files
Solution
Step 1: Understand the role of logs
Logs record events and actions inside Hadoop components.
Step 2: Identify the purpose of logs in troubleshooting
Logs help find and fix problems by showing errors and system behavior.
Final Answer:
To show what happens inside Hadoop and help find problems -> Option B
Quick Check:
Logs = Problem detection [OK]
Hint: Logs help spot problems by showing system events [OK]
Common Mistakes:
Confusing logs with data storage
Thinking logs speed up processing
Assuming logs backup configs
2. Which command is used to view logs of a YARN application in Hadoop?
easy
A. yarn logs -applicationId <app_id>
B. hdfs dfs -ls
C. hadoop fs -put
D. yarn application -status
Solution
Step 1: Identify commands related to logs
Only yarn logs fetches logs for YARN apps.
Step 2: Match command with correct syntax
yarn logs -applicationId <app_id> shows logs for the given app.
Final Answer:
yarn logs -applicationId <app_id> -> Option A
Quick Check:
YARN logs command = yarn logs -applicationId [OK]
Hint: Use 'yarn logs -applicationId' to get app logs fast [OK]
Common Mistakes:
Using 'hdfs dfs -ls' which lists files, not logs
Confusing 'hadoop fs -put' with log commands
Using 'yarn application -status' which shows status, not logs
3. Given the command hdfs dfs -cat /logs/app1.log | grep ERROR, what is the expected output?
medium
A. A list of files in /logs directory
B. All lines from app1.log
C. Only lines without the word 'ERROR'
D. Only lines containing the word 'ERROR' from app1.log
Solution
Step 1: Understand the command parts
hdfs dfs -cat /logs/app1.log outputs the file content; grep ERROR filters lines with 'ERROR'.
Step 2: Combine command effects
The pipe sends file content to grep, which shows only lines containing 'ERROR'.
Final Answer:
Only lines containing the word 'ERROR' from app1.log -> Option D
Quick Check:
grep ERROR filters lines with ERROR [OK]
Hint: Pipe file content to grep ERROR to find error lines [OK]
Common Mistakes:
Thinking grep removes ERROR lines
Expecting full file output
Confusing grep with listing files
4. You run yarn logs -applicationId app_12345 but get an error saying 'Application not found'. What is the likely cause?
medium
A. The logs directory is empty
B. The command syntax is wrong
C. The application ID is incorrect or does not exist
D. YARN is not installed on the cluster
Solution
Step 1: Analyze the error message
'Application not found' means the given app ID is not recognized by YARN.
Step 2: Check command correctness
The command syntax is correct; error is about app ID validity.
Final Answer:
The application ID is incorrect or does not exist -> Option C
Quick Check:
Invalid app ID = Application not found error [OK]
Hint: Check app ID spelling if 'Application not found' error appears [OK]
Common Mistakes:
Assuming command syntax is wrong
Thinking YARN is missing
Believing logs directory emptiness causes this error
5. You want to quickly find all error messages in Hadoop logs stored in HDFS at /var/log/hadoop. Which command sequence will help you achieve this efficiently?
hard
A. hdfs dfs -cat /var/log/hadoop/* | grep ERROR
B. yarn logs -applicationId * | grep ERROR
C. hdfs dfs -ls /var/log/hadoop | grep ERROR
D. hadoop fs -copyToLocal /var/log/hadoop /tmp && grep ERROR /tmp/*
Solution
Step 1: Identify command to read all logs in HDFS directory
hdfs dfs -cat /var/log/hadoop/* outputs all files in that directory.
Step 2: Filter output for error lines
Piping to grep ERROR shows only lines with 'ERROR'.
Final Answer:
hdfs dfs -cat /var/log/hadoop/* | grep ERROR -> Option A