Spark - RDD 다루기
2024. 5. 13. 23:43ㆍSPARK
RDD란?
- RDD는 Spark의 기본적인 자료구조로 데이터처리에 사용한다.
- RDD를 사용해 새로운 RDD를 생성하고, 또 반복하여 원하는 결과를 얻을 수 있다.
데이터 가공 (Rdd로부터 다른 Rdd 생성): transformation
데이터 처리(가공없이 원하는 결과 조작) : action
<데이터 요소 transformation>
--> 각각의 원본 데이터를 넣었을때 어떠한 Rdd를 생성할 수 있는지 보여준다.
Rdd.Map
[1, 2, 3] | 각 요소에 2를 곱함 | [2, 4, 6] |
["hello", "world"] | 각 문자열에 "!"를 추가함 | ["hello!", "world!"] |
[(1, "a"), (2, "b")] | 각 튜플의 두 번째 요소를 대문자로 변환함 | [("a", "A"), ("b", "B")] |
Rdd.FlatMap
["hello world", "hi there"] | 각 문자열을 공백으로 분리하여 단어로 변환함 | ["hello", "world", "hi", "there"] |
["a b c", "d e f"] | 각 문자열을 공백으로 분리하여 알파벳 하나씩 추출함 | ["a", "b", "c", "d", "e", "f"] |
Rdd.Distinct
[1, 2, 3, 1, 2] | 중복된 요소를 제거함 | [1, 2, 3] |
["apple", "banana", "apple", "cherry"] | 중복된 문자열을 제거함 | ["apple", "banana", "cherry"] |
Rdd.Filter
[1, 2, 3, 4, 5] | 3의 배수만 남기고 나머지는 제거함 | [3] |
["apple", "banana", "cherry"] | 문자열에 'a'가 포함된 것만 남기고 나머지는 제거함 | ["apple", "banana"] |
<Action> - 생성한 Rdd를 사용해 최종 결과 조작
count => RDD 요소 개수 리턴
[1, 2, 3, 4, 5] | count() | 5 |
["apple", "banana", "cherry"] | count() | 3 |
[(1, "a"), (2, "b"), (3, "c")] | count() | 3 |
reduce => RDD 요소 병합하여 리턴. 함수는 파라미터로 전달
[1, 2, 3, 4, 5] | reduce(lambda x, y: x + y) | 15 |
["apple", "banana", "cherry"] | reduce(lambda x, y: x + " " + y) | "apple banana cherry" |
[(1, "a"), (2, "b"), (3, "c")] | reduce(lambda x, y: (x[0] + y[0], x[1] + y[1])) | (6, "abc") |
countByValue => Rdd 이루고 있는 각 요소들의 개수 리턴 (예시- wordCount할때!)
[1, 2, 3, 1, 2, 1] | countByValue() | {1: 3, 2: 2, 3: 1} |
["apple", "banana", "apple", "cherry"] | countByValue() | {"apple": 2, "banana": 1, "cherry": 1} |
[(1, "a"), (2, "b"), (1, "a")] | countByValue() | {(1, "a"): 2, (2, "b"): 1} |
그 밖에 집합관련 transformation도 사용할 수 있다.
1. Rdd.union(Rdd2) => 합집합
2. Rdd.intersection(Rdd2) => 교집합
3. Rdd.subtract(Rdd2) => 차집합
'SPARK' 카테고리의 다른 글
Spark - WordCount 예시 (0) | 2024.05.14 |
---|