SparkSessionWrapper
SparkSessionWrapper
를 생성하는 이유는SparkSession
을 시작/중지하는 비용이 크기 때문에SparkSession
을 하나 생성해서 빠르게 코드 수행이 가능하다.
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
package com.github.direcision | |
import org.apache.spark.sql.SparkSession | |
trait SparkSessionWrapper { | |
lazy val spark: SparkSession = { | |
SparkSession | |
.builder() | |
.master("local") | |
.appName("spark app") | |
.getOrCreate() | |
} | |
} |
trait
은 java에서 interface를 생각하면 된다. 사용하기 위해서는with
를 통해 확장이 가능하다.- 여기서
lazy
를 사용하는 이유는 정의된spark
의 변수가 처음 사용될때 코드가 실행되도록- that a val is executed when it is defined
- a lazy val is executed when it is accessed the first time.
getOrCreate
의 메소드는SparkSession
이 있으면 가져오고, 없으면 새로 생성하는 메소드
Lazy Example
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
scala> val x = { println("x"); 15 } | |
x | |
x: Int = 15 | |
scala> lazy val y = { println("y"); 13 } | |
y: Int = <lazy> | |
scala> x | |
res2: Int = 15 | |
scala> y | |
y | |
res3: Int = 13 | |
scala> y | |
res4: Int = 13 |
'우리는 개발자 > Data Engineering' 카테고리의 다른 글
[Spark] 시작할때 살펴보면 좋은 유용한 사이트 모음 (0) | 2020.02.27 |
---|---|
[Spark] Scala Test (Library, Sbt Test) (0) | 2020.02.26 |
[Spark] 자주 사용하는 명령어 모음 (업데이트) (0) | 2020.02.26 |
[Spark] Scala 프로젝트 구성 및 시작하기 (sbt, g8) (0) | 2020.02.26 |
[elasticsearch] es를 intellij 에서 debugging 하기. (0) | 2020.02.26 |