Skip to content

Commit 429f107

Browse files
committed
tests
1 parent f130277 commit 429f107

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<plugin>
4141
<groupId>org.scala-tools</groupId>
4242
<artifactId>maven-scala-plugin</artifactId>
43+
<version>2.15.2</version>
4344
<executions>
4445
<execution>
4546
<goals>
@@ -81,6 +82,12 @@
8182
<artifactId>spark-core_2.10</artifactId>
8283
<version>1.2.0-cdh5.3.2</version>
8384
</dependency>
85+
<dependency>
86+
<groupId>junit</groupId>
87+
<artifactId>junit</artifactId>
88+
<version>4.12</version>
89+
<scope>test</scope>
90+
</dependency>
8491
</dependencies>
8592

8693
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2015
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 sparkfun;
17+
18+
import org.apache.spark.SparkConf;
19+
20+
/**
21+
* @author Steffen Remus
22+
*
23+
*/
24+
public abstract class SparkConfigured {
25+
26+
public abstract void run(SparkConf conf, String[] args);
27+
28+
/*
29+
* -key1=val1 --key2=val2 -key3 val3 -key4 val4 val5 -key6=val7 val8 -key9
30+
* -key1=val1 --key2=val2 -key3 val3 -key4 val5 val6
31+
*/
32+
public void run(String[] args){
33+
SparkConf conf = new SparkConf();
34+
String key = "", value = "";
35+
for(int i = 0; i < args.length; i++){
36+
if(args[i].startsWith("-")){
37+
key = args[i].replaceFirst("--?", ""); // match -key, --key
38+
value = "";
39+
}
40+
else
41+
value += " " + args[i];
42+
if(key.contains("=")){
43+
value = key.substring(key.indexOf("=")+1);
44+
key = key.substring(0, key.indexOf("="));
45+
conf.set(key.trim(), value.trim());
46+
continue;
47+
}
48+
if(!key.isEmpty() && !value.isEmpty())
49+
conf.set(key.trim(), value.trim());
50+
}
51+
run(conf, args);
52+
}
53+
54+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2015
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 sparkfun;
17+
18+
import org.apache.spark.SparkConf;
19+
import org.apache.spark.api.java.JavaSparkContext;
20+
21+
import scala.Tuple2;
22+
23+
24+
/**
25+
* @author Steffen Remus
26+
*
27+
*/
28+
public class TestParams extends SparkConfigured{
29+
30+
public void run(SparkConf conf, String[] args) {
31+
32+
conf.setAppName("TestParams");
33+
34+
JavaSparkContext ctx = new JavaSparkContext(conf);
35+
36+
System.out.println(ctx.appName());
37+
System.out.println(ctx.getConf().getOption("testopt"));
38+
39+
for(Tuple2<?,?> t : ctx.getConf().getAll())
40+
System.out.println(t);
41+
System.out.println(ctx.getConf().getOption("spark.master"));
42+
43+
try {
44+
Thread.sleep(10000);
45+
} catch (InterruptedException e) {
46+
// TODO Auto-generated catch block
47+
e.printStackTrace();
48+
}
49+
50+
ctx.close();
51+
52+
}
53+
54+
public static void main(String[] args) {
55+
new TestParams().run(args);
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2015
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 sparkfun;
17+
18+
import java.util.Arrays;
19+
20+
import org.apache.spark.SparkConf;
21+
import org.junit.Test;
22+
23+
import scala.Tuple2;
24+
25+
/**
26+
* @author Steffen Remus
27+
*
28+
*/
29+
public class SparkConfiguredTest {
30+
31+
@Test
32+
public void test() {
33+
SparkConfigured job = new SparkConfigured() {
34+
@Override
35+
public void run(SparkConf conf, String[] args) {
36+
System.out.println(Arrays.toString(args));
37+
for(Tuple2<String, String> t : conf.getAll())
38+
System.out.println(t);
39+
}
40+
};
41+
job.run("-key1=val1 --key2=val2 -key3 val3 -key4 val4.1 val4.2 -key5=val5.1 val5.2 -key6".split(" "));
42+
job.run("-key1=val1 --key2=val2 -key3 val3 -key4 val4.1 val4.2".split(" "));
43+
job.run("---key1=val1".split(" "));
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)