Exams / DBX-ADAS / D1 Developing Apache Spark™ DataFrame/DataSet API Applications
D1.1
Developing Apache Spark™ DataFrame/DataSet API Applications (part 1 of 6)
- Exam domain: Developing Apache Spark™ DataFrame/DataSet API Applications
You are reading a free preview. The rest of the DBX-ADAS course unlocks with an Exam pass or All access.See plans
Reading and writing data
| Task | Pattern |
|---|---|
| Write JSON | df.write.json(path) (write is a property: no parentheses) |
| Write Parquet, partitioned | df.write.mode("overwrite").partitionBy("country").parquet(path) |
| Managed table | df.write.saveAsTable("catalog.schema.table") |
| Read with a known schema | spark.read.schema(structType).json(path) (skips inference) |
| Nested folders | option("recursiveFileLookup", "true") |
Save modes: "By default if data exists at the specified path the write operation fails." overwrite replaces data, append adds, ignore silently skips, and the default (error/errorifexists) fails.
Columns and rows
| Need | Code |
|---|---|
| New or replaced column | withColumn("c", expr): the value must be a Column |
| Constant | lit("PHYSICAL") |
| Cast | col("storeId").cast(StringType()) |
| Drop columns | df.drop("a", "b") |
| Fill nulls | df.na.fill(30000, Seq("sqft")) (Scala) |
| Drop null rows | df.na.drop(how="all") drops only all-null rows |
| Deduplicate | dropDuplicates() (all columns) or distinct() |
| Union | union by position, unionByName by name |
| Sort | sort/orderBy, ascending by default |
| Aggregate | groupBy(...) returns GroupedData, then agg |
| First row value | df.first().sqft |
| Conditional count | agg(count_if(cond)) (3.5.0) |
Joins
how defaults to inner; pass "outer" explicitly. on can be a name, a list of names, or boolean expressions (a list of bare col() is invalid).
Partitions and broadcast
UDFs
"The default return type is StringType." Declare T.DoubleType() / IntegerType() to match what the function returns.
Exam traps
df.write()and.paquetare typos or invalid syntax.describetakes names, notcol().coalescecan only reduce partitions and does not shuffle.- Create one SparkSession per application.