generated from CodelyTV/scala-basic-skeleton.g8
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add spark dependencies and test (#1)
- Loading branch information
Showing
5 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
package $package$ | ||
|
||
import org.scalatest.wordspec.AnyWordSpec | ||
import org.scalatest.matchers.should._ | ||
import org.apache.spark.sql.Row | ||
|
||
final class $name;format="Camel"$Test extends AnyWordSpec with Matchers { | ||
final class $name;format="Camel"$Test extends SparkTestHelper { | ||
"$name;format="Camel"$" should { | ||
"greet" in { | ||
val $name;format="camel"$ = new $name;format="Camel"$ | ||
|
||
val nameToGreet = "Codely" | ||
val greeting = $name;format="camel"$.greet(nameToGreet) | ||
|
||
greeting shouldBe "Hello " + nameToGreet | ||
import testSQLImplicits._ | ||
Seq(greeting).toDF("greeting").collect() shouldBe Array(Row("Hello Codely")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package $package$ | ||
|
||
import org.apache.commons.io.FileUtils | ||
import org.apache.spark.sql.{SQLContext, SQLImplicits, SparkSession} | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import java.io.File | ||
import java.nio.file.Files | ||
import scala.reflect.io.Directory | ||
|
||
trait SparkTestHelper | ||
extends AnyWordSpec | ||
with BeforeAndAfterEach | ||
with BeforeAndAfterAll | ||
with Matchers { | ||
|
||
private val sparkSession = SparkSession | ||
.builder() | ||
.master("local[*]") | ||
.appName("test-spark-session") | ||
.config(sparkConfiguration) | ||
//.enableHiveSupport() uncomment this if you want to use Hive | ||
.getOrCreate() | ||
|
||
protected var tempDir: String = _ | ||
|
||
protected implicit def spark: SparkSession = sparkSession | ||
|
||
protected def sc: SparkContext = sparkSession.sparkContext | ||
|
||
protected def sparkConfiguration: SparkConf = | ||
new SparkConf() | ||
/* Uncomment this if you want to use Delta Lake | ||
.set("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") | ||
.set( | ||
"spark.sql.catalog.spark_catalog", | ||
"org.apache.spark.sql.delta.catalog.DeltaCatalog" | ||
) | ||
*/ | ||
|
||
override protected def beforeAll(): Unit = { | ||
super.beforeAll() | ||
clearTemporaryDirectories() | ||
} | ||
|
||
override protected def beforeEach(): Unit = { | ||
super.beforeEach() | ||
tempDir = Files.createTempDirectory(this.getClass.toString).toString | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
super.afterAll() | ||
sparkSession.stop() | ||
SparkSession.clearActiveSession() | ||
SparkSession.clearDefaultSession() | ||
clearTemporaryDirectories() | ||
} | ||
|
||
override protected def afterEach(): Unit = { | ||
super.afterEach() | ||
new Directory(new File(tempDir)).deleteRecursively() | ||
spark.sharedState.cacheManager.clearCache() | ||
spark.sessionState.catalog.reset() | ||
} | ||
|
||
protected object testSQLImplicits extends SQLImplicits { | ||
protected override def _sqlContext: SQLContext = sparkSession.sqlContext | ||
} | ||
|
||
private def clearTemporaryDirectories(): Unit = { | ||
val warehousePath = new File("spark-warehouse").getAbsolutePath | ||
FileUtils.deleteDirectory(new File(warehousePath)) | ||
|
||
val metastoreDbPath = new File("metastore_db").getAbsolutePath | ||
FileUtils.deleteDirectory(new File(metastoreDbPath)) | ||
} | ||
} |