Skip to content

Commit 0b1bd98

Browse files
committed
Fixes #20
Signed-off-by: Pasha Finkelshteyn <[email protected]>
1 parent 8757094 commit 0b1bd98

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

kotlin-spark-api/src/main/kotlin/org/jetbrains/spark/api/ApiV1.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package org.jetbrains.spark.api
2323

24+
import org.apache.spark.SparkContext
2425
import org.apache.spark.api.java.function.*
2526
import org.apache.spark.sql.*
2627
import org.apache.spark.sql.Encoders.*
@@ -298,6 +299,12 @@ fun schema(type: KType, map: Map<String, KType> = mapOf()): DataType {
298299
}
299300
}
300301

302+
fun SparkContext.setLogLevel(level: SparkLogLevel) = setLogLevel(level.name)
303+
304+
enum class SparkLogLevel {
305+
ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE, WARN
306+
}
307+
301308
private val knownDataTypes = mapOf(
302309
Byte::class to DataTypes.ByteType,
303310
Short::class to DataTypes.ShortType,

kotlin-spark-api/src/main/kotlin/org/jetbrains/spark/api/SparkHelper.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.jetbrains.spark.api
2121

2222
import org.apache.spark.sql.SparkSession
23+
import org.jetbrains.spark.api.SparkLogLevel.ERROR
2324

2425
/**
2526
* Wrapper for spark creation which allows to set different spark params
@@ -29,7 +30,8 @@ import org.apache.spark.sql.SparkSession
2930
* @param appName [SparkSession.Builder.appName]
3031
* @param func function which will be executed in context of [KSparkSession] (it means that `this` inside block will point to [KSparkSession])
3132
*/
32-
inline fun withSpark(props: Map<String, Any> = emptyMap(), master: String = "local[*]", appName: String = "Kotlin Spark Sample", func: KSparkSession.() -> Unit) {
33+
@JvmOverloads
34+
inline fun withSpark(props: Map<String, Any> = emptyMap(), master: String = "local[*]", appName: String = "Kotlin Spark Sample", logLevel: SparkLogLevel = ERROR, func: KSparkSession.() -> Unit) {
3335
SparkSession
3436
.builder()
3537
.master(master)
@@ -46,7 +48,12 @@ inline fun withSpark(props: Map<String, Any> = emptyMap(), master: String = "loc
4648
}
4749
}
4850
.orCreate
49-
.apply { KSparkSession(this).apply(func) }
51+
.apply {
52+
KSparkSession(this).apply {
53+
sparkContext.setLogLevel(logLevel)
54+
func()
55+
}
56+
}
5057
.also { it.stop() }
5158
}
5259

0 commit comments

Comments
 (0)