大数据Spark(六十七):Transformation转换算子distinct和mapValues

文章目录

Transformation转换算子distinct和mapValues

一、distinct

二、mapValues


Transformation转换算子distinct和mapValues

一、distinct

用于对RDD数据进行去重,返回一个新的RDD,返回RDD包含原始RDD中所有唯一元素。实际底层distinct的实现为map+reduceByKey+map实现。

Java代码:

java 复制代码
SparkConf conf = new SparkConf().setMaster("local").setAppName("DistinctTest");
JavaSparkContext sc = new JavaSparkContext(conf);

JavaRDD<String> rdd = sc.parallelize(Arrays.asList("a", "b", "c", "a", "b", "c"));

//如果手动实现去重:mapToPair+reduceByKey+map
JavaPairRDD<String, Integer> rdd1 = rdd.mapToPair(new PairFunction<String, String, Integer>() {
    @Override
    public Tuple2<String, Integer> call(String s) throws Exception {
        return new Tuple2<>(s, 1);
    }
});
JavaPairRDD<String, Integer> rdd2 = rdd1.reduceByKey(new Function2<Integer, Integer, Integer>() {
    @Override
    public Integer call(Integer v1, Integer v2) throws Exception {
        return v1 + v2;
    }
});

rdd2.map(new Function<Tuple2<String,Integer>, String>() {
    @Override
    public String call(Tuple2<String, Integer> v1) throws Exception {
        return v1._1;
    }
}).foreach(s->System.out.println(s));


//使用distinct算子去重,对源RDD去重后返回一个新的RDD
JavaRDD<String> distinct = rdd.distinct();
distinct.foreach(s->System.out.println(s));
sc.stop();

Scala代码:

Scala 复制代码
val conf = new SparkConf().setMaster("local").setAppName("DistinctTest")
val sc = new SparkContext(conf)

val rdd: RDD[String] = sc.parallelize(Array("a","b","c","a","b","c"))
//手动去重,map+reduceByKey+map
rdd.map((_,1))
 .reduceByKey(_+_)
 .map(t=>t._1)
 .foreach(println)

//使用distinct去重
rdd.distinct().foreach(println)

sc.stop()

二、mapValues

该函数对K,V格式RDD中的Value按照传入的函数做转换,返回是K,V格式的RDD,Value为新转换后的Value。

Java代码:

java 复制代码
SparkConf conf = new SparkConf().setMaster("local").setAppName("MapValuesTest");
JavaSparkContext sc = new JavaSparkContext(conf);

JavaPairRDD<String, Integer> pairRDD = sc.parallelizePairs(Arrays.asList(
        new Tuple2<String, Integer>("a", 1),
        new Tuple2<String, Integer>("b", 2),
        new Tuple2<String, Integer>("c", 3)
));

//mapValues算子,只对value进行操作,key保持不变
JavaPairRDD<String, Integer> result = pairRDD.mapValues(new Function<Integer, Integer>() {
    @Override
    public Integer call(Integer integer) throws Exception {
        return integer * 2;
    }
});

result.foreach(tp->System.out.println(tp));

sc.stop();

Scala代码:

Scala 复制代码
val conf = new SparkConf().setMaster("local").setAppName("MapValuesTest")
val sc = new SparkContext(conf)

val rdd: RDD[(String, Int)] = sc.parallelize(List(("a", 1), ("b", 2), ("c", 3)))
//mapValues:对rdd中value进行操作,key不变
rdd.mapValues(_ * 10)
  .foreach(println)

sc.stop()

此外,还有个flatMapValues算子,其与mapValues类似,只是mapValues一对一返回数据,而flatMapValues一对多返回数据。


  • 📢博客主页:https://lansonli.blog.csdn.net
  • 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
  • 📢本文由 Lansonli 原创,首发于 CSDN博客🙉
  • 📢停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更美好的生活✨
相关推荐
武子康10 小时前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天11 小时前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
武子康2 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
肌肉娃子3 天前
20260227.spark.Spark 性能刺客:千万别在 for 循环里写 withColumn
spark
初次攀爬者3 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
武子康4 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
够快云库4 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
AI周红伟4 天前
周红伟:智能体全栈构建实操:OpenClaw部署+Agent Skills+Seedance+RAG从入门到实战
大数据·人工智能·大模型·智能体
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js高考推荐系统 高考可视化 大数据毕业设计(源码+LW文档+PPT+详细讲解)
大数据·vue.js·hadoop·django·毕业设计·课程设计·推荐算法