Consider the following Spark code that reads a CSV, drops duplicates, and writes to a parquet file. What will be the count of rows in the output parquet if the input CSV has 100 rows with 10 duplicates?
from pyspark.sql import SparkSession spark = SparkSession.builder.appName('Test').getOrCreate() df = spark.read.csv('input.csv', header=True, inferSchema=True) df_clean = df.dropDuplicates() df_clean.write.mode('overwrite').parquet('output.parquet') output_df = spark.read.parquet('output.parquet') print(output_df.count())
Think about what dropDuplicates() does to the data.
The dropDuplicates() removes duplicate rows, so from 100 rows with 10 duplicates, 90 unique rows remain. The output parquet file will have 90 rows.
Given a Spark DataFrame df with a column value containing integers, the following code applies a transformation twice. What is the content of df_transformed?
df_transformed = df.withColumn('value', (df['value'] * 2)).withColumn('value', (df_transformed['value'] * 2)) df_transformed.show()
Each withColumn applies the transformation on the current DataFrame.
Applying the doubling transformation twice multiplies the values by 4 (2 * 2). So values are quadrupled.
This Spark pipeline step is intended to be idempotent by writing data only if the output path does not exist. What error will occur when running this code twice?
import os output_path = '/data/output' if not os.path.exists(output_path): df.write.parquet(output_path) else: print('Output exists, skipping write')
Check what happens if the output path exists before writing.
The code checks if the output path exists before writing. If it exists, it skips writing. So no error occurs and the pipeline is idempotent.
You want to write streaming data to a sink without duplicates on restart. Which approach below ensures idempotent writes?
Think about how to avoid duplicates on batch retries.
Using foreachBatch with idempotent batch writes and checkpointing ensures that each batch is processed exactly once, avoiding duplicates on restart.
Which statement best explains the importance of idempotency in data pipelines?
Think about what happens if a pipeline runs twice with the same input.
Idempotency means running the pipeline multiple times does not change the final data beyond the first run, preventing duplicates or corruption.
