Skip to content

Commit 912201b

Browse files
Merge pull request #1223 from datastax/YannMoisan-SPARKC-458-scala-212-support
Yann moisan sparkc 458 scala 212 support
2 parents 29e31d3 + d55e33d commit 912201b

File tree

13 files changed

+138
-39
lines changed

13 files changed

+138
-39
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ metastore_db
2020
.worksheet
2121
.idea
2222
.idea_modules
23+
*.ipr
24+
*.iws
2325

2426
checkpoint

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ sudo: required
77
dist: trusty
88
scala:
99
- 2.11.12
10+
- 2.12.10
1011

1112
env:
1213
- CASSANDRA_VERSION=2.1.15

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2.4.2
2+
* Support for Scala 2.12 (SPARKC-458)
3+
14
2.4.1
25
* Includes all up to 2.3.3
36

project/Settings.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ object Settings extends Build {
4545
lazy val buildSettings = Seq(
4646
organization := "com.datastax.spark",
4747
version in ThisBuild := currentVersion,
48-
scalaVersion := Versions.scalaVersion,
48+
scalaVersion := Versions.scala211,
49+
crossScalaVersions := Seq(Versions.scala211, Versions.scala212),
4950
crossVersion := CrossVersion.binary,
5051
versionStatus := Versions.status(scalaVersion.value, scalaBinaryVersion.value)
5152
)

project/SparkCassandraConnectorBuild.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object CassandraSparkBuild extends Build {
3939
lazy val cassandraServerProject = Project(
4040
id = "cassandra-server",
4141
base = file(namespace),
42-
settings = defaultSettings ++ Seq(
42+
settings = defaultSettings ++ sparkPackageSettings ++ Seq(
4343
libraryDependencies ++= Seq(Artifacts.cassandraServer % "it", Artifacts.airlift),
4444
Testing.cassandraServerClasspath := {
4545
(fullClasspath in IntegrationTest).value.map(_.data.getAbsoluteFile).mkString(File.pathSeparator)

project/Versions.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import scala.util.Properties
1818
object Versions {
1919

2020

21-
lazy val scalaVersion = "2.11.12"
21+
lazy val scala211 = "2.11.7"
22+
lazy val scala212 = "2.12.8"
2223

2324
/* For `scalaBinaryVersion.value outside an sbt task. */
24-
lazy val scalaBinary = scalaVersion.dropRight(2)
25+
lazy val scalaBinary = scala212.dropRight(2)
2526

2627
val Akka = "2.3.4"
2728
val Cassandra = "3.11.3"

project/plugins.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5")
1818

1919
addSbtPlugin("com.scalapenos" % "sbt-prompt" % "1.0.0")
2020

21-
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.8.0")
21+
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")
2222

23-
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.0.4")
23+
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.8.0")
2424

2525
//SbtAssembly 0.12.0 is included in sbt-spark-package
2626
resolvers += "Spark Packages Main repo" at "https://dl.bintray.com/spark-packages/maven"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.datastax.spark.connector.embedded
2+
3+
import java.io._
4+
import java.net.URLClassLoader
5+
6+
import org.apache.spark.SparkConf
7+
import org.apache.spark.repl.{Main, SparkILoop}
8+
9+
import scala.collection.mutable.ArrayBuffer
10+
import scala.tools.nsc.GenericRunnerSettings
11+
12+
object SparkRepl {
13+
14+
def runInterpreter(input: String, conf: SparkConf): String = {
15+
val in = new BufferedReader(new StringReader(input + "\n"))
16+
val out = new StringWriter()
17+
val cl = getClass.getClassLoader
18+
var paths = new ArrayBuffer[String]
19+
cl match {
20+
case urlLoader: URLClassLoader =>
21+
for (url <- urlLoader.getURLs) {
22+
if (url.getProtocol == "file") {
23+
paths += url.getFile
24+
}
25+
}
26+
case _ =>
27+
}
28+
29+
Main.conf.setAll(conf.getAll)
30+
val interp = new SparkILoop(Some(in), new PrintWriter(out))
31+
Main.interp = interp
32+
val separator = System.getProperty("path.separator")
33+
val settings = new GenericRunnerSettings(s => throw new RuntimeException(s"Scala options error: $s"))
34+
settings.processArguments(List("-classpath", paths.mkString(separator)), true)
35+
interp.process(settings) // Repl starts and goes in loop of R.E.P.L
36+
Main.interp = null
37+
Option(Main.sparkContext).foreach(_.stop())
38+
System.clearProperty("spark.driver.port")
39+
out.toString
40+
}
41+
42+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.datastax.spark.connector.util
2+
3+
import scala.reflect.runtime.universe._
4+
5+
private[connector] object Reflect {
6+
7+
def constructor(tpe: Type): Symbol = tpe.decl(termNames.CONSTRUCTOR)
8+
9+
def member(tpe: Type, name: String): Symbol = tpe.member(TermName(name))
10+
11+
def methodSymbol(tpe: Type): MethodSymbol = {
12+
val constructors = constructor(tpe).asTerm.alternatives.map(_.asMethod)
13+
val paramCount = constructors.map(_.paramLists.flatten.size).max
14+
constructors.filter(_.paramLists.flatten.size == paramCount) match {
15+
case List(onlyOne) => onlyOne
16+
case _ => throw new IllegalArgumentException(
17+
"Multiple constructors with the same number of parameters not allowed.")
18+
}
19+
}
20+
}
21+

spark-cassandra-connector/src/it/java/com/datastax/spark/connector/CassandraJavaUtilTest.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import java.util.Map;
66
import java.util.Set;
77

8+
import com.datastax.spark.connector.japi.CassandraJavaUtil;
9+
import org.hamcrest.BaseMatcher;
10+
import org.hamcrest.Description;
11+
import org.hamcrest.Matcher;
812
import scala.reflect.api.TypeTags;
913

1014
import static com.datastax.spark.connector.japi.CassandraJavaUtil.*;
@@ -31,61 +35,71 @@
3135
@SuppressWarnings("unchecked")
3236
public class CassandraJavaUtilTest {
3337

38+
/**
39+
* Scala refelection type tags change the string reprsentation of some types, in scala 2.11 java.lang
40+
* is included, in scala 2.12 it is removed. To remove this conflict we just always remove the java.lang
41+
* portion
42+
*/
43+
private String removeJavaLang(String target) {
44+
return target.replaceAll("java.lang.", "");
45+
}
46+
47+
private final String STRING = removeJavaLang(String.class.getName());
48+
private final String LIST_STRING =
49+
removeJavaLang(String.format("%s[%s]", List.class.getName(), String.class.getName()));
50+
private final String MAP_STRING_INT =
51+
removeJavaLang(String.format("%s[%s,%s]", Map.class.getName(), String.class.getName(), Integer.class.getName()));
52+
private final String LIST_SET_MAP_STRING_INT =
53+
removeJavaLang(String.format("%s[%s[%s[%s,%s]]]", List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getName(), Integer.class.getName()));
54+
3455
@Test
3556
public void testTypeTag1() throws Exception {
3657
TypeTags.TypeTag<String> tt = typeTag(String.class);
37-
assertThat(tt.tpe().toString(), is(String.class.getName()));
58+
assertThat(removeJavaLang(tt.tpe().toString()), is(STRING));
3859
}
3960

4061
@Test
4162
public void testTypeTag2() throws Exception {
4263
TypeTags.TypeTag<List> tt1 = typeTag(List.class, String.class);
43-
assertThat(tt1.tpe().toString(), is(String.format("%s[%s]",
44-
List.class.getName(), String.class.getName())));
64+
assertThat(removeJavaLang(removeJavaLang(tt1.tpe().toString())), is(LIST_STRING));
4565

4666
TypeTags.TypeTag<Map> tt2 = typeTag(Map.class, String.class, Integer.class);
47-
assertThat(tt2.tpe().toString(), is(String.format("%s[%s,%s]",
48-
Map.class.getName(), String.class.getName(), Integer.class.getName())));
67+
assertThat(removeJavaLang(removeJavaLang(tt2.tpe().toString())), is(MAP_STRING_INT));
4968
}
5069

5170
@Test
5271
public void testTypeTag3() throws Exception {
5372
TypeTags.TypeTag<List> tt = typeTag(List.class, typeTag(Set.class, typeTag(Map.class, typeTag(String.class), typeTag(Integer.class))));
54-
assertThat(tt.tpe().toString(), is(String.format("%s[%s[%s[%s,%s]]]",
55-
List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getName(), Integer.class.getName())));
73+
assertThat(removeJavaLang(tt.tpe().toString()), is(LIST_SET_MAP_STRING_INT));
5674
}
5775

5876
@Test
5977
public void testTypeConverter1() throws Exception {
6078
TypeConverter<List<String>> tc = typeConverter(String.class);
61-
assertThat(tc.targetTypeName(), is(String.class.getSimpleName()));
79+
assertThat(removeJavaLang(tc.targetTypeName()), is(STRING));
6280
}
6381

6482
@Test
6583
public void testTypeConverter2() throws Exception {
6684
TypeConverter<List<String>> tc1 = typeConverter(List.class, String.class);
67-
assertThat(tc1.targetTypeName(), is(String.format("%s[%s]",
68-
List.class.getName(), String.class.getSimpleName())));
85+
assertThat(removeJavaLang(tc1.targetTypeName()), is(LIST_STRING));
6986

7087
TypeConverter<Map<String, Integer>> tc2 = typeConverter(Map.class, String.class, Integer.class);
71-
assertThat(tc2.targetTypeName(), is(String.format("%s[%s,%s]",
72-
Map.class.getName(), String.class.getSimpleName(), Integer.class.getName())));
88+
assertThat(removeJavaLang(tc2.targetTypeName()), is(MAP_STRING_INT));
7389

7490
}
7591

7692
@Test
7793
public void testTypeConverter3() throws Exception {
7894
TypeConverter<List> tc = typeConverter(List.class, typeTag(Set.class, typeTag(Map.class, typeTag(String.class), typeTag(Integer.class))));
79-
assertThat(tc.targetTypeName(), is(String.format("%s[%s[%s[%s,%s]]]",
80-
List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getSimpleName(), Integer.class.getName())));
95+
assertThat(removeJavaLang(tc.targetTypeName()), is(LIST_SET_MAP_STRING_INT));
8196
}
8297

8398
@Test
8499
public void testTypeConverter4() throws Exception {
85100
TypeTags.TypeTag<List> tt = typeTag(List.class, typeTag(Set.class, typeTag(Map.class, typeTag(String.class), typeTag(Integer.class))));
86101
TypeConverter<List> tc = typeConverter(tt);
87-
assertThat(tc.targetTypeName(), is(String.format("%s[%s[%s[%s,%s]]]",
88-
List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getSimpleName(), Integer.class.getName())));
102+
assertThat(removeJavaLang(tc.targetTypeName()), is(LIST_SET_MAP_STRING_INT));
89103
}
90104

91105
@Test

0 commit comments

Comments
 (0)