変換とアクション
遅延計算です。
「変換とアクション」はCoddyKit上の無料Scala for Backend Engineering & Functional Programmingレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはScala for Backend Engineering & Functional Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Kinds of Operations
Spark operations split into transformations (build a new dataset, lazy) and actions (trigger computation and return a result or write output).
Lazy Evaluation
Transformations are lazy: they record what to do but run nothing. Spark builds a directed acyclic graph (DAG) of transformations and only executes when an action is called.
map and filter
map transforms each element; filter keeps elements matching a predicate. Both return new RDDs/Datasets and do not run yet.
val nums = sc.parallelize(1 to 10)
val evens = nums.filter(_ % 2 == 0).map(_ * 10)
// nothing computed yetflatMap
flatMap maps each element to zero or more outputs and flattens them — classic for splitting lines into words.
val lines = sc.parallelize(Seq("hello world", "spark rocks"))
val words = lines.flatMap(_.split(" "))Common Actions
Actions trigger execution:
collect— bring all results to the drivercount— number of elementsfirst/take(n)— sample rowsreduce— fold into one value
val total = sc.parallelize(1 to 100).reduce(_ + _)
println(total) // 5050 — runs nowNarrow vs Wide
Narrow transformations (map, filter) need no data movement. Wide transformations (groupByKey, reduceByKey, join) trigger a shuffle across the network.
reduceByKey
On key-value RDDs, reduceByKey aggregates values per key. It combines locally before shuffling, making it more efficient than groupByKey.
val pairs = sc.parallelize(Seq(("a", 1), ("b", 1), ("a", 1)))
val counts = pairs.reduceByKey(_ + _)
// (a, 2), (b, 1)Caching
If a dataset is reused across multiple actions, cache or persist keeps it in memory so it is not recomputed each time.
val cached = sc.parallelize(1 to 1000).filter(_ % 3 == 0).cache()
println(cached.count())
println(cached.sum())DataFrame Transformations
DataFrames have their own lazy transformations: select, where, withColumn, orderBy. Actions like show and collect trigger them.
import org.apache.spark.sql.functions._
val adults = df.where(col("age") >= 18)
.withColumn("adult", lit(true))
adults.show()The Word Count Classic
The canonical Spark job: split lines, map each word to one, reduce by key. Only the final collect runs the pipeline.
val text = sc.textFile("book.txt")
val counts = text.flatMap(_.split(" "))
.map(w => (w, 1))
.reduceByKey(_ + _)
counts.collect().foreach(println)Plain Scala Lazy Analog
A self-contained Scala analog: a lazy view defers work until forced, mirroring Spark's transformation/action split.
object Main {
def main(args: Array[String]): Unit = {
val pipeline = (1 to 10).view.filter(_ % 2 == 0).map(_ * 10) // lazy
val result = pipeline.toList // forces evaluation (action)
println(result)
}
}Quick Check
When does Spark actually execute a chain of map and filter transformations?
Recap
You learned Spark's execution model:
- Transformations are lazy (
map,filter,flatMap,reduceByKey) - Actions trigger work (
collect,count,reduce) - narrow vs wide (shuffle) operations
cacheto reuse results
Next: Spark SQL.
よくある質問
「変換とアクション」レッスンは無料ですか?
はい。「変換とアクション」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Scala for Backend Engineering & Functional Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
「変換とアクション」で何を学びますか?
遅延計算です。 ブラウザで直接実行するハンズオンコードでScala for Backend Engineering & Functional Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Scala for Backend Engineering & Functional Programmingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのScala for Backend Engineering & Functional Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「変換とアクション」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このScala for Backend Engineering & Functional Programmingレッスンでコードを書いて実行できますか?
はい。すべてのScala for Backend Engineering & Functional Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- RDD と DataFrame
- 変換とアクション
- Spark SQL
- 集約