WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Hadoop & Big Data · MapReduce Framework · question 39 of 120

What are counters in Hadoop?

📕 Buy this interview preparation book: 120 Hadoop & Big Data questions & answers — PDF + EPUB for $5

Counters in Hadoop are a mechanism to collect and maintain the statistics of various user-defined or system-defined values during the execution of MapReduce jobs. Counters help monitor and debug the job progress by providing insights into the data flow, and they can be used for various purposes such as error detection, performance analysis, and understanding data distribution.

There are two types of counters in Hadoop:

1. Built-in counters: These are provided by the Hadoop framework and maintain information like input records, output records, bytes read, bytes written, etc. Built-in counters are organized into groups, such as ‘FileSystem‘, ‘FileInputFormat‘, and ‘TaskCounter‘.

2. User-defined counters: These are custom counters created by users to track specific information during the execution of the MapReduce job. You can define these counters by creating an enumeration or by using the ‘Counter‘ class in your code.

Here’s an example of how to use a user-defined counter. Suppose we want to count the number of invalid records in our dataset. We can create a counter called ‘INVALID_RECORDS‘ and increment it every time we find an invalid record during the map or reduce phase.

public static enum COUNTERS {
    INVALID_RECORDS
}

public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {
    private Text word = new Text();
    private final static IntWritable one = new IntWritable(1);

    public void map(Object key, Text value, Context context)
            throws IOException, InterruptedException {
        String[] fields = value.toString().split("t");

        if (fields.length != EXPECTED_NUMBER_OF_FIELDS) {
            context.getCounter(COUNTERS.INVALID_RECORDS).increment(1);
            return;
        }

        // Proceed with mapping logic for valid records
    }
}

After the job execution, you can access the value of this counter using the Job client or by parsing job logs.

In summary, counters in Hadoop offer a useful tool for monitoring, understanding, and debugging the execution of MapReduce jobs. Built-in counters provide insights into data flow statistics, while user-defined counters allow you to track custom information specific to your use case.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Hadoop & Big Data interview — then scores it.
📞 Practice Hadoop & Big Data — free 15 min
📕 Buy this interview preparation book: 120 Hadoop & Big Data questions & answers — PDF + EPUB for $5

All 120 Hadoop & Big Data questions · All topics