source: proiecte/HadoopJUnit/hadoop-0.20.1/src/mapred/org/apache/hadoop/mapreduce/JobID.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.6 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.mapreduce;
20
21import java.io.DataInput;
22import java.io.DataOutput;
23import java.io.IOException;
24import java.text.NumberFormat;
25
26import org.apache.hadoop.io.Text;
27
28/**
29 * JobID represents the immutable and unique identifier for
30 * the job. JobID consists of two parts. First part
31 * represents the jobtracker identifier, so that jobID to jobtracker map
32 * is defined. For cluster setup this string is the jobtracker
33 * start time, for local setting, it is "local".
34 * Second part of the JobID is the job number. <br>
35 * An example JobID is :
36 * <code>job_200707121733_0003</code> , which represents the third job
37 * running at the jobtracker started at <code>200707121733</code>.
38 * <p>
39 * Applications should never construct or parse JobID strings, but rather
40 * use appropriate constructors or {@link #forName(String)} method.
41 *
42 * @see TaskID
43 * @see TaskAttemptID
44 * @see org.apache.hadoop.mapred.JobTracker#getNewJobId()
45 * @see org.apache.hadoop.mapred.JobTracker#getStartTime()
46 */
47public class JobID extends org.apache.hadoop.mapred.ID 
48                   implements Comparable<ID> {
49  protected static final String JOB = "job";
50  private final Text jtIdentifier;
51 
52  protected static final NumberFormat idFormat = NumberFormat.getInstance();
53  static {
54    idFormat.setGroupingUsed(false);
55    idFormat.setMinimumIntegerDigits(4);
56  }
57 
58  /**
59   * Constructs a JobID object
60   * @param jtIdentifier jobTracker identifier
61   * @param id job number
62   */
63  public JobID(String jtIdentifier, int id) {
64    super(id);
65    this.jtIdentifier = new Text(jtIdentifier);
66  }
67 
68  public JobID() { 
69    jtIdentifier = new Text();
70  }
71 
72  public String getJtIdentifier() {
73    return jtIdentifier.toString();
74  }
75 
76  @Override
77  public boolean equals(Object o) {
78    if (!super.equals(o))
79      return false;
80
81    JobID that = (JobID)o;
82    return this.jtIdentifier.equals(that.jtIdentifier);
83  }
84 
85  /**Compare JobIds by first jtIdentifiers, then by job numbers*/
86  @Override
87  public int compareTo(ID o) {
88    JobID that = (JobID)o;
89    int jtComp = this.jtIdentifier.compareTo(that.jtIdentifier);
90    if(jtComp == 0) {
91      return this.id - that.id;
92    }
93    else return jtComp;
94  }
95 
96  /**
97   * Add the stuff after the "job" prefix to the given builder. This is useful,
98   * because the sub-ids use this substring at the start of their string.
99   * @param builder the builder to append to
100   * @return the builder that was passed in
101   */
102  public StringBuilder appendTo(StringBuilder builder) {
103    builder.append(SEPARATOR);
104    builder.append(jtIdentifier);
105    builder.append(SEPARATOR);
106    builder.append(idFormat.format(id));
107    return builder;
108  }
109
110  @Override
111  public int hashCode() {
112    return jtIdentifier.hashCode() + id;
113  }
114
115  @Override
116  public String toString() {
117    return appendTo(new StringBuilder(JOB)).toString();
118  }
119
120  @Override
121  public void readFields(DataInput in) throws IOException {
122    super.readFields(in);
123    this.jtIdentifier.readFields(in);
124  }
125
126  @Override
127  public void write(DataOutput out) throws IOException {
128    super.write(out);
129    jtIdentifier.write(out);
130  }
131 
132  /** Construct a JobId object from given string
133   * @return constructed JobId object or null if the given String is null
134   * @throws IllegalArgumentException if the given string is malformed
135   */
136  public static JobID forName(String str) throws IllegalArgumentException {
137    if(str == null)
138      return null;
139    try {
140      String[] parts = str.split("_");
141      if(parts.length == 3) {
142        if(parts[0].equals(JOB)) {
143          return new org.apache.hadoop.mapred.JobID(parts[1], 
144                                                    Integer.parseInt(parts[2]));
145        }
146      }
147    }catch (Exception ex) {//fall below
148    }
149    throw new IllegalArgumentException("JobId string : " + str
150        + " is not properly formed");
151  }
152 
153}
Note: See TracBrowser for help on using the repository browser.