Interview Questions For Apache Spark and Scala
by Bhavya Sri, on May 23, 2017 5:40:09 PM
Q1. What are the various levels of persistence in Apache Spark?
Ans: Apache Spark automatically persists the intermediary data from various shuffle operations, however it is often suggested that users call persist () method on the RDD in case they plan to reuse it. Spark has various persistence levels to store the RDDs on disk or in memory or as a combination of both with different replication levels.
Q2. What is Shark?
Ans: Most of the data users know only SQL and are not good at programming. Shark is a tool, developed for people who are from a database background - to access Scala MLib capabilities through Hive like SQL interface. Shark tool helps data users run Hive on Spark - offering compatibility with Hive metastore, queries and data.
Q3. List some use cases where Spark outperforms Hadoop in processing.
- Sensor Data Processing –Apache Spark’s ‘In-memory computing’ works best here, as data is retrieved and combined from different sources.
- Spark is preferred over Hadoop for real time querying of data
- Stream Processing – For processing logs and detecting frauds in live streams for alerts, Apache Spark is the best solution.
Q4. What is a Sparse Vector?
Ans: A sparse vector has two parallel arrays –one for indices and the other for values. These vectors are used for storing non-zero entries to save space.
Q5. What is RDD?
Ans: RDDs (Resilient Distributed Datasets) are basic abstraction in Apache Spark that represent the data coming into the system in object format. RDDs are used for in-memory computations on large clusters, in a fault tolerant manner. RDDs are read-only portioned, collection of records, that are:
- Immutable – RDDs cannot be altered.
- Resilient – If a node holding the partition fails the other node takes the data.
Q6. Explain about transformations and actions in the context of RDDs.
Ans: Transformations are functions executed on demand, to produce a new RDD. All transformations are followed by actions. Some examples of transformations include map, filter and reduceByKey.
Actions are the results of RDD computations or transformations. After an action is performed, the data from RDD moves back to the local machine. Some examples of actions include reduce, collect, first, and take.
Q7. What are the languages supported by Apache Spark for developing big data applications?
Ans: Scala, Java, Python, R and Clojure
Q8. Can you use Spark to access and analyse data stored in Cassandra databases?
Ans: Yes, it is possible if you use Spark Cassandra Connector.
Q9. Is it possible to run Apache Spark on Apache Mesos?
Ans: Yes, Apache Spark can be run on the hardware clusters managed by Mesos.
Q10. Explain about the different cluster managers in Apache Spark
Ans: The 3 different clusters managers supported in Apache Spark are:
- Apache Mesos: Has rich resource scheduling capabilities and is well suited to run Spark along with other applications. It is advantageous when several users run interactive shells because it scales down the CPU allocation between commands.
- Standalone deployments: Well suited for new deployments which only run and are easy to set up.
Q11. How can Spark be connected to Apache Mesos?
Ans: To connect Spark with Mesos:
- Configure the spark driver program to connect to Mesos. Spark binary package should be in a location accessible by Mesos. (or)
- Install Apache Spark in the same location as that of Apache Mesos and configure the property ‘spark.mesos.executor.home’ to point to the location where it is installed.
Q12. How can you minimize data transfers when working with Spark?
Ans: Minimizing data transfers and avoiding shuffling helps write spark programs that run in a fast and reliable manner. The various ways in which data transfers can be minimized when working with Apache Spark are:
- Using Broadcast Variable- Broadcast variable enhances the efficiency of joins between small and large RDDs.
- Using Accumulators – Accumulators help update the values of variables in parallel while executing.
- The most common way is to avoid operations ByKey, repartition or any other operations which trigger shuffles.
Q13. Why is there a need for broadcast variables when working with Apache Spark?
Ans: These are read only variables, present in-memory cache on every machine. When working with Spark, usage of broadcast variables eliminates the necessity to ship copies of a variable for every task, so data can be processed faster. Broadcast variables help in storing a lookup table inside the memory which enhances the retrieval efficiency when compared to an RDD lookup ().
Q14. Is it possible to run Spark and Mesos along with Hadoop?
Ans: Yes, it is possible to run Spark and Mesos with Hadoop by launching each of these as a separate service on the machines. Mesos acts as a unified scheduler that assigns tasks to either Spark or Hadoop.
Q15. What is lineage graph?
Ans: The RDDs in Spark, depend on one or more other RDDs. The representation of dependencies in between RDDs is known as the lineage graph. Lineage graph information is used to compute each RDD on demand, so that whenever a part of persistent RDD is lost, the data that is lost can be recovered using the lineage graph information.
Q16. How can you trigger automatic clean-ups in Spark to handle accumulated metadata?
Ans: You can trigger the clean-ups by setting the parameter ‘spark.cleaner.ttl’ or by dividing the long running jobs into different batches and writing the intermediary results to the disk.
Q17. Explain about the major libraries that constitute the Spark Ecosystem.
- Spark MLib- Machine learning library in Spark for commonly used learning algorithms like clustering, regression, classification, etc.
- Spark Streaming – This library is used to process real time streaming data.
- Spark GraphX – Spark API for graph parallel computations with basic operators like joinVertices, subgraph, aggregateMessages, etc.
- Spark SQL – Helps execute SQL like queries on Spark data using standard visualization or BI tools.
Q18. What are the benefits of using Spark with Apache Mesos?
Ans: It renders scalable partitioning among various Spark instances and dynamic partitioning between Spark and other big data frameworks.
Q19. What is the significance of Sliding Window operation?
Ans: Sliding Window controls transmission of data packets between various computer networks. Spark Streaming library provides windowed computations where the transformations on RDDs are applied over a sliding window of data. Whenever the window slides, the RDDs that fall within the particular window are combined and operated upon to produce new RDDs of the windowed DStream.
Q20. What is a DStream?
Ans: Discretized Stream is a sequence of Resilient Distributed Databases that represent a stream of data. DStreams can be created from various sources like Apache Kafka, HDFS, and Apache Flume. DStreams have two operations:
- Transformations that produce a new DStream.
- Output operations that write data to an external system.
Q21. When running Spark applications, is it necessary to install Spark on all the nodes of YARN cluster?
Ans: Spark need not be installed when running a job under YARN or Mesos because Spark can execute on top of YARN or Mesos clusters without affecting any change to the cluster.
Q22. What is Catalyst framework?
Ans: Catalyst framework is a new optimization framework present in Spark SQL. It allows Spark to automatically transform SQL queries by adding new optimizations to build a faster processing system.
Q23. Name a few companies that use Apache Spark in production.
Ans: Pinterest, Conviva, Shopify, Open Table.
Q24. Which spark library allows reliable file sharing at memory speed across different cluster frameworks?
Q25. Why is BlinkDB used?
Ans: BlinkDB is a query engine for executing interactive SQL queries on huge volumes of data and renders query results marked with meaningful error bars. BlinkDB helps users balance ‘query accuracy’ with response time.
Q26. How can you compare Hadoop and Spark in terms of ease of use?
Ans: Hadoop MapReduce requires programming in Java which is difficult, though Pig and Hive make it considerably easier. Learning Pig and Hive syntax takes time. Spark has interactive APIs for different languages like Java, Python or Scala and also includes Shark i.e. Spark SQL for SQL lovers - making it comparatively easier to use than Hadoop.
Q27. What are the common mistakes developers make when running Spark applications?
Ans: Developers often make the mistake of:
- Hitting the web service several times by using multiple clusters.
- Run everything on the local node instead of distributing it.
Developers need to be careful with this, as Spark makes use of memory for processing.
28. What is the advantage of a Parquet file?
Ans: Parquet file is a columnar format file that helps:
- Limit I/O operations
- Consumes less space
- Fetches only required columns.
Q29. What are the various data sources available in SparkSQL?
- Parquet file
- JSON Datasets
- Hive tables
Q30. How Spark uses Hadoop?
Ans: Spark has its own cluster management computation and mainly uses Hadoop for storage.
Q31. What are the key features of Apache Spark that you like?
- Spark provides advanced analytic options like graph algorithms, machine learning, streaming data, etc
- It has built-in APIs in multiple languages like Java, Scala, Python and R
- It has good performance gains, as it helps run an application in the Hadoop cluster ten times faster on disk and 100 times faster in memory.
Q32. What do you understand by Pair RDD?
Ans: Special operations can be performed on RDDs in Spark using key/value pairs and such RDDs are referred to as Pair RDDs. Pair RDDs allow users to access each key in parallel. They have a reduceByKey () method that collects data based on each key and a join () method that combines different RDDs together, based on the elements having the same key.
Q33. Which one will you choose for a project –Hadoop MapReduce or Apache Spark?
Ans: The answer to this question depends on the given project scenario - as it is known that Spark makes use of memory instead of network and disk I/O. However, Spark uses large amount of RAM and requires dedicated machine to produce effective results. So the decision to use Hadoop or Spark varies dynamically with the requirements of the project and budget of the organization.
Q34. Explain about the different types of transformations on DStreams?
- Stateless Transformations- Processing of the batch does not depend on the output of the previous batch. Examples – map (), reduceByKey (), filter ().
- Stateful Transformations- Processing of the batch depends on the intermediary results of the previous batch. Examples –Transformations that depend on sliding windows.
Q35. Explain about the popular use cases of Apache Spark
Ans: Apache Spark is mainly used for:
- Iterative machine learning.
- Interactive data analytics and processing.
- Stream processing.
- Sensor data processing.
Q36. Is Apache Spark a good fit for Reinforcement learning?
Ans: No. Apache Spark works well only for simple machine learning algorithms like clustering, regression, classification.
Q37. What is Spark Core?
Ans: It has all the basic functionalities of Spark, like - memory management, fault recovery, interacting with storage systems, scheduling tasks, etc.
Q38. How can you remove the elements with a key present in any other RDD?
Ans: Use the subtractByKey () function.
Q39. What is the difference between persist() and cache()
Ans: persist () allows the user to specify the storage level whereas cache () uses the default storage level.
Q40. Explain what is Scala?
Ans: Scala is an object functional programming and scripting language for general software applications designed to express solutions in a concise manner.
Q41. What is a ‘Scala set’? What are methods through which operation sets are expressed?
Ans: Scala set is a collection of pairwise elements of the same type. Scala set does not contain any duplicate elements. There are two kinds of sets, mutable and immutable.
Q42. What is a ‘Scala map’?
Ans: Scala map is a collection of key or value pairs. Based on its key any value can be retrieved. Values are not unique but keys are unique in the Map.
Q43. What is the advantage of Scala?
- Less error prone functional style
- High maintainability and productivity
- High scalability
- High testability
- Provides features of concurrent programming
Q44. In what ways Scala is better than other programming language?
- The arrays uses regular generics, while in other language, generics are bolted on as an afterthought and are completely separate but have overlapping behaviours with arrays.
- Scala has immutable “val” as a first class language feature. The “val” of scala is similar to Java final variables. Contents may mutate but top reference is immutable.
- Scala lets ‘if blocks’, ‘for-yield loops’, and ‘code’ in braces to return a value. It is more preferable, and eliminates the need for a separate ternary operator.
- Singleton has singleton objects rather than C++/Java/ C# classic static. It is a cleaner solution.
- Persistent immutable collections are the default and built into the standard library.
- It has native tuples and a concise code.
- It has no boiler plate code.
Q45. What are the Scala variables?
Ans: Values and variables are two shapes that come in Scala. A value variable is constant and cannot be changed once assigned. It is immutable, while a regular variable, on the other hand, is mutable, and you can change the value.
The two types of variables are:
- var myVar : Int=0;
- val myVal: Int=1;
Q46. Mention the difference between an object and a class ?
Ans: A class is a definition for a description. It defines a type in terms of methods and composition of other types. A class is a blueprint of the object. While, an object is a singleton, an instance of a class which is unique. An anonymous class is created for every object in the code, it inherits from whatever classes you declared object to implement.
Q47. What is recursion tail in scala?
Ans: ‘Recursion’ is a function that calls itself. A function that calls itself, for example, a function ‘A’ calls function ‘B’, which calls the function ‘C’. It is a technique used frequently in functional programming. In order for a tail recursive, the call back to the function must be the last function to be performed.
Q48. What is ‘scala trait’ in scala?
Ans: ‘Traits’ are used to define object types specified by the signature of the supported methods. Scala allows to be partially implemented but traits may not have constructor parameters. A trait consists of method and field definition, by mixing them into classes it can be reused.
Q49. When can you use traits?
Ans: There is no specific rule when you can use traits, but there is a guideline which you can consider.
- If the behaviour will not be reused, then make it a concrete class. Anyhow it is not a reusable behaviour.
- In order to inherit from it in Java code, an abstract class can be used.
- If efficiency is a priority then lean towards using a class.
- Make it a trait if it might be reused in multiple and unrelated classes. In different parts of the class hierarchy only traits can be mixed into different parts.
- You can use abstract class, if you want to distribute it in compiled form and expects outside groups to write classes inheriting from it.
Q50. What is Case Classes?
Ans: Case classes provides a recursive decomposition mechanism via pattern matching, it is a regular classes which export their constructor parameter. The constructor parameters of case classes can be accessed directly and are treated as public values.
Q51. What is the use of tuples in scala?
Ans: Scala tuples combine a fixed number of items together so that they can be passed around as whole. A tuple is immutable and can hold objects with different types, unlike an array or list.
Q52. What is function currying in Scala?
Ans: Currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument Many of the same techniques as language like Haskell and LISP are supported by Scala. Function currying is one of the least used and misunderstood one.
Q53. What are implicit parameters in Scala?
Ans: Implicit parameter is the way that allows parameters of a method to be “found”. It is similar to default parameters, but it has a different mechanism for finding the “default” value. The implicit parameter is a parameter to method or constructor that is marked as implicit. This means if a parameter value is not mentioned then the compiler will search for an “implicit” value defined within a scope.
Q54. What is a closure in Scala?
Ans: A closure is a function whose return value depends on the value of the variables declared outside the function.
Q55. What is Monad in Scala?
Ans: A monad is an object that wraps another object. You pass the Monad mini-programs, i.e functions, to perform the data manipulation of the underlying object, instead of manipulating the object directly. Monad chooses how to apply the program to the underlying object.
Q56. What is Scala anonymous function?
Ans: In a source code, anonymous functions are called ‘function literals’ and at run time, function literals are instantiated into objects called function values. Scala provides a relatively easy syntax for defining anonymous functions.
Q57. Explain ‘Scala higher order’ functions?
Ans: Scala allows the definition of higher order functions. These are functions that take other functions as parameters, or whose result is a function. In the following example, apply () function takes another function ‘f’ and a value ‘v’ and applies function to v.
Related Interview Questions...
- Big Data and Hadoop Interview Questions and Answers
- Hadoop Administration Interview Questions and Answers
- Hadoop Interview Questions and Answers For Experienced
- Apache Solr interview questions
- Apache Spark Interview Questions and Answers
- Apache Kafka Interview Questions and Answers
- Interview Questions For Apache Storm