Action算子
package com.shujia.spark.core import org.apache.hadoop.conf.Configuration import org.apache.hadoop.fs.{FileSystem, Path} import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} object Demo11Action { def main(args: Array[String]): Unit = { val conf: SparkConf = new SparkConf() .setAppName("map") .setMaster("local") //spark 上下文对象 val sc = new SparkContext(conf) /** * action算子,触发任务执行,每一个action算子都会触发一个job * */ val students: RDD[String] = sc.textFile("data/students.txt") /** * count; 统计rdd的数量 * */ val count: Long = students.count() /** * collect;将rdd的数据拉取到内存中,变成一个数组 * * 如果rdd的数据量超过了内存限制会出现内存溢出 */ val array: Array[String] = students.collect() /** * reduce; 全局聚合 * */ val str: String = students.reduce(_ + _) println(str) /** * foreach: 遍历rdd中的数据 * foreachPartition: 遍历一个分区, 一般用在将rdd的数据保存到外部数据库的时候 */ students.foreach(println) students.foreachPartition((iter: Iterator[String]) => { iter.foreach(println) }) /** * saveAsTextFile保存数据到hdfs, 如果输出目录已存在会报错 * */ val configuration = new Configuration() val fileSystem: FileSystem = FileSystem.get(configuration) //判断输出目录是否存在 if (fileSystem.exists(new Path("data/ou1"))) { //递归删除目录 fileSystem.delete(new Path("data/ou1"), true) } students.saveAsTextFile("data/ou1") while (true) { } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能