/
SparkVersion.java
130 lines (104 loc) · 4.32 KB
/
SparkVersion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zeppelin.spark;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Provide reading comparing capability of spark version returned from SparkContext.version()
*/
public class SparkVersion {
Logger logger = LoggerFactory.getLogger(SparkVersion.class);
public static final SparkVersion SPARK_1_0_0 = SparkVersion.fromVersionString("1.0.0");
public static final SparkVersion SPARK_1_1_0 = SparkVersion.fromVersionString("1.1.0");
public static final SparkVersion SPARK_1_2_0 = SparkVersion.fromVersionString("1.2.0");
public static final SparkVersion SPARK_1_3_0 = SparkVersion.fromVersionString("1.3.0");
public static final SparkVersion SPARK_1_4_0 = SparkVersion.fromVersionString("1.4.0");
public static final SparkVersion SPARK_1_5_0 = SparkVersion.fromVersionString("1.5.0");
public static final SparkVersion SPARK_1_6_0 = SparkVersion.fromVersionString("1.6.0");
public static final SparkVersion SPARK_2_0_0 = SparkVersion.fromVersionString("2.0.0");
public static final SparkVersion SPARK_2_2_0 = SparkVersion.fromVersionString("2.2.0");
public static final SparkVersion MIN_SUPPORTED_VERSION = SPARK_1_0_0;
public static final SparkVersion UNSUPPORTED_FUTURE_VERSION = SPARK_2_2_0;
private int version;
private String versionString;
SparkVersion(String versionString) {
this.versionString = versionString;
try {
int pos = versionString.indexOf('-');
String numberPart = versionString;
if (pos > 0) {
numberPart = versionString.substring(0, pos);
}
String versions[] = numberPart.split("\\.");
int major = Integer.parseInt(versions[0]);
int minor = Integer.parseInt(versions[1]);
int patch = Integer.parseInt(versions[2]);
// version is always 5 digits. (e.g. 2.0.0 -> 20000, 1.6.2 -> 10602)
version = Integer.parseInt(String.format("%d%02d%02d", major, minor, patch));
} catch (Exception e) {
logger.error("Can not recognize Spark version " + versionString +
". Assume it's a future release", e);
// assume it is future release
version = 99999;
}
}
public int toNumber() {
return version;
}
public String toString() {
return versionString;
}
public boolean isUnsupportedVersion() {
return olderThan(MIN_SUPPORTED_VERSION) || newerThanEquals(UNSUPPORTED_FUTURE_VERSION);
}
public static SparkVersion fromVersionString(String versionString) {
return new SparkVersion(versionString);
}
public boolean isPysparkSupported() {
return this.newerThanEquals(SPARK_1_2_0);
}
public boolean isSparkRSupported() {
return this.newerThanEquals(SPARK_1_4_0);
}
public boolean hasDataFrame() {
return this.newerThanEquals(SPARK_1_4_0);
}
public boolean getProgress1_0() {
return this.olderThan(SPARK_1_1_0);
}
public boolean oldLoadFilesMethodName() {
return this.olderThan(SPARK_1_3_0);
}
public boolean oldSqlContextImplicits() {
return this.olderThan(SPARK_1_3_0);
}
public boolean equals(Object versionToCompare) {
return version == ((SparkVersion) versionToCompare).version;
}
public boolean newerThan(SparkVersion versionToCompare) {
return version > versionToCompare.version;
}
public boolean newerThanEquals(SparkVersion versionToCompare) {
return version >= versionToCompare.version;
}
public boolean olderThan(SparkVersion versionToCompare) {
return version < versionToCompare.version;
}
public boolean olderThanEquals(SparkVersion versionToCompare) {
return version <= versionToCompare.version;
}
}