Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.

Commit 95a329b

Browse files
author
Thomas Risberg
committed
SHDP-316 Remove ConfigurationUtils dependency on JobConf class
1 parent 284d7cc commit 95a329b

File tree

8 files changed

+175
-43
lines changed

8 files changed

+175
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2011-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.hadoop.configuration;
17+
18+
import org.apache.hadoop.conf.Configuration;
19+
import org.apache.hadoop.fs.FileSystem;
20+
import org.apache.hadoop.mapred.JobConf;
21+
import org.apache.hadoop.mapreduce.Job;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.springframework.data.hadoop.util.VersionUtils;
25+
import org.springframework.util.ReflectionUtils;
26+
27+
import java.lang.reflect.Method;
28+
import java.util.Properties;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertTrue;
33+
34+
/**
35+
* @author Thomas Risberg
36+
*/
37+
public class ConfigurationUtilsTest {
38+
39+
@Test
40+
public void testConfigurationCreation() throws Exception {
41+
Properties prop = new Properties();
42+
prop.setProperty("name", "test1");
43+
Configuration cfg = ConfigurationUtils.createFrom(null, prop);
44+
assertEquals("test1", cfg.get("name"));
45+
Configuration cfg2 = ConfigurationUtils.createFrom(cfg, prop);
46+
assertEquals("test1", cfg2.get("name"));
47+
assertTrue(cfg2 instanceof Configuration);
48+
assertTrue(!(cfg2 instanceof JobConf));
49+
}
50+
51+
@Test
52+
public void testJobConfCreation() throws Exception {
53+
Properties prop = new Properties();
54+
prop.setProperty("name", "test2");
55+
Configuration cfg = JobConfUtils.createFrom(null, prop);
56+
assertEquals("test2", cfg.get("name"));
57+
Configuration cfg2 = ConfigurationUtils.createFrom(cfg, prop);
58+
assertEquals("test2", cfg2.get("name"));
59+
assertTrue(cfg2 instanceof JobConf);
60+
}
61+
62+
@Test
63+
public void testConfigurationProperties() throws Exception {
64+
Properties prop = new Properties();
65+
prop.setProperty("mapred.reduce.tasks", "8");
66+
prop.setProperty("mapred.map.tasks", "4");
67+
68+
Configuration cfg = ConfigurationUtils.createFrom(null, prop);
69+
70+
assertEquals("8", cfg.get("mapred.reduce.tasks"));
71+
assertEquals("4", cfg.get("mapred.map.tasks"));
72+
@SuppressWarnings("deprecation")
73+
Job j = new Job(cfg);
74+
assertEquals("8", j.getConfiguration().get("mapred.reduce.tasks"));
75+
assertEquals("4", j.getConfiguration().get("mapred.map.tasks"));
76+
}
77+
}

spring-hadoop-build-tests/src/test/java/org/springframework/data/hadoop/configuration/UrlInfiniteLoopTest.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,4 @@ public void testInfiniteLoop() throws Exception {
6868
assertNotNull(fs);
6969
}
7070

71-
@Test
72-
public void testConfigurationProperties() throws Exception {
73-
Properties prop = new Properties();
74-
prop.setProperty("mapred.reduce.tasks", "8");
75-
prop.setProperty("mapred.map.tasks", "4");
76-
77-
Configuration cfg = ConfigurationUtils.createFrom(null, prop);
78-
79-
assertEquals("8", cfg.get("mapred.reduce.tasks"));
80-
assertEquals("4", cfg.get("mapred.map.tasks"));
81-
@SuppressWarnings("deprecation")
82-
Job j = new Job(cfg);
83-
assertEquals("8", j.getConfiguration().get("mapred.reduce.tasks"));
84-
assertEquals("4", j.getConfiguration().get("mapred.map.tasks"));
85-
}
8671
}

spring-hadoop-core/src/main/java/org/springframework/data/hadoop/configuration/ConfigurationUtils.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Properties;
2424

2525
import org.apache.hadoop.conf.Configuration;
26-
import org.apache.hadoop.mapred.JobConf;
2726
import org.apache.hadoop.util.GenericOptionsParser;
2827
import org.springframework.core.io.Resource;
2928
import org.springframework.util.Assert;
@@ -63,28 +62,20 @@ public static void addProperties(Configuration configuration, Properties propert
6362
public static Configuration createFrom(Configuration original, Properties properties) {
6463
Configuration cfg = null;
6564
if (original != null) {
66-
cfg = (original instanceof JobConf ? new JobConf(original) : new Configuration(original));
65+
if ("org.apache.hadoop.mapred.JobConf".equals(original.getClass().getName())) {
66+
cfg = JobConfUtils.createFrom(original, properties);
67+
} else {
68+
cfg = new Configuration(original);
69+
addProperties(cfg, properties);
70+
}
6771
}
6872
else {
69-
cfg = new JobConf();
73+
cfg = new Configuration();
74+
addProperties(cfg, properties);
7075
}
71-
addProperties(cfg, properties);
7276
return cfg;
7377
}
7478

75-
/**
76-
* Creates a new {@link JobConf} based on the given arguments. Identical to
77-
* {@link #createFrom(Configuration, Properties)} but forces the use of
78-
* {@link JobConf}.
79-
*
80-
* @param original initial configuration to read from. May be null.
81-
* @param properties properties object to add to the newly created configuration. May be null.
82-
* @return newly created configuration based on the input parameters.
83-
*/
84-
public static JobConf createFrom(JobConf original, Properties properties) {
85-
return (JobConf) createFrom((Configuration) original, properties);
86-
}
87-
8879
/**
8980
* Returns a static {@link Properties} copy of the given configuration.
9081
*
@@ -113,12 +104,12 @@ public static Properties asProperties(Configuration configuration) {
113104
public static Configuration merge(Configuration one, Configuration two) {
114105
if (one == null) {
115106
if (two == null) {
116-
return new JobConf();
107+
return new Configuration();
117108
}
118-
return new JobConf(two);
109+
return new Configuration(two);
119110
}
120111

121-
Configuration c = new JobConf(one);
112+
Configuration c = new Configuration(one);
122113

123114
if (two == null) {
124115
return c;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2011-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.hadoop.configuration;
17+
18+
import org.apache.hadoop.conf.Configuration;
19+
import org.apache.hadoop.mapred.JobConf;
20+
21+
import java.util.Map;
22+
import java.util.Properties;
23+
24+
/**
25+
* Reusable utility class for common {@link org.apache.hadoop.mapred.JobConf} operations.
26+
*
27+
* @author Thomas Risberg
28+
*/
29+
public abstract class JobConfUtils {
30+
31+
/**
32+
* Creates a new {@link org.apache.hadoop.mapred.JobConf} based on the given arguments.
33+
*
34+
* @param original initial configuration to read from. May be null.
35+
* @param properties properties object to add to the newly created configuration. May be null.
36+
* @return newly created configuration based on the input parameters.
37+
*/
38+
public static JobConf createFrom(Configuration original, Properties properties) {
39+
JobConf cfg = null;
40+
if (original != null) {
41+
cfg = new JobConf(original);
42+
}
43+
else {
44+
cfg = new JobConf();
45+
}
46+
ConfigurationUtils.addProperties(cfg, properties);
47+
return cfg;
48+
}
49+
50+
/**
51+
* Creates a new {@link org.apache.hadoop.conf.Configuration} by merging the given configurations.
52+
* Ordering is important - the second configuration overriding values in the first.
53+
*
54+
* @param one configuration to read from. May be null.
55+
* @param two configuration to read from. May be null.
56+
* @return the result of merging the two configurations.
57+
*/
58+
public static JobConf merge(Configuration one, Configuration two) {
59+
if (one == null) {
60+
if (two == null) {
61+
return new JobConf();
62+
}
63+
return new JobConf(two);
64+
}
65+
66+
JobConf c = new JobConf(one);
67+
68+
if (two == null) {
69+
return c;
70+
}
71+
72+
for (Map.Entry<String, String> entry : two) {
73+
c.set(entry.getKey(), entry.getValue());
74+
}
75+
76+
return c;
77+
}
78+
79+
}

spring-hadoop-core/src/main/java/org/springframework/data/hadoop/mapreduce/HadoopCodeExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.beans.factory.BeanClassLoaderAware;
2626
import org.springframework.beans.factory.InitializingBean;
2727
import org.springframework.core.io.Resource;
28-
import org.springframework.data.hadoop.configuration.ConfigurationUtils;
28+
import org.springframework.data.hadoop.configuration.JobConfUtils;
2929
import org.springframework.data.hadoop.mapreduce.ExecutionUtils.ExitTrapped;
3030
import org.springframework.util.Assert;
3131
import org.springframework.util.ClassUtils;
@@ -116,7 +116,7 @@ public Integer run() throws Exception {
116116

117117

118118
protected Configuration resolveConfiguration() throws Exception {
119-
Configuration cfg = ConfigurationUtils.createFrom(configuration, properties);
119+
Configuration cfg = JobConfUtils.createFrom(configuration, properties);
120120
// add the jar if present
121121
if (jar != null) {
122122
String jarUrl = jar.getURL().toString();

spring-hadoop-core/src/main/java/org/springframework/data/hadoop/mapreduce/JobFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import org.springframework.beans.factory.InitializingBean;
4444
import org.springframework.core.io.Resource;
4545
import org.springframework.core.io.support.ResourcePatternResolver;
46-
import org.springframework.data.hadoop.configuration.ConfigurationUtils;
46+
import org.springframework.data.hadoop.configuration.JobConfUtils;
4747
import org.springframework.util.Assert;
4848
import org.springframework.util.ClassUtils;
4949
import org.springframework.util.CollectionUtils;
@@ -118,7 +118,7 @@ public boolean isSingleton() {
118118

119119
@SuppressWarnings({ "rawtypes", "deprecation" })
120120
public void afterPropertiesSet() throws Exception {
121-
final Configuration cfg = ConfigurationUtils.createFrom(configuration, properties);
121+
final Configuration cfg = JobConfUtils.createFrom(configuration, properties);
122122

123123
buildGenericOptions(cfg);
124124

spring-hadoop-core/src/main/java/org/springframework/data/hadoop/mapreduce/JobUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.apache.hadoop.mapreduce.Job;
2727
import org.apache.hadoop.mapreduce.Job.JobState;
2828
import org.apache.hadoop.mapreduce.JobID;
29-
import org.springframework.data.hadoop.configuration.ConfigurationUtils;
29+
import org.springframework.data.hadoop.configuration.JobConfUtils;
3030
import org.springframework.data.hadoop.util.VersionUtils;
3131
import org.springframework.util.ReflectionUtils;
3232

@@ -197,7 +197,7 @@ public static JobConf getJobConf(Job job) {
197197
return (JobConf) configuration;
198198
}
199199

200-
return (JobConf) ConfigurationUtils.createFrom(configuration, null);
200+
return JobConfUtils.createFrom(configuration, null);
201201
}
202202

203203
/**

spring-hadoop-core/src/main/java/org/springframework/data/hadoop/mapreduce/StreamJobFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.springframework.beans.factory.BeanNameAware;
3333
import org.springframework.beans.factory.FactoryBean;
3434
import org.springframework.beans.factory.InitializingBean;
35-
import org.springframework.data.hadoop.configuration.ConfigurationUtils;
35+
import org.springframework.data.hadoop.configuration.JobConfUtils;
3636
import org.springframework.util.Assert;
3737
import org.springframework.util.ObjectUtils;
3838
import org.springframework.util.ReflectionUtils;
@@ -80,7 +80,7 @@ public void afterPropertiesSet() throws Exception {
8080
Assert.isTrue(!ObjectUtils.isEmpty(input), "at least one input required");
8181
Assert.hasText(output, "the output is required");
8282

83-
final Configuration cfg = ConfigurationUtils.createFrom(configuration, properties);
83+
final Configuration cfg = JobConfUtils.createFrom(configuration, properties);
8484

8585
buildGenericOptions(cfg);
8686

0 commit comments

Comments
 (0)