makeRdd的创建
RDD的创建
rdd的创建方式大致分为3种:从集合中创建rdd,从外部存储,从其他rdd创建
从集合中创建
分为parallelize和makeRdd的2种方式,异同点在于makeRdd还可以指定数据的分区位置
val rdd = sc.parallelize(Array(1,2,3,4,5,6,7,8))
val rdd = sc.makeRdd(Array(1,2,3,4,5,6,7,8))
val seq = List((1, List("slave01")),| (2, List("slave02")))
val guigu3 = sc.makeRDD(seq)
//scala>guigu3.preferredLocations(guigu3.partitions(1))
//res26: Seq[String] = List(slave02)
TransFormation
def map[U:classTag](f: T => U)
一对一转换
def filter(f: T=>Boolean): RDD[T]
传入一个Boolean的方法,过滤数据
def flatmap[U:ClassTag](f: T => TraversableOnce): RDD[U]
一对多 并且将多压平
def mapPartition[U:ClassTag](f:Iterator[T] => Iterator[U],preservesPartitioning: Boolean = false):RDD[U]
对于一个分区的数据执行一个函数,性能比map要高
def mapPartitionsWithIndex[U:ClassTag](f:(Int,Iterator[T])=>Iterator[U],preservesPartitioning:Boolean = false):RDD[U]
指定某一个分区
def sample(withReplacement:Boolean,fraction:Double,seed: Long = Utils.random.nextLong):RDD[T]
有放回和无放回抽样
def union(other: RDD[T]): RDD[T]
联合一个RDD,返回组合的RDD
def intersection(other: RDD[T]):RDD[T]
rdd求交集
def distinct():RDD[T]
去重
def partitionBy(partitioner: Partitioner):RDD[(k,v)]
用提供的分区器分区
def reduceByKey(func:(v,v) => v):RDD[(k,v)]
根据Key进行聚合 预聚合
def groupByKey(partitioner:Partitioner):RDD[(k,Iterable[v])]
将key相同的value聚合在一起
def combinByKey[C]( createCombiner:V => C, mergeValue:(C,V) => C, mergeCombiners:(C,C) => C, numPartitions: Int):RDD[(K,C)]
def aggregateByKey[U:ClassTag](zeroValue:U,partitioner:Partitioner)(seqOp:(U,V)=>U,combOp:(U,U)=>U):RDD[(k,u)]
是CombineByKey的简化版,可以通过zeroValue直接提供一个初始值
def foldBykey(zeroValue:V,partitioner:Partitioner)(func:(v,v)=>v):RDD(K,V))
该函数为aggeregateByKey的简化版,seqOp和combOp一样,相同
def sortByKey
根据Key来进行排序,如果key目前不支持排序,需求with Order接口,实现compare方法,告诉spark key的大小判定
def sortBy[K](f:(T)=>K,ascending:Boolean=true,numPartitons:Int = this.partitions.length):RDD[(K,V)]
根据f函数提供可以排序的key
def join[W](other:RDD[(K,W)],partition:Partitioner):RDD[K,(V,W)]
连接二个RDD的数据
def cogroup[W](other:RDD[(K,W)],partitioner:Partitioner):RDD[K,(Iterable[V],Iterable[W]))]
分别将相同key的数据聚集在一起
def cartesian[U:ClassTag](other:RDD[U]):RDD[(T,U)]
做笛卡尔积 n*m
def pipe(command:String):RDD[]String
执行外部脚本
def coalesce(numPartitions:Int,shuffle:Boolean = false,partitionCoalescer:Option[PartitionCoalescer] = Option.empty)(impliciatord:Order[T] = null): RDD[T]
缩减分区数,用于大数据集过滤后。提高小数据集的执行效率
def repartition(numPartitions:Int)(implicit ord: Ordering[T] = null): RDD[T]
重新分区
def glom():RDD[Array[T]]
def mapValues[U](f: V => U):RDD[(K,U)]
对于kv结构RDD,只处理value
def subtracte(other: RDD[T]): RDD[T]
去掉和other重复的元素
网友评论