PageRenderTime 66ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.2.0-rc0/src/java/org/apache/hcatalog/mapreduce/HCatBaseOutputCommitter.java

#
Java | 116 lines | 78 code | 18 blank | 20 comment | 11 complexity | 05ee2601e451e0f7c6412af0df774309 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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.hcatalog.mapreduce;
  19. import java.io.IOException;
  20. import org.apache.hadoop.conf.Configuration;
  21. import org.apache.hadoop.fs.FileSystem;
  22. import org.apache.hadoop.fs.Path;
  23. import org.apache.hadoop.mapreduce.JobContext;
  24. import org.apache.hadoop.mapreduce.JobStatus.State;
  25. import org.apache.hadoop.mapreduce.OutputCommitter;
  26. import org.apache.hadoop.mapreduce.TaskAttemptContext;
  27. import org.apache.hcatalog.common.HCatException;
  28. public abstract class HCatBaseOutputCommitter extends OutputCommitter {
  29. /** The underlying output committer */
  30. protected final OutputCommitter baseCommitter;
  31. public HCatBaseOutputCommitter(JobContext context, OutputCommitter baseCommitter) {
  32. this.baseCommitter = baseCommitter;
  33. }
  34. @Override
  35. public void abortTask(TaskAttemptContext context) throws IOException {
  36. baseCommitter.abortTask(context);
  37. }
  38. @Override
  39. public void commitTask(TaskAttemptContext context) throws IOException {
  40. baseCommitter.commitTask(context);
  41. }
  42. @Override
  43. public boolean needsTaskCommit(TaskAttemptContext context) throws IOException {
  44. return baseCommitter.needsTaskCommit(context);
  45. }
  46. @Override
  47. public void setupJob(JobContext context) throws IOException {
  48. if( baseCommitter != null ) {
  49. baseCommitter.setupJob(context);
  50. }
  51. }
  52. @Override
  53. public void setupTask(TaskAttemptContext context) throws IOException {
  54. baseCommitter.setupTask(context);
  55. }
  56. @Override
  57. public void abortJob(JobContext jobContext, State state) throws IOException {
  58. if(baseCommitter != null) {
  59. baseCommitter.abortJob(jobContext, state);
  60. }
  61. OutputJobInfo jobInfo = HCatOutputFormat.getJobInfo(jobContext);
  62. doAbortJob(jobContext, jobInfo);
  63. Path src = new Path(jobInfo.getLocation());
  64. FileSystem fs = src.getFileSystem(jobContext.getConfiguration());
  65. fs.delete(src, true);
  66. }
  67. protected void doAbortJob(JobContext jobContext, OutputJobInfo jobInfo) throws HCatException {
  68. }
  69. public static final String SUCCEEDED_FILE_NAME = "_SUCCESS";
  70. static final String SUCCESSFUL_JOB_OUTPUT_DIR_MARKER =
  71. "mapreduce.fileoutputcommitter.marksuccessfuljobs";
  72. private static boolean getOutputDirMarking(Configuration conf) {
  73. return conf.getBoolean(SUCCESSFUL_JOB_OUTPUT_DIR_MARKER,
  74. false);
  75. }
  76. @Override
  77. public void commitJob(JobContext jobContext) throws IOException {
  78. if(baseCommitter != null) {
  79. baseCommitter.commitJob(jobContext);
  80. }
  81. // create _SUCCESS FILE if so requested.
  82. OutputJobInfo jobInfo = HCatOutputFormat.getJobInfo(jobContext);
  83. if(getOutputDirMarking(jobContext.getConfiguration())) {
  84. Path outputPath = new Path(jobInfo.getLocation());
  85. if (outputPath != null) {
  86. FileSystem fileSys = outputPath.getFileSystem(jobContext.getConfiguration());
  87. // create a file in the folder to mark it
  88. if (fileSys.exists(outputPath)) {
  89. Path filePath = new Path(outputPath, SUCCEEDED_FILE_NAME);
  90. if(!fileSys.exists(filePath)) { // may have been created by baseCommitter.commitJob()
  91. fileSys.create(filePath).close();
  92. }
  93. }
  94. }
  95. }
  96. cleanupJob(jobContext);
  97. }
  98. }