RDD弹性分布式数据集(一)

来源:互联网 发布:公司取名软件注册码 编辑:程序博客网 时间:2024/05/28 05:17

1.自动容错

2.只读,批量创建。

3.一个RDD包含多个分区,RDD相互依赖

如果RDD的每个分区最多只能被一个Child RDD的一个分区使用,则称之为narrow dependency;若多个Child RDD分区都可以依赖,则称之为wide dependency。不同的操作依据其特性,可能会产生不同的依赖。例如map操作会产生narrow dependency,而join操作则产生wide dependency。


bin/spark-shell --executor-memory 1G --total-executor-cores 4 --executor-cores 1

4.创建  scala> val a = sc.parallelize(1 to 9 ,3)   普通数组创建RDD,包含1到9个数字,分别在3个分区中

  操作linux文件  scala> val b = sc.textFile("file:///usr/spark1/README.md")   文件每一行是一个RDD中一个元素。(默认是操作hdfs文件系统)


TransformationMeaningmap(func) Return a new distributed dataset formed by passing each element of the source through a function func.filter(func) Return a new dataset formed by selecting those elements of the source on which func returns true.flatMap(func) Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).mapPartitions(func) Similar to map, but runs separately on each partition (block) of the RDD, so func must be of type Iterator<T> => Iterator<U> when running on an RDD of type T.mapPartitionsWithIndex(func) Similar to mapPartitions, but also provides func with an integer value representing the index of the partition, so func must be of type (Int, Iterator<T>) => Iterator<U> when running on an RDD of type T.sample(withReplacement, fraction, seed) Sample a fraction fraction of the data, with or without replacement, using a given random number generator seed.union(otherDataset) Return a new dataset that contains the union of the elements in the source dataset and the argument.intersection(otherDataset) Return a new RDD that contains the intersection of elements in the source dataset and the argument.distinct([numTasks])) Return a new dataset that contains the distinct elements of the source dataset.groupByKey([numTasks]) When called on a dataset of (K, V) pairs, returns a dataset of (K, Iterable<V>) pairs. Note: If you are grouping in order to perform an aggregation (such as a sum or average) over each key, using reduceByKey or aggregateByKey will yield much better performance. Note: By default, the level of parallelism in the output depends on the number of partitions of the parent RDD. You can pass an optional numTasks argument to set a different number of tasks.reduceByKey(func, [numTasks]) When called on a dataset of (K, V) pairs, returns a dataset of (K, V) pairs where the values for each key are aggregated using the given reduce function func, which must be of type (V,V) => V. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.aggregateByKey(zeroValue)(seqOp, combOp, [numTasks]) When called on a dataset of (K, V) pairs, returns a dataset of (K, U) pairs where the values for each key are aggregated using the given combine functions and a neutral "zero" value. Allows an aggregated value type that is different than the input value type, while avoiding unnecessary allocations. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.sortByKey([ascending], [numTasks]) When called on a dataset of (K, V) pairs where K implements Ordered, returns a dataset of (K, V) pairs sorted by keys in ascending or descending order, as specified in the boolean ascending argument.join(otherDataset, [numTasks]) When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (V, W)) pairs with all pairs of elements for each key. Outer joins are supported through leftOuterJoin, rightOuterJoin, and fullOuterJoin.cogroup(otherDataset, [numTasks]) When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (Iterable<V>, Iterable<W>)) tuples. This operation is also called groupWith.cartesian(otherDataset) When called on datasets of types T and U, returns a dataset of (T, U) pairs (all pairs of elements).pipe(command, [envVars]) Pipe each partition of the RDD through a shell command, e.g. a Perl or bash script. RDD elements are written to the process's stdin and lines output to its stdout are returned as an RDD of strings.coalesce(numPartitions) Decrease the number of partitions in the RDD to numPartitions. Useful for running operations more efficiently after filtering down a large dataset.repartition(numPartitions) Reshuffle the data in the RDD randomly to create either more or fewer partitions and balance it across them. This always shuffles all data over the network.repartitionAndSortWithinPartitions(partitioner) Repartition the RDD according to the given partitioner and, within each resulting partition, sort records by their keys. This is more efficient than calling repartition and then sorting within each partition because it can push the sorting down into the shuffle machinery.



(1)Map    :map对RDD中的每个元素都执行一个指定的函数来产生一个新的RDD。任何原RDD中的元素在新的RDD中都有且只有一个元素与之对应。

      RDD元素一个对一个

  (1_1) reduce:是一个actions,最后得到一个值


(2)mapPartitions:

     处理单元由原来的RDD中的每个值变为每个分区,但最后输出对应个数相同


(3)flatMap:RDD元素一个变多个。像单词统计那样,

       scala> val wordCounts = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)  解析:不断处理每行元素按空格分开,得到以一个单词为元素的RDD。又对每个元素变为这样(word,1)。最后的reduceByKey就自己理解了,key相同的元素,value进行a+b。


(4)mapvalue:key不处理,处理value。





0 0