본문 바로가기
데엔(Data-Engineering)/스파크(Spark)

[Spark] 04. 스파크 기초(Transformations & Actions / Cache & Persist)

by KwonSoonBin 2023. 2. 12.

Spark Operation은 Transformations & Actions 두 가지로 나뉠 수 있다.

 

메모리를 최대한 활용할 수 있다.
(디스크, 네트워크 연산을 최소화 할 수 있다)

데이터를 다루는 task는 반복되는 경우가 많다
• 예) 머신러닝 학습

 

Transformations

지연 실행되기 때문에 메모리에 저장해둘 수 있다

  • 결과값으로 새로운 RDD를 반환
  • 지연 실행 - Lazy Execution

반복을 할때 생기는 비효율
in-memory : 지연되는 연산이 유용한 경우

관련함수

• map()
• flatMap()
• filter()
• distinct()
• reduceByKey()
• groupByKey()
• mapValues()
• flatMapValues()
• sortByKey()

• keys
• join (+ leftOuterJoin, rightOuterJoin)

 

 

Transformations은 Narrow , Wide 두 가지로 나뉜다.

 

Narrow Transformation
• 1:1 변환
• filter(), map(), flatMap(), sample(), union()
• 1열을 조작하기 위해 다른 열/파티션의 데이터를 쓸 필요가 없다
• 정렬이 필요하지 않은 경우

 

Wide Transformation
• Shuffling
• Intersection and join, distinct, cartesian, reduceByKey(), groupByKey()
• 아웃풋 RDD의 파티션에 다른 파티션의 데이터가 들어갈 수 있음

 

 

Actions

결과값을 연산하여 출력하거나 저장

• 즉시 실행 - Eager Execution

 

관련함수

• collect()
• count()
• countByValue()
• take()
• top()
• reduce()
• fold()
• foreach()

• countByKey

 


Reduction

  • 근접하는 요소들을 모아서 하나의 결과로 만드는 일
  • 많은 Spark의 연산들이 reduction
  • 대부분의 Action은 Reduction
  • 파일 저장, collect()등과 같이 Reduction이 아닌 액션도 있다.

대표적인 Reduction Actions
• Reduce
• Fold
• GroupBy
• Aggregate


Cache() & Persist()

cache()와 persist()로 데이터를 메모리에 저장해두고 사용이 가능하다.

 

Cache
• 디폴트 Storage Level 사용
• RDD: MEMORY_ONLY
• DF: MEMORY_AND_DISK


Persist
• Storage Level을 사용자가 원하는대로 지정 가능

댓글