tags:

views:

239

answers:

1

The Task Side-Effect Files section of the Hadoop tutorial mentions using the "attemptid" of the task as a unique name. How do I get this attempt ID in my mapper or reducer?

+2  A: 

If you need a unique id for a side effect file in hadoop, you can leverage the attempt unique id in the job with this code:

   public static String getAttemptId(Configuration conf) throws IllegalArgumentException
   {
       if (conf == null) {
           throw new NullPointerException("conf is null");
       }

       String taskId = conf.get("mapred.task.id");
       if (taskId == null) {
           throw new IllegalArgumentException("Configutaion does not contain the property mapred.task.id");
       }

       String[] parts = taskId.split("_");
       if (parts.length != 6 ||
               !parts[0].equals("attempt") ||
               (!"m".equals(parts[3]) && !"r".equals(parts[3]))) {
           throw new IllegalArgumentException("TaskAttemptId string : " + taskId + " is not properly formed");
       }

       return parts[4] + "-" + parts[5];
   }
Dain Sundstrom