Skip to Content
Spark를 사용한 데이터 알고리즘
book

Spark를 사용한 데이터 알고리즘

by Mahmoud Parsian
May 2025
Beginner to intermediate
438 pages
6h 57m
Korean
O'Reilly Media, Inc.
Content preview from Spark를 사용한 데이터 알고리즘

4장. Spark 감소

이 작품은 AI를 사용하여 번역되었습니다. 여러분의 피드백과 의견을 환영합니다: translation-feedback@oreilly.com

이 장에서는 Spark에서 RDD의 축소 변환에 중점을 둡니다. 특히, Spark의 많은 작업에 필요한 일반적인 데이터 추상화인 (키, 값) 쌍의 RDD로 작업할 것입니다. 데이터를 (키, 값) 형태로 가져오기 위해 일부 초기 ETL 작업이 필요할 수 있지만, 쌍 RDD를 사용하면 값 집합에 대해 원하는 모든 집계를 수행할 수 있습니다.

Spark는 몇 가지 강력한 축소 변환과 작업을 지원합니다. 가장 중요한 축소 변환은 다음과 같습니다:

  • reduceByKey()

  • combineByKey()

  • groupByKey()

  • aggregateByKey()

모든 *ByKey() 변환은 소스RDD[(K, V)] 를 받아 대상RDD[(K, C)] 을 생성합니다( reduceByKey(), V 및 C와 같은 일부 변환의 경우 동일). 이러한 변환의 기능은 예를 들어 주어진 키의 모든 값(모든 고유 키에 대해)을 찾아서 줄이는 것입니다:

  • 모든 값의 평균

  • 모든 값의 합계 및 개수

  • 모든 값의 모드와 중앙값

  • 모든 값의 표준 편차

감소 변환 선택

매퍼 변환과 마찬가지로, 작업에 적합한 도구를 선택하는 것이 중요합니다. 중앙값 찾기와 같은 일부 축소 작업의 경우, 축소기는 모든 값에 동시에 액세스할 수 있어야 합니다. 하지만 모든 값의 합계나 개수를 찾는 것과 같은 다른 작업의 경우에는 그렇지 않습니다. 키별 값의 중앙값을 찾으려면 groupByKey()을 사용하는 것이 좋지만, 키에 값이 많은 경우 이 변환은 잘 작동하지 않습니다(OOM 문제가 발생할 수 있음). 반면에 모든 값의 합계 또는 개수를 찾으려면 reduceByKey() 이 좋은 선택이 될 수 있습니다. 이 함수는 연관 및 정류 감소 함수를 사용하여 각 키의 값을 병합합니다.

이 장에서는 간단한 PySpark 예제를 통해 가장 중요한 Spark 감소 변환을 사용하는 방법을 보여드리겠습니다. Spark 애플리케이션에서 가장 일반적으로 사용되는 변환에 초점을 맞출 것입니다. 또한 환원의 일반적인 개념과 효율적인 환원 알고리즘을 위한 설계 원리인 모노이드에 대해서도 설명합니다. 먼저 Spark의 환원 변환에 필요한 쌍 RDD를 만드는 방법부터 살펴보겠습니다.

쌍 RDD 생성

키 집합과 관련 값이 주어지면 환원 변환은 알고리즘(값의 합, 값의 중앙값 등)을 사용하여 각 키의 값을 줄입니다. 따라서 이 장에서 소개하는 환원 변환은 (키, 값) 쌍에 대해 작동하므로 RDD 요소는 이 형식을 준수해야 합니다. Spark에서 쌍 RDD를 만드는 방법에는 여러 가지가 있습니다. 예를 들어, 여기에 표시된 것처럼 컬렉션(예: 튜플 및 사전 목록)에parallelize() 을 사용할 수도 있습니다:

>>> key_value = [(
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

데이터 엔지니어를 위한 97가지 조언

데이터 엔지니어를 위한 97가지 조언

Tobias Macey, 임혜연(Lim Hye Yeon)
실무자를 위한 그래프 데이터 활용법

실무자를 위한 그래프 데이터 활용법

마티아스 브뢰헬러, 데니즈 고즈넬

Publisher Resources

ISBN: 9798341655317