DEV Community

Cover image for A Beginner’s Guide to Big Data Analytics with Apache Spark and PySpark
Aimé Bangirahe
Aimé Bangirahe

Posted on

A Beginner’s Guide to Big Data Analytics with Apache Spark and PySpark

What is Apache Spark?

Apache Spark
is a distributed processing system used to perform big data and machine learning tasks on large datasets. With Apache Spark, users can run queries and machine learning workflows on petabytes of data, which is impossible to do on your local device.

This framework is even faster than previous data processing engines like Hadoop, and has increased in popularity in the past eight years. Companies like IBM, Amazon, and Yahoo are using Apache Spark as their computational framework.

What is PySpark?
PySpark is an interface for Apache Spark in Python. With PySpark, you can write Python and SQL-like commands to manipulate and analyze data in a distributed processing environment. Using PySpark, data scientists manipulate data, build machine learning pipelines, and tune models.

Why Use PySpark?
The reason companies choose to use a framework like PySpark is because of how quickly it can process big data. It is faster than libraries like Pandas and Dask, and can handle larger amounts of data than these frameworks. If you had over petabytes of data to process, for instance, Pandas and Dask would fail but PySpark would be able to handle it easily.

Spark, a unified big data analytics tool with over 32k stars and 1800 contributors on Github, was created specifically for handling big data using clustered computing. It has built-in machine learning algorithms, SQL, and data streaming modules. It provides high-level APIs for R, Python, Java, and Scala. Additionally, Spark provides a wide range of high-level tools, such as Spark Streaming, MLlib for machine learning, GraphX for processing graph data sets, and Spark SQL for real-time processing of structured and unstructured data. Both stream and batch real-time processing are supported. Since Spark is an open-source platform with many built-in features, it can be applied to any industry that uses big data and data science.

PySpark Applications-How are Businesses leveraging PySpark?

Some of the industry giants like Yahoo, Netflix, etc., are leveraging various functionalities of Spark PySpark. Yahoo utilizes Apache Spark's Machine Learning capabilities to customize its news, web pages, and advertising. They use Pypark to determine what kind of news readers are interested in reading. Also, PySpark helps them categorize the news stories to determine who would be interested in reading each news category.

Netflix amazes its users with fantastic recommendations every time they watch they are using the platform. But how does this happen? Netflix uses the collaborative filtering feature offered by PySpark. Apart from that, Runtastic also uses Spark PySpark for Big Data sanity checks. Their team uses Python's unittest package to keep things simple and manageable and creates a task for each entity type (e.g., sports activities).

The PySpark Architecture

The PySpark architecture consists of various parts such as Spark Conf, RDDs, Spark Context, Dataframes, etc.

Spark Conf
SparkConf helps establish a few setups and parameters to execute a Spark application on the local cluster or a dataset. It contains the settings needed to launch a Spark application.

There are plenty of SparkConf features you can use, such as-

To set a configuration property, use set(key, value).
To set the master URL, use setMaster(value).
To name an application, use setAppName(value).
To access the configuration value, use get(key, defaultValue=None).
To set the Spark installation path on worker nodes, use setSparkHome(value).

Any Spark program begins by generating a SparkContext object, instructing the application to connect to a cluster. You must implement SparkConf to store the application's configuration data in the SparkContext instance to finish the job. Let's have a look at what SparkContext can do.

Spark Context
The Apache Spark functionality is accessed through the SparkContext portal. In any Spark operator program, the production of SparkContext is a significantly more crucial phase. The Resource Manager (YARN/Mesos) allows the Spark Application to interact with the Spark Cluster. SparkContext can only be generated once SparkConf is formed. The Spark driver program can transmit a configuration parameter to the SparkContext via SparkConf.

When you execute a Spark application, a driver program with the primary function runs, and your SparkContext is created here. The driver program then performs the operations inside the executors on worker nodes.

Resilient Distributed Databases - RDDs
The components that run and operate on numerous nodes to execute parallel processing on a cluster are RDDs (Resilient Distributed Datasets). RDDs are irrevocable entities, which means you can't change them after creation. RDDs are also fault-tolerant; thus, they will automatically recover in the event of a failure.

RDD is an acronym for-

Resilient- It is fault-tolerant and capable of regenerating data in the event of a failure.

Distributed- The data in a cluster is distributed among the various nodes.

Dataset- It refers to a collection of segregated data that contains values.

RDD uses a key to partition data into smaller chunks. The advantage of breaking data into manageable blocks is that if one executor node crashes, another node can still process the data. Because the same data blocks are duplicated over numerous executor nodes, these can quickly recover from any failures. RDD allows you to efficiently conduct functional calculations against a dataset by linking together multiple nodes.

To generate RDDs, PySpark offers two choices: loading an external dataset or distributing a set of objects. The most straightforward approach to building RDDs is the parallelize() function, which takes an existing program collection and passes it to the Spark Context.

There are mainly two types of operations you can perform with RDDs-

Transformations: These operations are used to generate a new RDD. Map, flatMap, filter, distinct, reduceByKey, mapPartitions, and sortByKey are some of the transformation operations used on RDDs.

Actions: These operations are used on an RDD to enable Apache Spark to perform computations and then provide the result to the driver. Collect, collectAsMap, reduce, countByKey/countByValue are some of the action operations used on RDDs.

PySpark SQL and Dataframes
A dataframe is a shared collection of organized or semi-structured data in PySpark. This collection of data is kept in Dataframe in rows with named columns, similar to relational database tables. It also has several characteristics in common with RDD, such as being immutable, distributed, and following lazy evaluations. It accepts various file types, including JSON, CSV, TXT, and others. You can load it from existing RDDs or by setting the schema dynamically.

Top comments (0)