Which of the following code blocks prints out in how many rows the expression Inc. appears in the string-type column supplier of DataFrame itemsDf?
Correct Answer: E
Explanation Correct code block: accum=sc.accumulator(0) def check_if_inc_in_supplier(row): if 'Inc.' in row['supplier']: accum.add(1) itemsDf.foreach(check_if_inc_in_supplier) print(accum.value) To answer this question correctly, you need to know both about the DataFrame.foreach() method and accumulators. When Spark runs the code, it executes it on the executors. The executors do not have any information about variables outside of their scope. This is whhy simply using a Python variable counter, like in the two examples that start with counter = 0, will not work. You need to tell the executors explicitly that counter is a special shared variable, an Accumulator, which is managed by the driver and can be accessed by all executors for the purpose of adding to it. If you have used Pandas in the past, you might be familiar with the iterrows() command. Notice that there is no such command in PySpark. The two examples that start with print do not work, since DataFrame.foreach() does not have a return value. More info: pyspark.sql.DataFrame.foreach - PySpark 3.1.2 documentation Static notebook | Dynamic notebook: See test 3
Associate-Developer-Apache-Spark Exam Question 2
Which of the following is not a feature of Adaptive Query Execution?
Correct Answer: D
Explanation Reroute a query in case of an executor failure. Correct. Although this feature exists in Spark, it is not a feature of Adaptive Query Execution. The cluster manager keeps track of executors and will work together with the driver to launch an executor and assign the workload of the failed executor to it (see also link below). Replace a sort merge join with a broadcast join, where appropriate. No, this is a feature of Adaptive Query Execution. Coalesce partitions to accelerate data processing. Wrong, Adaptive Query Execution does this. Collect runtime statistics during query execution. Incorrect, Adaptive Query Execution (AQE) collects these statistics to adjust query plans. This feedback loop is an essential part of accelerating queries via AQE. Split skewed partitions into smaller partitions to avoid differences in partition processing time. No, this is indeed a feature of Adaptive Query Execution. Find more information in the Databricks blog post linked below. More info: Learning Spark, 2nd Edition, Chapter 12, On which way does RDD of spark finish fault-tolerance? - Stack Overflow, How to Speed up SQL Queries with Adaptive Query Execution
Associate-Developer-Apache-Spark Exam Question 3
The code block shown below should convert up to 5 rows in DataFrame transactionsDf that have the value 25 in column storeId into a Python list. Choose the answer that correctly fills the blanks in the code block to accomplish this. Code block: transactionsDf.__1__(__2__).__3__(__4__)
Correct Answer: D
Explanation The correct code block is: transactionsDf.filter(col("storeId")==25).take(5) Any of the options with collect will not work because collect does not take any arguments, and in both cases the argument 5 is given. The option with toLocalIterator will not work because the only argument to toLocalIterator is prefetchPartitions which is a boolean, so passing 5 here does not make sense. The option using head will not work because the expression passed to select is not proper syntax. It would work if the expression would be col("storeId")==25. Static notebook | Dynamic notebook: See test 1 (https://flrs.github.io/spark_practice_tests_code/#1/24.html , https://bit.ly/sparkpracticeexams_import_instructions)
Associate-Developer-Apache-Spark Exam Question 4
The code block displayed below contains an error. The code block is intended to return all columns of DataFrame transactionsDf except for columns predError, productId, and value. Find the error. Excerpt of DataFrame transactionsDf: transactionsDf.select(~col("predError"), ~col("productId"), ~col("value"))
Correct Answer: E
Explanation Correct code block: transactionsDf.drop("predError", "productId", "value") Static notebook | Dynamic notebook: See test 1
Associate-Developer-Apache-Spark Exam Question 5
Which of the following code blocks reads the parquet file stored at filePath into DataFrame itemsDf, using a valid schema for the sample of itemsDf shown below? Sample of itemsDf: 1.+------+-----------------------------+-------------------+ 2.|itemId|attributes |supplier | 3.+------+-----------------------------+-------------------+ 4.|1 |[blue, winter, cozy] |Sports Company Inc.| 5.|2 |[red, summer, fresh, cooling]|YetiX | 6.|3 |[green, summer, travel] |Sports Company Inc.| 7.+------+-----------------------------+-------------------+
Correct Answer: D
Explanation The challenge in this question comes from there being an array variable in the schema. In addition, you should know how to pass a schema to the DataFrameReader that is invoked by spark.read. The correct way to define an array of strings in a schema is through ArrayType(StringType()). A schema can be passed to the DataFrameReader by simply appending schema(structType) to the read() operator. Alternatively, you can also define a schema as a string. For example, for the schema of itemsDf, the following string would make sense: itemId integer, attributes array<string>, supplier string. A thing to keep in mind is that in schema definitions, you always need to instantiate the types, like so: StringType(). Just using StringType does not work in pySpark and will fail. Another concern with schemas is whether columns should be nullable, so allowed to have null values. In the case at hand, this is not a concern however, since the question just asks for a "valid" schema. Both non-nullable and nullable column schemas would be valid here, since no null value appears in the DataFrame sample. More info: Learning Spark, 2nd Edition, Chapter 3 Static notebook | Dynamic notebook: See test 3