SPARK PROCESSING:
Then big data phenomenon arrives. Because the data volume is huge, it cannot be hold by a single database server. Big data is typically partitioned and stored across many physical DB server machines. On the other hand, application servers need to be added to increase the processing power of big data.
However, as we increase the number of App servers and DB servers for storing and processing the big data, more data need to be transfer back and forth across the network during the processing cycle, up to a point where the network becomes a major bottleneck.
Notice the change of the program structure
To make a clear distinction between data reference and data itself, a Spark program is organized as a sequence of execution steps, which can either be a "transformation" or an "action".
When the driver program starts its execution, it builds up a graph where nodes are RDD and edges are transformation steps. However, no execution is happening at the cluster until an action is encountered. At that point, the driver program will ship the execution graph as well as the code block to the cluster, where every worker server will get a copy.
The execution graph is a DAG.
In the traditional 3-tier architecture, data
processing is performed by the application server where the data itself is
stored in the database server. Application server and database server are
typically two different machine. Therefore, the processing cycle proceeds
as follows
1.
Application server send a query to the database server to
retrieve the necessary data
2.
Application server perform processing on the received data
3.
Application server will save the changed data to the database
server
In the traditional data processing paradigm, we
move data to the code.
It can be depicted as follows ...
It can be depicted as follows ...
Then big data phenomenon arrives. Because the data volume is huge, it cannot be hold by a single database server. Big data is typically partitioned and stored across many physical DB server machines. On the other hand, application servers need to be added to increase the processing power of big data.
However, as we increase the number of App servers and DB servers for storing and processing the big data, more data need to be transfer back and forth across the network during the processing cycle, up to a point where the network becomes a major bottleneck.
Moving code to data
To overcome the network bottleneck, we need a
new computing paradigm. Instead of moving data to the code, we move the
code to the data and perform the processing at where the data is stored.
Notice the change of the program structure
·
The program execution starts at a driver, which orchestrate the
execution happening remotely across many worker servers within a cluster.
·
Data is no longer transferred to the driver program, the driver
program holds a data reference in its variable rather than the data
itself. The data reference is basically an id to locate the corresponding
data residing in the database server
·
Code is shipped from the program to the database server, where
the execution is happening, and data is modified at the database server without
leaving the server machine.
·
Finally the program request a save of the modified data.
Since the modified data resides in the database server, no data transfer
happens over the network.
By moving the code to the data, the volume of
data transfer over network is significantly reduced. This is an important
paradigm shift for big data processing.
In the following session, I will use Apache Spark to illustrate how this big data processing paradigm is implemented.
In the following session, I will use Apache Spark to illustrate how this big data processing paradigm is implemented.
RDD
Resilient Distributed Dataset (RDD) is how
Spark implements the data reference concept. RDD is a logical reference
of a dataset which is partitioned across many server machines in the cluster.
To make a clear distinction between data reference and data itself, a Spark program is organized as a sequence of execution steps, which can either be a "transformation" or an "action".
Programming Model
A typical program is organized as follows
1.
From an environment variable "context", create some
initial data reference RDD objects
2.
Transform initial RDD objects to create more RDD objects.
Transformation is expressed in terms of functional programming where a code
block is shipped from the driver program to multiple remote worker server,
which hold a partition of the RDD. Variable appears inside the code block
can either be an item of the RDD or a local variable inside the driver program
which get serialized over to the worker machine. After the code (and the
copy of the serialized variables) is received by the remote worker server, it
will be executed there by feeding the items of RDD residing in that
partition. Notice that the result of a transformation is a brand new RDD
(the original RDD is not mutated)
3.
Finally, the RDD object (the data reference) will need to be
materialized. This is achieved through an "action", which will
dump the RDD into a storage, or return its value data to the driver program.
Here is a word count example
#
Get initial RDD from the context
file
= spark.textFile("hdfs://...")
#
Three consecutive transformation of the RDD
counts
= file.flatMap(lambda line: line.split(" "))
.map(lambda word: (word, 1))
.reduceByKey(lambda a, b: a + b)
#
Materialize the RDD using an action
counts.saveAsTextFile("hdfs://...")
When the driver program starts its execution, it builds up a graph where nodes are RDD and edges are transformation steps. However, no execution is happening at the cluster until an action is encountered. At that point, the driver program will ship the execution graph as well as the code block to the cluster, where every worker server will get a copy.
The execution graph is a DAG.
·
Each DAG is a atomic unit of execution.
·
Each source node (no incoming edge) is an external data source
or driver memory
·
Each intermediate node is a RDD
·
Each sink node (no outgoing edge) is an external data source or
driver memory
·
Green edge connecting to RDD represents a transformation.
Red edge connecting to a sink node represents an action