dp-203 Practice Question 1947

Exam: dp-203
Domain: Design and develop data processing
Topic: Develop a batch processing solution
Difficulty: medium
You are tasked with developing a batch processing solution using Azure Databricks to transform data from a CSV file stored in Azure Blob Storage. The CSV file contains customer purchase records with columns: CustomerID, ProductID, Quantity, and PurchaseDate. You need to calculate the total quantity purchased by each customer and write the results to another CSV file in Azure Blob Storage. Which of the following PySpark code snippets correctly performs this transformation and writes the output to a new CSV file in Azure Blob Storage?

Answer Options

A
```df.groupBy('CustomerID').count().write.csv('output.csv')```
B
```df.groupBy('CustomerID').sum('Quantity').write.csv('output.csv')```
C
```df.groupBy('CustomerID').agg({'Quantity': 'sum'}).write.csv('output.csv')```
D
```df.write.csv('output.csv', mode='overwrite')```

Correct Answer

B: ```df.groupBy('CustomerID').sum('Quantity').write.csv('output.csv')```

Explanation

The correct answer is B because it uses the `groupby` function to aggregate the data by `CustomerID`, sums the `Quantity` column, and then writes the result to a new CSV file. Option A is incorrect as it does not group by `CustomerID`. Option C is incorrect as it does not sum the `Quantity` column. Option D is incorrect as it does not use the `write.csv` method to write the output to a new CSV file.

Related Practice Questions