大数据平台与编程第6章 Spark RDD编程.pptx
第6章SparkRDD编程;?RDD概述;RDD概述;;4)一个Partitioner,即RDD的分片函数。当前Spark中实现了两种类型的分片函数,一个是基于哈希的HashPartitioner,另外一个是基于范围的RangePartitioner。只有对于于key-value的RDD,才会有Partitioner,非key-value的RDD的Parititioner的值是None。Partitioner函数不但决定了RDD本身的分片数量,也决定了ParentRDDShuffle输出时的分片数量;RDD概述;;;;;;;;?RDD编程;RDD编程;RDD编程;2.由外部存储系统的数据集创建;;;;;;;;;;(3)放回抽样
scalavarsample1=rdd.sample(true,0.4,2)
sample1:org.apache.spark.rdd.RDD[Int]=PartitionwiseSampledRDD[21]atsampleatconsole:26
(4)打印放回抽样结果
scalasample1.collect()
res16:Array[Int]=Array(1,2,2,7,7,8,9)
(5)不放回抽样
scalavarsample2=rdd.sample(false,0.2,3)
sample2:org.apache.spark.rdd.RDD[Int]=PartitionwiseSampledRDD[22]atsampleatconsole:26
(6)打印不放回抽样结果
scalasample2.collect()
res17:Array[Int]=Array(1,9);;(3)打印去重后生成的新RDD
scalaunionRDD.collect()
res20:Array[Int]=Array(1,9,5,6,2)
(4)对RDD(指定并行度为2)
scalavalunionRDD=distinctRdd.distinct(2)
unionRDD:org.apache.spark.rdd.RDD[Int]=MapPartitionsRDD[40]atdistinctatconsole:26
(5)打印去重后生成的新RDD
scalaunionRDD.collect()
res21:Array[Int]=Array(6,2,1,9,5);;;;;;(3)将脚本作用该RDD并打印
scalardd.pipe(/opt/module/spark/pipe.sh).collect()
res18:Array[String]=Array(AA,hi,Hello,how,are,you)
(4)创建一个有两个分区的RDD
scalavalrdd=sc.parallelize(List(hi,Hello,how,are,you),2)
rdd:org.apache.spark.rdd.RDD[String]=ParallelCollectionRDD[52]atparallelizeatconsole:24
(5)将脚本作用该RDD并打印
scalardd.pipe(/opt/module/spark/pipe.sh).collect()
res19:Array[String]=Array(AA,hi,Hello,AA,how,are,you);;;;;;;(3)第一个RDD组合第二个RDD并打印
scalardd1.zip(rdd2).collect
res1:Array[(Int,String)]=Array((1,a),(2,b),(3,c))
(4)第二个RDD组合第一个RDD并打印
scalardd2.zip(rdd1).collect
res2:Array[(String,Int)]=Array((a,1),(b,2),(c,3))
(5)创建第三个RDD(与1,2分区数不同)
scalavalrdd3=sc.parallelize(Array(a,b,c),2)
rdd3:org.apache.spark.rdd.RDD[String]=ParallelCollectionRDD[5]atparallelizeatconsole:24
(6)第一个RDD组合第三个RDD并打印
scalardd1.zip(rdd3).collect
java.la