Skip to Content
スパーク定義ガイド
book

スパーク定義ガイド

by Bill Chambers, Matei Zaharia
March 2025
Intermediate to advanced
606 pages
9h
Japanese
O'Reilly Media, Inc.
Content preview from スパーク定義ガイド

第5章. 基本的な構造化演算子

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

第4章では、構造化APIの核となる抽象化について紹介した。この章では、アーキテクチャの概念から離れ、DataFrameとその中のデータを操作するために使用する戦術的なツールについて説明する。この章では、DataFrameの基本的な演算子のみに焦点を当て、集約、ウィンドウ関数、結合は避けている。これらについては後の章で説明する。

データフレームは、Row 型の一連のレコード(テーブルの行のようなもの)と、データセット内の個々のレコードに対して実行できる計算式を表すいくつかの(スプレッドシートの列のようなもの)から定義される。スキーマは、各列のデータの名前と型を定義する。DataFrameのパーティショニングは、クラスタ全体におけるDataFrameやデータセットの物理的な配置を定義する。 パーティショニング・スキームは、その割り当て方法を定義する。特定の列の値に基づいて設定することも、非決定的に設定することもできる。

作業用のDataFrameを作成しよう:

// in Scala
val df = spark.read.format("json")
  .load("/data/flight-data/json/2015-summary.json")
# in Python
df = spark.read.format("json").load("/data/flight-data/json/2015-summary.json")

DataFameにはカラムがあり、それらを定義するためにスキーマを使用することを説明した。現在のDataFrameのスキーマを見てみよう:

df.printSchema()

スキーマはすべてを結びつけるものだから、念入りに説明する価値がある。

スキーマ

スキーマはDataFrameのカラム名と型を定義する。スキーマはデータソースに定義させることもできるし(スキーマ・オンリードと呼ばれる)、自分で明示的なスキーマを定義することもできる。

警告

データを読み取る前にスキーマを定義する必要があるかどうかは、使用するケースによって異なる。アドホックな分析の場合、スキーマ・オン・リードは通常問題なく機能する(CSVやJSONのようなプレーンテキストのファイル形式では少し遅くなることもあるが)。しかし、ファイル読み取り時にlong 型が誤って整数としてセットされるなど、精度の問題につながることもある。ETL(Extract、Transform、Load)のためにSparkを使用する場合、スキーマを手動で定義するのが良いことが多い。特にCSVやJSONのような型付けのないデータソースを扱う場合、スキーマの推論は読み込むデータの種類によって異なる可能性があるからだ。

第4章で見た単純なファイルから始め、行切り文字JSONの半構造化された性質に構造を定義させてみよう。これは米国運輸統計局のフライトデータである:

// in Scala
spark.read.format("json").load("/data/flight-data/json/2015-summary.json").schema

Scalaは次のように返す:

org.apache.spark.sql.types.StructType = ... StructType(StructField(DEST_COUNTRY_NAME,StringType,true), ...
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

高性能Spark

高性能Spark

Holden Karau, Rachel Warren
生成AIの可視化

生成AIの可視化

Priyanka Vergadia, Valliappa Lakshmanan
ソフトウェア工学の基礎

ソフトウェア工学の基礎

Nathaniel Schutta, Dan Vega

Publisher Resources

ISBN: 9798341627567