为何Spark的parallelize操作未生成对应Job?代码对比验证
parallelize, But It Does for Word Count? Great question! This boils down to Spark's core execution model: lazy evaluation. Let me break this down clearly:
1. Spark's Lazy Evaluation Explained
Spark doesn’t run any computation the moment you define an RDD or apply transformation operations. Instead, it builds up a logical plan of what needs to be done behind the scenes. Only when you run an action operation (a command that requires returning results to the driver or writing data out) does Spark trigger a Job, execute the full plan, and log it in the Spark UI.
2. Analyzing Your Two Code Snippets
Your First Code (No Job in UI)
val data = List.range(2, 11000000) val distData = sparkSession.sparkContext.parallelize(data)
Here, parallelize is a transformation operation—it just creates an RDD definition (telling Spark "here's the data I want to distribute across the cluster") but doesn’t trigger any actual computation. Since no action is called, Spark never launches a Job, so nothing appears in the UI.
Your Second Code (Job Visible in UI)
Your word count code uses transformations like textFile, flatMap, map, and reduceByKey, but the reason a Job shows up is that you’re almost certainly triggering an action implicitly or explicitly:
- If you’re running this in an interactive environment like
spark-shellor a Databricks notebook, typingwordCountsand hitting enter will automatically trigger an action (likecollectorshow) to display results. - If you added an explicit action like
wordCounts.collect(),wordCounts.count(), orwordCounts.saveAsTextFile("output"), that’s what kicks off the actual computation and creates a Job visible in the UI.
3. Test It Yourself!
To confirm this behavior, add an action to your first code snippet:
val data = List.range(2, 11000000) val distData = sparkSession.sparkContext.parallelize(data) // Add an action to trigger computation distData.count()
Now you’ll see a corresponding Job appear in the Spark UI, just like your word count example.
内容的提问来源于stack exchange,提问作者More Than Five

