PageRenderTime 125ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/org/apache/hadoop/mapreduce/JobID.java

https://github.com/RS1999ent/hadoop-mapreduce
Java | 162 lines | 89 code | 20 blank | 53 comment | 9 complexity | de439512f2f45c85d1f313af8220ad97 MD5 | raw file
  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. package org.apache.hadoop.mapreduce;
  19. import java.io.DataInput;
  20. import java.io.DataOutput;
  21. import java.io.IOException;
  22. import java.text.NumberFormat;
  23. import org.apache.hadoop.classification.InterfaceAudience;
  24. import org.apache.hadoop.classification.InterfaceStability;
  25. import org.apache.hadoop.io.Text;
  26. /**
  27. * JobID represents the immutable and unique identifier for
  28. * the job. JobID consists of two parts. First part
  29. * represents the jobtracker identifier, so that jobID to jobtracker map
  30. * is defined. For cluster setup this string is the jobtracker
  31. * start time, for local setting, it is "local".
  32. * Second part of the JobID is the job number. <br>
  33. * An example JobID is :
  34. * <code>job_200707121733_0003</code> , which represents the third job
  35. * running at the jobtracker started at <code>200707121733</code>.
  36. * <p>
  37. * Applications should never construct or parse JobID strings, but rather
  38. * use appropriate constructors or {@link #forName(String)} method.
  39. *
  40. * @see TaskID
  41. * @see TaskAttemptID
  42. * @see org.apache.hadoop.mapred.JobTracker#getNewJobId()
  43. * @see org.apache.hadoop.mapred.JobTracker#getStartTime()
  44. */
  45. @InterfaceAudience.Public
  46. @InterfaceStability.Stable
  47. public class JobID extends org.apache.hadoop.mapred.ID
  48. implements Comparable<ID> {
  49. protected static final String JOB = "job";
  50. // Jobid regex for various tools and framework components
  51. public static final String JOBID_REGEX =
  52. JOB + SEPARATOR + "[0-9]+" + SEPARATOR + "[0-9]+";
  53. private final Text jtIdentifier;
  54. protected static final NumberFormat idFormat = NumberFormat.getInstance();
  55. static {
  56. idFormat.setGroupingUsed(false);
  57. idFormat.setMinimumIntegerDigits(4);
  58. }
  59. /**
  60. * Constructs a JobID object
  61. * @param jtIdentifier jobTracker identifier
  62. * @param id job number
  63. */
  64. public JobID(String jtIdentifier, int id) {
  65. super(id);
  66. this.jtIdentifier = new Text(jtIdentifier);
  67. }
  68. public JobID() {
  69. jtIdentifier = new Text();
  70. }
  71. public String getJtIdentifier() {
  72. return jtIdentifier.toString();
  73. }
  74. @Override
  75. public boolean equals(Object o) {
  76. if (!super.equals(o))
  77. return false;
  78. JobID that = (JobID)o;
  79. return this.jtIdentifier.equals(that.jtIdentifier);
  80. }
  81. /**Compare JobIds by first jtIdentifiers, then by job numbers*/
  82. @Override
  83. public int compareTo(ID o) {
  84. JobID that = (JobID)o;
  85. int jtComp = this.jtIdentifier.compareTo(that.jtIdentifier);
  86. if(jtComp == 0) {
  87. return this.id - that.id;
  88. }
  89. else return jtComp;
  90. }
  91. /**
  92. * Add the stuff after the "job" prefix to the given builder. This is useful,
  93. * because the sub-ids use this substring at the start of their string.
  94. * @param builder the builder to append to
  95. * @return the builder that was passed in
  96. */
  97. public StringBuilder appendTo(StringBuilder builder) {
  98. builder.append(SEPARATOR);
  99. builder.append(jtIdentifier);
  100. builder.append(SEPARATOR);
  101. builder.append(idFormat.format(id));
  102. return builder;
  103. }
  104. @Override
  105. public int hashCode() {
  106. return jtIdentifier.hashCode() + id;
  107. }
  108. @Override
  109. public String toString() {
  110. return appendTo(new StringBuilder(JOB)).toString();
  111. }
  112. @Override
  113. public void readFields(DataInput in) throws IOException {
  114. super.readFields(in);
  115. this.jtIdentifier.readFields(in);
  116. }
  117. @Override
  118. public void write(DataOutput out) throws IOException {
  119. super.write(out);
  120. jtIdentifier.write(out);
  121. }
  122. /** Construct a JobId object from given string
  123. * @return constructed JobId object or null if the given String is null
  124. * @throws IllegalArgumentException if the given string is malformed
  125. */
  126. public static JobID forName(String str) throws IllegalArgumentException {
  127. if(str == null)
  128. return null;
  129. try {
  130. String[] parts = str.split("_");
  131. if(parts.length == 3) {
  132. if(parts[0].equals(JOB)) {
  133. return new org.apache.hadoop.mapred.JobID(parts[1],
  134. Integer.parseInt(parts[2]));
  135. }
  136. }
  137. }catch (Exception ex) {//fall below
  138. }
  139. throw new IllegalArgumentException("JobId string : " + str
  140. + " is not properly formed");
  141. }
  142. }