spark笔记

./bin/spark-shell --master local[2]

./bin/pyspark --master local[2]

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[4] 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是负责处理应用程序的计算和存储相关的数据)
3.发送application code至executors
4.发送tasks至executors去执行

转载于:https://my.oschina.net/u/2510197/blog/614854

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值