Spark广播变量broadcast案例

Posted Mr.zhou_Zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark广播变量broadcast案例相关的知识,希望对你有一定的参考价值。

Spark共享变量之广播变量broadcast

package com.zxy.spark.core.Day04

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD

/**
 * file:broadcast
 * author:zxy
 * date:2021-06-10
 * desc:共享变量之广播变量
 */
object Demo11_share {
    def main(args: Array[String]): Unit = {
        val sc: SparkContext = new SparkContext(new SparkConf().setMaster("local[*]").setAppName("share"))
        //共享变量性别
        val genderMap = Map(
            "0" -> "小姐姐",
            "1" -> "小哥哥"
        )
        //共享变量名字
        val nameMap = Map(
            "1" -> "鸣人",
            "2" -> "博人",
            "3" -> "佐助",
            "4" -> "凯",
            "5" -> "卡卡西"
        )
    
        val listRDD: RDD[(String, String, Int)] = sc.parallelize(List(
            ("1", "1", 30),
            ("2", "0", 18),
            ("3", "1", 31),
            ("4", "0", 40),
            ("5", "2", 41)
        ))
        val genders: Broadcast[Map[String, String]] = sc.broadcast(genderMap)
        val names: Broadcast[Map[String, String]] = sc.broadcast(nameMap)
        listRDD.map(stu => {
            val id: String = stu._1
            val sex: String = stu._2
            val age: Int = stu._3
            
            val gender: String = genders.value.getOrElse(sex,"未知")
            val name: String = names.value.getOrElse(id,"未知")
            Student(id,name,gender,age)
        }).foreach(println)
    }
}
case class Student(id:String,name:String,sex:String,age:Int){}

在这里插入图片描述

以上是关于Spark广播变量broadcast案例的主要内容,如果未能解决你的问题,请参考以下文章

Spark调优Broadcast广播变量

Spark Broadcast总结

访问不同类中的 Spark 广播变量

Spark-广播变量

如何用java创建一个用于构建广播变量spark的类标记?

Spark-Join优化之Broadcast