0% found this document useful (0 votes)
5 views7 pages

Apache Spark Application Submission Guide

Uploaded by

mitmak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Apache Spark Application Submission Guide

Uploaded by

mitmak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Apache Spark

Submit the spark application using the following command:

spark-submit --class SparkWordCount --master local [Link]

If it is executed successfully, then you will find the output given below. The OK letting in
the following output is for user identification and that is the last line of the program. If
you carefully read the following output, you will find different things, such as:

 successfully started service 'sparkDriver' on port 42954


 MemoryStore started with capacity 267.3 MB
 Started SparkUI at [Link]
 Added JAR file:/home/hadoop/piapplication/[Link]
 ResultStage 1 (saveAsTextFile at [Link]) finished in 0.566 s
 Stopped Spark web UI at [Link]
 MemoryStore cleared

15/07/08 13:56:04 INFO Slf4jLogger: Slf4jLogger started


15/07/08 13:56:04 INFO Utils: Successfully started service 'sparkDriver' on
port 42954.
15/07/08 13:56:04 INFO Remoting: Remoting started; listening on addresses
:[[Link]://sparkDriver@[Link]:42954]
15/07/08 13:56:04 INFO MemoryStore: MemoryStore started with capacity 267.3 MB
15/07/08 13:56:05 INFO HttpServer: Starting HTTP Server
15/07/08 13:56:05 INFO Utils: Successfully started service 'HTTP file server'
on port 56707.
15/07/08 13:56:06 INFO SparkUI: Started SparkUI at [Link]
15/07/08 13:56:07 INFO SparkContext: Added JAR
file:/home/hadoop/piapplication/[Link] at
[Link] with timestamp 1436343967029
15/07/08 13:56:11 INFO Executor: Adding file:/tmp/spark-45a07b83-42ed-42b3-
b2c2-823d8d99c5af/userFiles-df4f4c20-a368-4cdd-a2a7-39ed45eb30cf/[Link] to
class loader
15/07/08 13:56:11 INFO HadoopRDD: Input split:
file:/home/hadoop/piapplication/[Link]+54
15/07/08 13:56:12 INFO Executor: Finished task 0.0 in stage 0.0 (TID 0). 2001
bytes result sent to driver
(MapPartitionsRDD[5] at saveAsTextFile at [Link]), which is now
runnable
15/07/08 13:56:12 INFO DAGScheduler: Submitting 1 missing tasks from
ResultStage 1 (MapPartitionsRDD[5] at saveAsTextFile at [Link])
15/07/08 13:56:13 INFO DAGScheduler: ResultStage 1 (saveAsTextFile at
[Link]) finished in 0.566 s
15/07/08 13:56:13 INFO DAGScheduler: Job 0 finished: saveAsTextFile at
[Link], took 2.892996 s

25
Apache Spark

OK
15/07/08 13:56:13 INFO SparkContext: Invoking stop() from shutdown hook
15/07/08 13:56:13 INFO SparkUI: Stopped Spark web UI at
[Link]
15/07/08 13:56:13 INFO DAGScheduler: Stopping DAGScheduler
15/07/08 13:56:14 INFO MapOutputTrackerMasterEndpoint:
MapOutputTrackerMasterEndpoint stopped!
15/07/08 13:56:14 INFO Utils: path = /tmp/spark-45a07b83-42ed-42b3-b2c2-
823d8d99c5af/blockmgr-ccdda9e3-24f6-491b-b509-3d15a9e05818, already present as
root for deletion.
15/07/08 13:56:14 INFO MemoryStore: MemoryStore cleared
15/07/08 13:56:14 INFO BlockManager: BlockManager stopped
15/07/08 13:56:14 INFO BlockManagerMaster: BlockManagerMaster stopped
15/07/08 13:56:14 INFO SparkContext: Successfully stopped SparkContext
15/07/08 13:56:14 INFO Utils: Shutdown hook called
15/07/08 13:56:14 INFO Utils: Deleting directory /tmp/spark-45a07b83-42ed-42b3-
b2c2-823d8d99c5af
15/07/08 13:56:14 INFO OutputCommitCoordinator$OutputCommitCoordinatorEndpoint:
OutputCommitCoordinator stopped!

Step 5: Checking output


After successful execution of the program, you will find the directory named outfile in
the spark-application directory.

The following commands are used for opening and checking the list of files in the outfile
directory.

$ cd outfile
$ ls
Part-00000 part-00001 _SUCCESS

The commands for checking output in part-00000 file are:

$ cat part-00000
(people,1)
(are,2)
(not,1)
(as,8)
(beautiful,2)
(they, 7)

26
Apache Spark

(look,1)

The commands for checking output in part-00001 file are:

$ cat part-00001
(walk, 1)
(or, 1)
(talk, 1)
(only, 1)
(love, 1)
(care, 1)
(share, 1)

Go through the following section to know more about the ‘spark-submit’ command.

Spark-submit Syntax
spark-submit [options] <app jar | python file> [app arguments]

Options
The table given below describes a list of options:-

[Link] Option Description

1 --master spark://host:port, mesos://host:port, yarn, or local.

Whether to launch the driver program locally


2 --deploy-mode ("client") or on one of the worker machines inside the
cluster ("cluster") (Default: client).

3 --class Your application's main class (for Java / Scala apps).

4 --name A name of your application.

Comma-separated list of local jars to include on the


5 --jars
driver and executor classpaths.

Comma-separated list of maven coordinates of jars to


6 --packages
include on the driver and executor classpaths.

Comma-separated list of additional remote


7 --repositories repositories to search for the maven coordinates
given with --packages.

27
Apache Spark

Comma-separated list of .zip, .egg, or .py files to


8 --py-files
place on the PYTHON PATH for Python apps.

Comma-separated list of files to be placed in the


9 --files
working directory of each executor.

10 --conf (prop=val) Arbitrary Spark configuration property.

Path to a file from which to load extra properties. If


11 --properties-file
not specified, this will look for conf/spark-defaults.

12 --driver-memory Memory for driver (e.g. 1000M, 2G) (Default: 512M).

13 --driver-java-options Extra Java options to pass to the driver.

14 --driver-library-path Extra library path entries to pass to the driver.

Extra class path entries to pass to the driver.


15 --driver-class-path Note that jars added with --jars are automatically
included in the classpath.

16 --executor-memory Memory per executor (e.g. 1000M, 2G) (Default: 1G).

17 --proxy-user User to impersonate when submitting the application.

18 --help, -h Show this help message and exit.

19 --verbose, -v Print additional debug output.

20 --version Print the version of current Spark.

21 --driver-cores NUM Cores for driver (Default: 1).

22 --supervise If given, restarts the driver on failure.

23 --kill If given, kills the driver specified.

24 --status If given, requests the status of the driver specified.

25 --total-executor-cores Total cores for all executors.

Number of cores per executor. (Default: 1 in YARN


26 --executor-cores mode, or all available cores on the worker in
standalone mode).

28
Apache Spark

29
6. ADVANCED SPARK PROGRAMMING Apache Spark

Spark contains two different types of shared variables- one is broadcast variables and
second is accumulators.

 Broadcast variables: used to efficiently, distribute large values.

 Accumulators: used to aggregate the information of particular collection.

Broadcast Variables
Broadcast variables allow the programmer to keep a read-only variable cached on each
machine rather than shipping a copy of it with tasks. They can be used, for example, to
give every node, a copy of a large input dataset, in an efficient manner. Spark also
attempts to distribute broadcast variables using efficient broadcast algorithms to reduce
communication cost.

Spark actions are executed through a set of stages, separated by distributed “shuffle”
operations. Spark automatically broadcasts the common data needed by tasks within
each stage.

The data broadcasted this way is cached in serialized form and is deserialized before
running each task. This means that explicitly creating broadcast variables, is only useful
when tasks across multiple stages need the same data or when caching the data in
deserialized form is important.

Broadcast variables are created from a variable v by calling


[Link](v). The broadcast variable is a wrapper around v, and its
value can be accessed by calling the value method. The code given below shows this:

scala> val broadcastVar = [Link](Array(1, 2, 3))

Output:

broadcastVar: [Link][Array[Int]] = Broadcast(0)

After the broadcast variable is created, it should be used instead of the value v in any
functions run on the cluster, so that v is not shipped to the nodes more than once. In
addition, the object v should not be modified after its broadcast, in order to ensure that
all nodes get the same value of the broadcast variable.

Accumulators
Accumulators are variables that are only “added” to through an associative operation
and can therefore, be efficiently supported in parallel. They can be used to implement
counters (as in MapReduce) or sums. Spark natively supports accumulators of numeric
types, and programmers can add support for new types. If accumulators are created
with a name, they will be displayed in Spark’s UI. This can be useful for understanding
the progress of running stages (NOTE: this is not yet supported in Python).
30
Apache Spark

An accumulator is created from an initial value v by calling


[Link](v). Tasks running on the cluster can then add to it using
the add method or the += operator (in Scala and Python). However, they cannot read
its value. Only the driver program can read the accumulator’s value, using
its value method.

The code given below shows an accumulator being used to add up the elements of an
array:

scala> val accum = [Link](0)

scala> [Link](Array(1, 2, 3, 4)).foreach(x => accum += x)

If you want to see the output of above code then use the following command:

scala> [Link]

Output

res2: Int = 10

Numeric RDD Operations


Spark allows you to do different operations on numeric data, using one of the
predefined API methods. Spark’s numeric operations are implemented with a streaming
algorithm that allows building the model, one element at a time.

These operations are computed and returned as a StatusCounter object by calling


status() method.

The following is a list of numeric methods available in StatusCounter.

[Link] Method & Meaning

count()
1
Number of elements in the RDD.

Mean()
2
Average of the elements in the RDD.

Sum()
3
Total value of the elements in the RDD.

Max()
4
Maximum value among all elements in the RDD.

31

Common questions

Powered by AI

Apache Spark supports various numeric operations on RDDs such as count, mean, sum, and max. These operations allow users to efficiently analyze and aggregate data as each operation builds statistical models or summaries of the numeric data within RDDs. These operations contribute significantly to data processing by enabling streamlined calculation of important metrics that help in further data analysis .

Apache Spark manages distributed tasks by dividing them into stages separated by shuffle operations. Each stage processes a portion of the data and only outputs the shuffle files needed for the next stage. This method reduces data movement across nodes, optimizing performance. Spark utilizes a Directed Acyclic Graph (DAG) for task execution and applies task pipelining within stages to enhance execution flow and resource utilization efficiency .

The 'spark-submit' command enhances fault tolerance by offering a '--supervise' option, which automatically restarts the driver upon failure. This, combined with checkpointing and task speculation features within Spark, ensures that failed tasks are re-executed, and driver failures are mitigated with restarts. This robustness allows applications to recover from partial failures without restarting an entire job, maintaining high availability .

The 'spark-submit' command allows users to submit Spark applications for execution with several options for configuration. Users specify the master URL for resource managers like YARN, standalone or Mesos, deploy modes such as client or cluster, and define application specifics like the main class for Java/Scala apps. It also enables setting memory allocation, specifying jar dependencies, and customizing Spark through configuration properties. Spark-submit supports controlling tasks' execution, such as supervising failures or checking driver status .

SparkContext plays a crucial role in managing both broadcast variables and accumulators. It creates broadcast variables by caching them on all executor nodes, ensuring tasks have efficient and consistent access without repeated data shipment. SparkContext also initializes accumulators, allowing tasks to increment them for aggregation during distributed computations. Only the driver program can read their values, maintaining integrity across computations .

The '--deploy-mode' option dictates where the Spark driver runs, either in 'client' mode or 'cluster' mode. In 'client' mode, the driver runs on the local machine from which the application is submitted, useful for interactive sessions. In 'cluster' mode, the driver runs on one of the cluster's worker nodes, which is beneficial for resource management and fault-tolerance in large-scale deployments. This choice affects network traffic, latency, and resource allocation .

Apache Spark efficiently handles broadcast variables by caching a read-only variable on each machine rather than passing a copy of it with tasks. Spark uses efficient broadcast algorithms to distribute these variables, reducing communication costs. The data is cached in serialized form and deserialized before running tasks. This mechanism is particularly useful when multiple stages require the same data or when accessing cached, deserialized data is important .

Spark's '--conf' option allows users to set any arbitrary Spark configuration property directly at runtime, enhancing flexibility. This option is ideal for temporary or job-specific tweaking of performance parameters. The '--properties-file' option provides a more structured approach, allowing users to load predefined key-value pairs from a file, ensuring consistency and reusability across submissions. Together, these options enable comprehensive tuning of performance and behavior of Spark applications .

Modifying a broadcast variable after it has been broadcasted can lead to inconsistent state across the executor nodes, as broadcast variables are meant to be read-only after their initial distribution. Any attempt to change the broadcast variable will not change its cached copies across the executors, hence, should be avoided to ensure that all nodes operate with the same data, preventing logical errors and data inconsistencies .

Accumulators in Apache Spark are variables that can be incremented in parallel through associative operations, such as sums and counters, but cannot be read by the tasks. This is different from regular variables which are typically read and modified by the tasks. Accumulators allow aggregating information efficiently across tasks. Only the driver can read their values to monitor progress, which is not applicable to regular variables .

You might also like