本期内容:
1 map、filter、flatmap操作回顾2 reduceByKey、groupByKey3 join、cogroup
以上 算子都是lazy的,count 、collect、saveAsTextFile等是action对于的算子(1)map操作(将原来RDD的每个数据项通过map中的用户自定义函数f映射转变为一个新的元素。源码中的map算子相当于初始化一个RDD,新RDD叫作MappedRDD(this, sc.clean(f)))。案例: map使用与任何元素,且对其作用的集合中的每一个元素循环遍历并调用其作为参数函数对每个遍历的元素进行具体化的处理。用函数封装map:object Transformation { def main(args: Array[String]) { //创建SparkConf对象,初始化Transformation的配置运行的参数 val conf = new SparkConf().setMaster("The Transformation").setMaster("local") //创建SparkContext对象,这是RDD创建的唯一入口,也是Driver的灵魂,是通往集群的唯一通道 val sc = new SparkContext(conf) //创建第一个RDD,通过集合创建RDD val nums = sc.parallelize(1 to 10) //开始算子操作:map val mapped = nums.map(item => 2*item) mapped.collect.foreach(println) } }
(2)flatmap( 将原来RDD中的每个元素通过函数f转换为新的元素,并将生成的RDD的每个集合中的元素合并为一个集合。内部创建 FlatMappedRDD(this, sc.clean(f)))def mapTransformation(sc:SparkContext) ={ val nums = sc.parallelize(1 to 10) //开始算子操作:map val mapped = nums.map(item => 2*item) mapped.collect.foreach(println) }
小方框表示RDD的一个分区,对分区进行flatMap函数操作,flatMap中传入的函数为f:T->U,T和U可以是任意的数据类型。将分区中的数据通过用户自定义函数f转换为新的数据。外部大方框可以认为是一个RDD分区,小方框代表一个集合。V1、V2、V3在一个集合作为RDD的一个数据项,转换为V’1、V’2、V’3后,将结合拆散,形成为RDD中的数据项。
val bigDate = Array("Spark","Hadoop","Scala","Flink") val bigString = sc.parallelize(bigDate) val words = bigString.flatMap(_.split(" ")) words.foreach(println) sc.stop()
使用函数封装:(3)filter:/* *封装flatmap * 创建以字符串为类型的parallelcollectionRDD * 首先通过传入的函数作为参数来作用于RDD的每个字符串进行单词切分(是以结合的方式存在的) * 然后将产生一个大的集合产生结果为{Spark,Hadoop,Scala,Flink} */ def flatmapTransformation(sc:SparkContext) ={ val bigDate = Array("Spark","Hadoop","Scala","Flink") val bigString = sc.parallelize(bigDate) val words = bigString.flatMap(_.split(" ")) words.foreach(println) }
(4)reduceByKey:def filterTransformation(sc:SparkContext) ={ val nums = sc.parallelize(1 to 10) //根据filter中作为参数的函数的Boolean来判断符合条件的元素,并根据这些元素构建新的MapPartitionRDD val filters = nums.filter(items => items%2 ==0) filters.collect.foreach(println) }
def reduceByKeyTransformation(sc:SparkContext) = { val lines = sc.textFile("C://Hello.txt", 1) //并行度为1(partition),读取本地文件 //根据类型推断,这边的lines是RDD[String] /** * 对初始的RDD进行Transformation级别的处理(高阶函数:map、filter、flatmap等) * 来进行具体的数据计算; * 将每一行的字符串拆分成单个的单词 */ val words = lines.flatMap(line => line.split(" ")) //对每一行的字符串进行单词拆分并把所以行的拆分结果通过flat合并成一个大的单词集合 /** * 对初始的RDD进行Transformation级别的处理(高阶函数:map、filter、flatmap等) * 来进行具体的数据计算; * 在单词拆分的基础上对每个单词实例计数为1,也就是word =>(word,1) */ val pairs = words.map { word => (word, 1) } /** * 对初始的RDD进行Transformation级别的处理(高阶函数:map、filter、flatmap等) * 来进行具体的数据计算; * 在每个单词实例计数为1的基础上统计每个单词在文件中出现的总次数 */ val wordCounts = pairs.reduceByKey(_ + _) //对相同的Key,进行value的累积(包括local和Reduce级别同时进行Reduce) // wordCounts.foreach(wordNumberPair => println(wordNumberPair._1 + ":" +wordNumberPair._2)) wordCounts.collect.foreach(wordNumberPair => println(wordNumberPair._1 + ":" + wordNumberPair._2)) }
(5)groupByKey:<span style="font-size:12px;"> /* *封装groupByKey * 安照分数进行排名 */ def groupByKeTransformation(sc:SparkContext) ={ val data =Array(Tuple2(100,"Spark"),Tuple2(90,"Hadoop"),Tuple2(95,"Scala")) val dataRDD = sc.parallelize(data) //创建RDD val groupedRDD = dataRDD.groupByKey()//按照相同的Key对value进行分组,分组后的value是一个集合 groupedRDD.collect.foreach(println) }</span>
(6)join:<span style="font-size:12px;"> /* * join 和 cogroup是所有Spark学习者必须掌握的内容 * join 将两者集合元素根据key将他们聚合到一块 * 大数据核心:整合数据孤岛没打通数据孤岛,所以大数据也是一个join的过程 * 例如:电商中将用户和商品join在一起 */ def joinTransformation(sc:SparkContext){ val studentNames = Array ( Tuple2(1,"Spark"), Tuple2(2,"Hadoop"), Tuple2(3,"Scala") ) val studentScores = Array ( Tuple2(1,100), Tuple2(1,90), Tuple2(1,96) ) val name = sc.parallelize(studentNames) val scores = sc.parallelize(studentScores) //使用join val nameAndScores = name.join(scores) nameAndScores.collect.foreach(println) }</span>
(7)cogroupcogroup函数原型<span style="font-size:10px;">def cogroup[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (Iterable[V], Iterable[W]))] = { if (partitioner.isInstanceOf[HashPartitioner] && keyClass.isArray) { throw new SparkException("Default partitioner cannot partition array keys.") } val cg = new CoGroupedRDD[K](Seq(self, other), partitioner) cg.mapValues { case Array(vs, w1s) => (vs.asInstanceOf[Iterable[V]], w1s.asInstanceOf[Iterable[W]]) } }</span>
<span style="font-size:10px;">def cogroup[W1, W2, W3](other1: RDD[(K, W1)], other2: RDD[(K, W2)], other3: RDD[(K, W3)], partitioner: Partitioner) : RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2], Iterable[W3]))] def cogroup[W1, W2, W3](other1: RDD[(K, W1)], other2: RDD[(K, W2)], other3: RDD[(K, W3)], numPartitions: Int) :RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2], Iterable[W3]))] def cogroup[W1, W2, W3](other1: RDD[(K, W1)], other2: RDD[(K, W2)], other3: RDD[(K, W3)]) : RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2], Iterable[W3]))] def cogroup[W1, W2](other1: RDD[(K, W1)], other2: RDD[(K, W2)],partitioner: Partitioner) : RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2]))] def cogroup[W1, W2](other1: RDD[(K, W1)], other2: RDD[(K, W2)], numPartitions: Int) : RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2]))] def cogroup[W1, W2](other1: RDD[(K, W1)], other2: RDD[(K, W2)]) : RDD[(K, (Iterable[V], Iterable[W1], Iterable[W2]))] def cogroup[W](other: RDD[(K, W)], partitioner: Partitioner) : RDD[(K, (Iterable[V], Iterable[W]))] def cogroup[W](other: RDD[(K, W)], numPartitions: Int): RDD[(K, (Iterable[V], Iterable[W]))] def cogroup[W](other: RDD[(K, W)]): RDD[(K, (Iterable[V], Iterable[W]))]</span>
/* *cogroup大数据中第二重要的算子 */ def cogroupTransformation(sc:SparkContext) ={ val list1 = sc.parallelize(List((1,"Scala"),(2,"Java"),(3,"C"))) val list2 = sc.parallelize(List((1,"Spark"),(2,"Hadoop"),(3,"linux"))) val list3 = sc.parallelize(List((1,"RDD"),(2,"MapReduce"))) val result = list1.cogroup(list2,list3) result.collect.foreach(println) }
SparkContext:
mian()def SparkContext(name:String)={ //创建SparkConf对象,初始化Transformation的配置运行的参数 val conf = new SparkConf().setMaster("The Transformation").setMaster("local") //创建SparkContext对象,这是RDD创建的唯一入口,也是Driver的灵魂,是通往集群的唯一通道 val sc = new SparkContext(conf) sc }
def main(args: Array[String]) { val sc = new SparkContext("Transformation") mapTransformation(sc) filterTransformation(sc) flatmapTransformation(sc) groupByKeTransformation(sc) reduceByKeyTransformation(sc) joinTransformation(sc) cogroupTransformation(sc) //停止SparkContext,销毁Driver对象,释放资源 sc.stop() }
王家林老师是大数据技术集大成者,中国Spark第一人:DT大数据梦工厂
新浪微博:www.weibo.com/ilovepains/
微信公众号:DT_Spark
博客:http://.blog.sina.com.cn/ilovepains
TEL:18610086859
Email:18610086859@vip.126.com