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.jobcontrol; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.text.NumberFormat; |
---|
23 | import java.util.Iterator; |
---|
24 | import java.util.List; |
---|
25 | import java.util.Random; |
---|
26 | |
---|
27 | import org.apache.hadoop.conf.Configuration; |
---|
28 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
29 | import org.apache.hadoop.fs.FileSystem; |
---|
30 | import org.apache.hadoop.fs.Path; |
---|
31 | import org.apache.hadoop.io.LongWritable; |
---|
32 | import org.apache.hadoop.io.Text; |
---|
33 | import org.apache.hadoop.mapred.FileInputFormat; |
---|
34 | import org.apache.hadoop.mapred.FileOutputFormat; |
---|
35 | import org.apache.hadoop.mapred.JobConf; |
---|
36 | import org.apache.hadoop.mapred.MapReduceBase; |
---|
37 | import org.apache.hadoop.mapred.Mapper; |
---|
38 | import org.apache.hadoop.mapred.OutputCollector; |
---|
39 | import org.apache.hadoop.mapred.Reducer; |
---|
40 | import org.apache.hadoop.mapred.Reporter; |
---|
41 | |
---|
42 | /** |
---|
43 | * Utility methods used in various Job Control unit tests. |
---|
44 | */ |
---|
45 | public class JobControlTestUtils { |
---|
46 | |
---|
47 | static private Random rand = new Random(); |
---|
48 | |
---|
49 | private static NumberFormat idFormat = NumberFormat.getInstance(); |
---|
50 | |
---|
51 | static { |
---|
52 | idFormat.setMinimumIntegerDigits(4); |
---|
53 | idFormat.setGroupingUsed(false); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Cleans the data from the passed Path in the passed FileSystem. |
---|
58 | * |
---|
59 | * @param fs FileSystem to delete data from. |
---|
60 | * @param dirPath Path to be deleted. |
---|
61 | * @throws IOException If an error occurs cleaning the data. |
---|
62 | */ |
---|
63 | static void cleanData(FileSystem fs, Path dirPath) throws IOException { |
---|
64 | fs.delete(dirPath, true); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Generates a string of random digits. |
---|
69 | * |
---|
70 | * @return A random string. |
---|
71 | */ |
---|
72 | private static String generateRandomWord() { |
---|
73 | return idFormat.format(rand.nextLong()); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Generates a line of random text. |
---|
78 | * |
---|
79 | * @return A line of random text. |
---|
80 | */ |
---|
81 | private static String generateRandomLine() { |
---|
82 | long r = rand.nextLong() % 7; |
---|
83 | long n = r + 20; |
---|
84 | StringBuffer sb = new StringBuffer(); |
---|
85 | for (int i = 0; i < n; i++) { |
---|
86 | sb.append(generateRandomWord()).append(" "); |
---|
87 | } |
---|
88 | sb.append("\n"); |
---|
89 | return sb.toString(); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Generates data that can be used for Job Control tests. |
---|
94 | * |
---|
95 | * @param fs FileSystem to create data in. |
---|
96 | * @param dirPath Path to create the data in. |
---|
97 | * @throws IOException If an error occurs creating the data. |
---|
98 | */ |
---|
99 | static void generateData(FileSystem fs, Path dirPath) throws IOException { |
---|
100 | FSDataOutputStream out = fs.create(new Path(dirPath, "data.txt")); |
---|
101 | for (int i = 0; i < 10000; i++) { |
---|
102 | String line = generateRandomLine(); |
---|
103 | out.write(line.getBytes("UTF-8")); |
---|
104 | } |
---|
105 | out.close(); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Creates a simple copy job. |
---|
110 | * |
---|
111 | * @param indirs List of input directories. |
---|
112 | * @param outdir Output directory. |
---|
113 | * @return JobConf initialised for a simple copy job. |
---|
114 | * @throws Exception If an error occurs creating job configuration. |
---|
115 | */ |
---|
116 | static JobConf createCopyJob(List<Path> indirs, Path outdir) throws Exception { |
---|
117 | |
---|
118 | Configuration defaults = new Configuration(); |
---|
119 | JobConf theJob = new JobConf(defaults, TestJobControl.class); |
---|
120 | theJob.setJobName("DataMoveJob"); |
---|
121 | |
---|
122 | FileInputFormat.setInputPaths(theJob, indirs.toArray(new Path[0])); |
---|
123 | theJob.setMapperClass(DataCopy.class); |
---|
124 | FileOutputFormat.setOutputPath(theJob, outdir); |
---|
125 | theJob.setOutputKeyClass(Text.class); |
---|
126 | theJob.setOutputValueClass(Text.class); |
---|
127 | theJob.setReducerClass(DataCopy.class); |
---|
128 | theJob.setNumMapTasks(12); |
---|
129 | theJob.setNumReduceTasks(4); |
---|
130 | return theJob; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Simple Mapper and Reducer implementation which copies data it reads in. |
---|
135 | */ |
---|
136 | public static class DataCopy extends MapReduceBase implements |
---|
137 | Mapper<LongWritable, Text, Text, Text>, Reducer<Text, Text, Text, Text> { |
---|
138 | public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, |
---|
139 | Reporter reporter) throws IOException { |
---|
140 | output.collect(new Text(key.toString()), value); |
---|
141 | } |
---|
142 | |
---|
143 | public void reduce(Text key, Iterator<Text> values, |
---|
144 | OutputCollector<Text, Text> output, Reporter reporter) |
---|
145 | throws IOException { |
---|
146 | Text dumbKey = new Text(""); |
---|
147 | while (values.hasNext()) { |
---|
148 | Text data = (Text) values.next(); |
---|
149 | output.collect(dumbKey, data); |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|