./bin/spark-shell --master local ./bin/pyspark --master local The --master option specifies the master URL for a distributed cluster, or local to run locally with one thread, or local[N] to run locally with N threads. Spark also supports pulling data sets into a cluster-wide in-memory cache. bin/spark-submit --master local test.py cat test.py from pyspark import SparkContext logFile = "/home/q/spark/spark-1.3.1-bin-hadoop2.6/README.md" # Should be some file on your system sc = SparkContext("local", "Simple App") logData = sc.textFile(logFile).cache() numAs = logData.filter(lambda s: 'a' in s).count() numBs = logData.filter(lambda s: 'b' in s).count() print("Lines with a: %i, lines with b: %i" % (numAs, numBs)) Spark programming The main abstraction Spark provides is a resilient distributed dataset (RDD), which is a collection of elements partitioned across the nodes of the cluster that can be operated on in parallel. A second abstraction in Spark is shared variables that can be used in parallel operations. (broadcast variables and accumulators) Only one SparkContext may be active per JVM. You must stop() the active SparkContext before creating a new one. In the Spark shell, a special interpreter-aware SparkContext (sc) is already created for you. There are two ways to create RDDs: parallelizing an existing collection in your driver program, or referencing a dataset in an external storage system. val distData = sc.parallelize(data) The elements of the collection are copied to form a distributed dataset that can be operated on in parallel. One important parameter for parallel collections is the number of partitions to cut the dataset into. textFile RDDS If using a path on the local filesystem, the file must also be accessible at the same path on worker nodes. All of Spark’s file-based input methods, including textFile, support running on directories, compressed files, and wildcards as well. The textFile method also takes an optional second argument for controlling the number of partitions of the file. RDD Operations RDDs support two types of operations: transformations, which create a new dataset from an existing one, and actions, which return a value to the driver program after running a computation on the dataset. Accumulators in Spark are used specifically to provide a mechanism for safely updating a variable when execution is split up across worker nodes in a cluster. collect() fetches the entire RDD to a single machine. take() only show a few elements of the RDD. The shuffle is Spark’s mechanism for re-distributing data so that it’s grouped differently across partitions. In Spark, data is generally not distributed across partitions to be in the necessary place for a specific operation. During computations, a single task will operate on a single partition - thus, to organize all the data for a single reduceByKey reduce task to execute, Spark needs to perform an all-to-all operation. It must read from all partitions to find all the values for all keys, and then bring together values across partitions to compute the final result for each key - this is called the shuffle. The cache() method is a shorthand for using the default storage level, which is StorageLevel.MEMORY_ONLY.
Program execution procedure
1.SparkContext connect to the cluster managers(which allocate resources across applications).
2.Spark acquires executors on nodes in the cluster(executor是负责处理应用程序的计算和存储相关的数据)