spark scala 코딩을 할때 datetime에 관련된 코딩을 한다면 아래 내용은 필수!!!
sbt build를 사용하기 때문에 libraryDependencies
가 필요하다.
build.sbt에 아래와 같이 추가
libraryDependencies ++= Seq(
(...)
"joda-time" % "joda-time" % "2.10.5",
"org.joda" % "joda-convert" % "1.8.1"
),
joda-time
만 설치하면 아마도 아래와 같이 WARN이 발생하니 org.joda
도 함께 라이브러리에 추가
class org.joda.convert.fromstring not found - continuing with a stub
소스코드
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
import org.joda.time.{DateTime, Days} | |
import org.joda.time.format.DateTimeFormat | |
val today = new DateTime(); | |
val yesterday = today.minusDays(30); | |
val dt = new DateTime(2020,1,1,10,5,1,0) | |
val svcStartYmd = "2020-02-01" | |
val curYmd = "2020-02-02" | |
val formatter = DateTimeFormat.forPattern("yyyy-MM-dd") | |
val svcStartDt = formatter.parseDateTime(svcStartYmd) | |
val curDt = formatter.parseDateTime(curYmd) | |
val curDt = svcStartDt.plusDays(0) | |
val days = Days.daysBetween(svcStartDt.toLocalDate(), curDt.toLocalDate()).getDays() // 1, 2, 3 | |
val d1 = new DateTime(); | |
val d2 = new DateTime(); | |
val diffInMillis = d2.getMillis() - d1.getMillis(); |
- 소스코드를 보면
DateTime
,Days
,DateTimeFormat
을 이용 - string을
DateTimeFormat.forPattern
을 이용해 DateTime으로 변환 Days.daysBetween
을 이용해 두개의 DateTime간의 날짜 차이를 계산- 오늘 날짜 DateTime 가져오기
- 30일전 DateTime 생성하기
- 두개의
DateTime
의 차이 milliseconds 계산
'우리는 개발자 > Data Engineering' 카테고리의 다른 글
[Spark] scala DataFrame 생성하기 for 예제 (0) | 2020.02.27 |
---|---|
[Spark] UserDefinedFunction (udf) 구현하는 방법 (+예제코드) (0) | 2020.02.27 |
[Spark] 시작할때 살펴보면 좋은 유용한 사이트 모음 (0) | 2020.02.27 |
[Spark] Scala Test (Library, Sbt Test) (0) | 2020.02.26 |
[Spark] SparkSessionWrapper를 구현하자 (0) | 2020.02.26 |