Skip to Content
Sparkによるデータアルゴリズム
book

Sparkによるデータアルゴリズム

by Mahmoud Parsian
March 2025
Intermediate to advanced
438 pages
6h 47m
Japanese
O'Reilly Media, Inc.
Content preview from Sparkによるデータアルゴリズム

第11章. デザインパターンに参加する

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この章では、データセットを結合するための実用的なデザインパターンを検討する。前の章と同様、実際の環境で役立つパターンに焦点を当てる。PySparkはRDD(pyspark.RDD.join())とDataFrame(pyspark.sql.DataFrame.join())の基本的な結合操作をサポートしており、ほとんどのユースケースではこれで十分である。しかし、このjoinがコスト高になる場合もあるので、役に立つと思われる特殊化joinアルゴリズムも紹介する。

この章では、2つのデータセットを結合する基本概念を紹介し、いくつかの便利で実用的な結合デザインパターンの例を示す。結合操作がMapReduceパラダイムでどのように実装されているか、そして結合を実行するためにSparkの演算子をどのように使うかを紹介する。RDDとDataFrameを使ったマップサイド結合の実行方法と、ブルームフィルターを使った効率的な結合の実行方法を紹介する。

結合操作の紹介

リレーショナルデータベースの世界では、2つのテーブル(別名 "リレーション")を共通のキー、つまりテーブル内の各レコード(タプルまたは行)を一意に識別できる1つまたは複数の列の属性または属性セットで結合する操作は頻繁に行われる。

次の2つの表、T1T2 を考えてみよう:

T1 = {(k1, v1)}
T2 = {(k2, v2)}

どこだ?

  • k1はT1のキー、v1は関連する属性である。

  • k2はT2のキー、v2は関連する属性である。

単純な内部結合は、2つ以上のテーブルのキーが一致する行を結合して新しいテーブルを作成するもので、次のように定義できる:

T1.join(T2) = {(k, (v1, v2))}
T2.join(T1) = {(k, (v2, v1))}

どこだ?

  • k = k1 = k2である。

  • (k, v1)はT1にある。

  • (k, v2)はT2にある。

この仕組みを説明するために、2つのテーブルを作成し、サンプル・データを入力して、それらを結合してみよう。まず、T1T2 というテーブルを作成する:

>>> d1 = [('a', 10), ('a', 11), ('a', 12), ('b', 100), ('b', 200), ('c', 80)]
>>> T1 = spark.createDataFrame(d1, ['id', 'v1'])
>>> T1.show()
+---+---+
| id| v1|
+---+---+
|  a| 10|
|  a| 11|
|  a| 12|
|  b|100|
|  b|200|
|  c| 80|
+---+---+

>>> d2 = [('a', 40), ('a', 50), ('b', 300), ('b', 400), ('d', 90)]
>>> T2 = spark.createDataFrame(d2, ['id', 'v2'])
>>> T2.show()
+---+---+
| id| v2|
+---+---+
|  a| 40|
|  a| 50|
|  b|300|
|  b|400|
|  d| 90|
+---+---+ ...
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

メダリオンアーキテクチャの構築

メダリオンアーキテクチャの構築

Piethein Strengholt

Publisher Resources

ISBN: 9798341635302