TestEmptyJobWithDFS.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.mapred;
  19. import java.io.IOException;
  20. import junit.framework.TestCase;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.hadoop.conf.Configuration;
  24. import org.apache.hadoop.dfs.MiniDFSCluster;
  25. import org.apache.hadoop.fs.FileSystem;
  26. import org.apache.hadoop.fs.Path;
  27. import org.apache.hadoop.io.IntWritable;
  28. import org.apache.hadoop.io.Text;
  29. import org.apache.hadoop.mapred.lib.IdentityMapper;
  30. import org.apache.hadoop.mapred.lib.IdentityReducer;
  31. /**
  32. * A JUnit test to test Map-Reduce empty jobs Mini-DFS.
  33. */
  34. public class TestEmptyJobWithDFS extends TestCase {
  35. private static final Log LOG =
  36. LogFactory.getLog(TestEmptyJobWithDFS.class.getName());
  37. /**
  38. * Simple method running a MapReduce job with no input data. Used
  39. * to test that such a job is successful.
  40. * @param fileSys
  41. * @param jobTracker
  42. * @param conf
  43. * @param numMaps
  44. * @param numReduces
  45. * @return true if the MR job is successful, otherwise false
  46. * @throws IOException
  47. */
  48. public static boolean launchEmptyJob(String fileSys,
  49. String jobTracker,
  50. JobConf conf,
  51. int numMaps,
  52. int numReduces) throws IOException {
  53. // create an empty input dir
  54. final Path inDir = new Path("/testing/empty/input");
  55. final Path outDir = new Path("/testing/empty/output");
  56. FileSystem fs = FileSystem.getNamed(fileSys, conf);
  57. fs.delete(outDir, true);
  58. if (!fs.mkdirs(inDir)) {
  59. LOG.warn("Can't create " + inDir);
  60. return false;
  61. }
  62. // use WordCount example
  63. conf.set("fs.default.name", fileSys);
  64. conf.set("mapred.job.tracker", jobTracker);
  65. conf.setJobName("empty");
  66. // use an InputFormat which returns no split
  67. conf.setInputFormat(EmptyInputFormat.class);
  68. conf.setOutputKeyClass(Text.class);
  69. conf.setOutputValueClass(IntWritable.class);
  70. conf.setMapperClass(IdentityMapper.class);
  71. conf.setReducerClass(IdentityReducer.class);
  72. conf.setInputPath(inDir);
  73. conf.setOutputPath(outDir);
  74. conf.setNumMapTasks(numMaps);
  75. conf.setNumReduceTasks(numReduces);
  76. // run job and wait for completion
  77. JobClient jc = new JobClient(conf);
  78. RunningJob runningJob = jc.submitJob(conf);
  79. while (true) {
  80. try {
  81. Thread.sleep(1000);
  82. } catch (InterruptedException e) {}
  83. if (runningJob.isComplete()) {
  84. break;
  85. }
  86. }
  87. try {
  88. assertTrue(runningJob.isComplete());
  89. assertTrue(runningJob.isSuccessful());
  90. } catch (NullPointerException npe) {
  91. // This NPE should no more happens
  92. fail("A NPE should not have happened.");
  93. }
  94. // return job result
  95. LOG.info("job is complete: " + runningJob.isSuccessful());
  96. return (runningJob.isSuccessful());
  97. }
  98. /**
  99. * Test that a job with no input data (and thus with no input split and
  100. * no map task to execute) is successful.
  101. * @throws IOException
  102. */
  103. public void testEmptyJobWithDFS() throws IOException {
  104. String namenode = null;
  105. MiniDFSCluster dfs = null;
  106. MiniMRCluster mr = null;
  107. FileSystem fileSys = null;
  108. try {
  109. final int taskTrackers = 4;
  110. final int jobTrackerPort = 60050;
  111. Configuration conf = new Configuration();
  112. dfs = new MiniDFSCluster(conf, 1, true, null);
  113. fileSys = dfs.getFileSystem();
  114. namenode = fileSys.getName();
  115. mr = new MiniMRCluster(taskTrackers, namenode, 2);
  116. final String jobTrackerName = "localhost:" + mr.getJobTrackerPort();
  117. JobConf jobConf = new JobConf();
  118. boolean result;
  119. result = launchEmptyJob(namenode, jobTrackerName, jobConf,
  120. 3, 1);
  121. assertTrue(result);
  122. } finally {
  123. if (fileSys != null) { fileSys.close(); }
  124. if (dfs != null) { dfs.shutdown(); }
  125. if (mr != null) { mr.shutdown(); }
  126. }
  127. }
  128. }