Spark-Core之算子详解(七)

Spark-Core之算子详解(七),第1张

Spark-Core之算子详解(七)
SparkCore 算子详解
  开始之前,先希望大家生活乐观,天天向上。没有风可以把温柔的人吹倒,但温柔的风一定能吹散所有的不愉快。希望大家在为生活奔波的同时不忘初心,砥砺前行,永远能打倒困难。


  SparkCore是spark的核心内容,是早起实现数据处理的主要Spark工具,前面说了SparkCore数据处理就是就是RDD之间的互相转换,那么怎么样让RDD实现随心所欲的转换呢?

Spark算子是实现RDD转换的唯一手段。


Spark算子主要分为两大类Transformations、Action,推荐Spark官网地址。

1、Transformations算子
  哈哈哈,先说说Transformations算子,因为刚开始学习Spark那会,Transformations算子总是让我头皮发麻,map算子已经是我理解的极限了,想想都是泪了。但是作为过来人,很高兴的是刀山火海后已经了解了,哈哈哈哈哈哈。好了 ,我是菜鸟,至于transformations算子,实际上是一种惰性算子,何为惰性,相当于老师布置了作业,但是不催我就是不写,哎,就是玩。只有这合适的触发器(action算子,相当于老师的苦口婆心)下。才会执行。那Transformations算子有哪些呢,怎么用?为什么要用?主要是实现什么功能呢?
这里是官网描述的常用Transformations算子。

transformations  算子类型Meaning or funcrtonmap(func)单值算子传递一个函数,将原RDD中每一个元素经过函数 *** 作,转换成新的RDD,注意RDD的类型没有变,只是RDD中的元素经过一对一转换成为了新的数值或者数据类型,这个应该是最朴素的RDD转换算子filter(func)单值算子过滤算子,将原RDD的元素经过过滤条件,如果为真则留下,为假则过滤掉。flatMap(func)单值算子数据扁平化算子,对集合中每个元素进行 *** 作然后再扁平化。所谓扁平化,我理解的就是降维。mapPartitions(func)单值算子类似于 map,但是不同处在于map针对单个元素,,然而mapPartitions针对分区 *** 作,先对RDD进行partition,再把每个partition进行map函数,主要和map的用法区别在于和数据库等外部连接, *** 作数据库不能每一个元素都连接一次,这是一种网络消耗,一般是一个分区数据连接一次,要避免连接次数过多mapPartitionsWithIndex(func)单值算子极其类似于mapPartitions,但是比mapPartitions多了一个参数,分区索引,可以指定分区执行不同的 *** 作 ,也可以打印查看数据分区信息。sample(func)单值算子Sample a fraction fraction of the data, with or without replacement, using a given random number generator seed.union(func)单值算子Return a new dataset that contains the union of the elements in the source dataset and the argument.intersection(func)单值算子Return a new RDD that contains the intersection of elements in the source dataset and the argument.distinct(func)单值算子Return a new dataset that contains the distinct elements of the source dataset.groupByKey(func)单值算子When called on a dataset of (K, V) pairs, returns a dataset of (K, Iterable) 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 the parent RDD. You can pass an optional numPartitions argument to set a different number of tasks.reduceByKey(func)单值算子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(func)单值算子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(func)单值算子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(func)单值算子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(func)单值算子When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (Iterable, Iterable)) tuples. This operation is also called groupWith.cartesian(func)单值算子When called on datasets of types T and U, returns a dataset of (T, U) pairs (all pairs of elements).pipe(func)单值算子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(func)单值算子Decrease the number of partitions in the RDD to numPartitions. Useful for running operations more efficiently after filtering down a large dataset.repartition(func)单值算子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(func)单值算子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.

2、Actions算子

Actions算子类型Meaning or funcrtonreduce(func)Aggregate the elements of the dataset using a function func (which takes two arguments and returns one). The function should be commutative and associative so that it can be computed correctly in parallel.collect()Return all the elements of the dataset as an array at the driver program. This is usually useful after a filter or other operation that returns a sufficiently small subset of the data.count()Return the number of elements in the dataset.first()Return the first element of the dataset (similar to take(1)).take(n)Return an array with the first n elements of the dataset.takeSample(withReplacement, num, [seed])Return an array with a random sample of num elements of the dataset, with or without replacement, optionally pre-specifying a random number generator seed.takeOrdered(n, [ordering])Return the first n elements of the RDD using either their natural order or a custom comparator.saveAsTextFile(path)Write the elements of the dataset as a text file (or set of text files) in a given directory in the local filesystem, HDFS or any other Hadoop-supported file system. Spark will call toString on each element to convert it to a line of text in the file.saveAsSequenceFile(path)Write the elements of the dataset as a Hadoop SequenceFile in a given path in the local filesystem, HDFS or any other Hadoop-supported file system. This is available on RDDs of key-value pairs that implement Hadoop’s Writable interface. In Scala, it is also available on types that are implicitly convertible to Writable (Spark includes conversions for basic types like Int, Double, String, etc).saveAsObjectFile(path)Write the elements of the dataset in a simple format using Java serialization, which can then be loaded using SparkContext.objectFile().countByKey()only available on RDDs of type (K, V). Returns a hashmap of (K, Int) pairs with the count of each key.foreach(func)Run a function func on each element of the dataset. This is usually done for side effects such as updating an Accumulator or interacting with external storage systems.Note: modifying variables other than Accumulators outside of the foreach() may result in undefined behavior. See Understanding closures for more details.
本篇持续更新,先进行上表算子从原理到源码详解。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5654245.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存