[120] | 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 | |
---|
| 19 | package org.apache.hadoop.mapred; |
---|
| 20 | |
---|
| 21 | import java.io.DataOutputStream; |
---|
| 22 | import java.io.IOException; |
---|
| 23 | |
---|
| 24 | import junit.framework.TestCase; |
---|
| 25 | |
---|
| 26 | import org.apache.commons.logging.Log; |
---|
| 27 | import org.apache.commons.logging.LogFactory; |
---|
| 28 | import org.apache.hadoop.conf.Configuration; |
---|
| 29 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
| 30 | import org.apache.hadoop.fs.FileSystem; |
---|
| 31 | import org.apache.hadoop.fs.Path; |
---|
| 32 | import org.apache.hadoop.io.LongWritable; |
---|
| 33 | import org.apache.hadoop.io.Text; |
---|
| 34 | import org.apache.hadoop.mapred.lib.IdentityMapper; |
---|
| 35 | import org.apache.hadoop.mapred.lib.IdentityReducer; |
---|
| 36 | import org.apache.hadoop.util.Progressable; |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * A JUnit test to test that jobs' output filenames are not HTML-encoded (cf HADOOP-1795). |
---|
| 40 | */ |
---|
| 41 | public class TestSpecialCharactersInOutputPath extends TestCase { |
---|
| 42 | private static final Log LOG = |
---|
| 43 | LogFactory.getLog(TestSpecialCharactersInOutputPath.class.getName()); |
---|
| 44 | |
---|
| 45 | private static final String OUTPUT_FILENAME = "result[0]"; |
---|
| 46 | |
---|
| 47 | public static boolean launchJob(String fileSys, |
---|
| 48 | String jobTracker, |
---|
| 49 | JobConf conf, |
---|
| 50 | int numMaps, |
---|
| 51 | int numReduces) throws IOException { |
---|
| 52 | |
---|
| 53 | final Path inDir = new Path("/testing/input"); |
---|
| 54 | final Path outDir = new Path("/testing/output"); |
---|
| 55 | FileSystem fs = FileSystem.getNamed(fileSys, conf); |
---|
| 56 | fs.delete(outDir, true); |
---|
| 57 | if (!fs.mkdirs(inDir)) { |
---|
| 58 | LOG.warn("Can't create " + inDir); |
---|
| 59 | return false; |
---|
| 60 | } |
---|
| 61 | // generate an input file |
---|
| 62 | DataOutputStream file = fs.create(new Path(inDir, "part-0")); |
---|
| 63 | file.writeBytes("foo foo2 foo3"); |
---|
| 64 | file.close(); |
---|
| 65 | |
---|
| 66 | // use WordCount example |
---|
| 67 | FileSystem.setDefaultUri(conf, fileSys); |
---|
| 68 | conf.set("mapred.job.tracker", jobTracker); |
---|
| 69 | conf.setJobName("foo"); |
---|
| 70 | |
---|
| 71 | conf.setInputFormat(TextInputFormat.class); |
---|
| 72 | conf.setOutputFormat(SpecialTextOutputFormat.class); |
---|
| 73 | conf.setOutputKeyClass(LongWritable.class); |
---|
| 74 | conf.setOutputValueClass(Text.class); |
---|
| 75 | conf.setMapperClass(IdentityMapper.class); |
---|
| 76 | conf.setReducerClass(IdentityReducer.class); |
---|
| 77 | FileInputFormat.setInputPaths(conf, inDir); |
---|
| 78 | FileOutputFormat.setOutputPath(conf, outDir); |
---|
| 79 | conf.setNumMapTasks(numMaps); |
---|
| 80 | conf.setNumReduceTasks(numReduces); |
---|
| 81 | |
---|
| 82 | // run job and wait for completion |
---|
| 83 | RunningJob runningJob = JobClient.runJob(conf); |
---|
| 84 | |
---|
| 85 | try { |
---|
| 86 | assertTrue(runningJob.isComplete()); |
---|
| 87 | assertTrue(runningJob.isSuccessful()); |
---|
| 88 | assertTrue("Output folder not found!", fs.exists(new Path("/testing/output/" + OUTPUT_FILENAME))); |
---|
| 89 | } catch (NullPointerException npe) { |
---|
| 90 | // This NPE should no more happens |
---|
| 91 | fail("A NPE should not have happened."); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // return job result |
---|
| 95 | LOG.info("job is complete: " + runningJob.isSuccessful()); |
---|
| 96 | return (runningJob.isSuccessful()); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public void testJobWithDFS() throws IOException { |
---|
| 100 | String namenode = null; |
---|
| 101 | MiniDFSCluster dfs = null; |
---|
| 102 | MiniMRCluster mr = null; |
---|
| 103 | FileSystem fileSys = null; |
---|
| 104 | try { |
---|
| 105 | final int taskTrackers = 4; |
---|
| 106 | final int jobTrackerPort = 60050; |
---|
| 107 | Configuration conf = new Configuration(); |
---|
| 108 | dfs = new MiniDFSCluster(conf, 1, true, null); |
---|
| 109 | fileSys = dfs.getFileSystem(); |
---|
| 110 | namenode = fileSys.getName(); |
---|
| 111 | mr = new MiniMRCluster(taskTrackers, namenode, 2); |
---|
| 112 | final String jobTrackerName = "localhost:" + mr.getJobTrackerPort(); |
---|
| 113 | JobConf jobConf = new JobConf(); |
---|
| 114 | boolean result; |
---|
| 115 | result = launchJob(namenode, jobTrackerName, jobConf, |
---|
| 116 | 3, 1); |
---|
| 117 | assertTrue(result); |
---|
| 118 | |
---|
| 119 | } finally { |
---|
| 120 | if (dfs != null) { dfs.shutdown(); } |
---|
| 121 | if (mr != null) { mr.shutdown(); } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** generates output filenames with special characters */ |
---|
| 126 | static class SpecialTextOutputFormat<K,V> extends TextOutputFormat<K,V> { |
---|
| 127 | @Override |
---|
| 128 | public RecordWriter<K,V> getRecordWriter(FileSystem ignored, JobConf job, |
---|
| 129 | String name, Progressable progress) throws IOException { |
---|
| 130 | return super.getRecordWriter(ignored, job, OUTPUT_FILENAME, progress); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|