当前位置:网站首页>Spark累加器和廣播變量
Spark累加器和廣播變量
2022-06-24 06:59:00 【Angryshark_128】
累加器
累加器有些類似Redis的計數器,但要比計數器强大,不僅可以用於計數,還可以用來累加求和、累加合並元素等。
假設我們有一個word.txt文本,我們想要統計該文本中單詞“sheep”的行數,我們可以直接讀取文本filter過濾然後計數。
sc.textFile("word.txt").filter(_.contains("sheep")).count()
假設我們想分別統計文本中單詞"sheep""wolf"的行數,如果按照上述方法需要計算兩次
sc.textFile("word.txt").filter(_.contains("sheep")).count()
sc.textFile("word.txt").filter(_.contains("wolf")).count()
如果要分別統計100個單詞的行數,則要計算100次
如果使用累加器,則只需要讀一次即可
val count1=sc.acccumlator(0)
val count2=sc.acccumlator(0)
...
def processLine(line:String):Unit{
if(line.contains("sheep")){
count1+=1
}
if(line.contains("wolf")){
count2+=1
}
...
}
sc.textFile("word.txt").foreach(processLine(_))
不僅Int類型可以累加,Long、Double、Collection也可以累加,還可以進行自定義,而且這個變量可以在Spark的WebUI界面看到。
注意:累加器只能在Driver端定義和讀取,不能在Executor端讀取。
廣播變量
廣播變量允許緩存一個只讀的變量在每臺機器(worker)上面,而不是每個任務(task)保存一份備份。利用廣播變量能够以一種更有效率的方式將一個大數據量輸入集合的副本分配給每個節點。
廣播變量通過兩個方面提高數據共享效率:
(1)集群中每個節點(物理機器)只有一個副本,默認的閉包是每個任務一個副本;
(2)廣播傳輸是通過BT下載模式實現的,也就是P2P下載,在集群多的情况下,可以極大地提高數據傳輸速率。廣播變量修改後,不會反饋到其他節點。
val list=sc.parallize(0 to 10)
val brdList=sc.broadcast(list)
sc.textFile("test.txt").filter(brdList.value.contains(_.toInt)).foreach(println)
使用時,需注意:
(1)適用於小變量分發,對於動則幾十M的變量,每個任務都發送一次既消耗內存,也浪費時間
(2)廣播變量只能在driver端定義,在Executor端讀取,Executor不能修改
边栏推荐
- 浅谈如何运营好小红书账号:利用好长尾词理论
- How do I check the IP address? What is an IP address
- Become TD hero, a superhero who changes the world with Technology | invitation from tdengine community
- 年中了,准备了少量的自动化面试题,欢迎来自测
- 数据同步工具 DataX 已经正式支持读写 TDengine
- Five minute run through 3D map demo
- Database stored procedure begin end
- How long does the domain name filing take and what materials need to be prepared
- Overview of new features in mongodb5.0
- MAUI使用Masa blazor组件库
猜你喜欢

puzzle(019.1)Hook、Gear

setInterval里面的函数不能有括号

缓存操作rockscache原理图

潞晨科技获邀加入NVIDIA初创加速计划

【愚公系列】2022年6月 ASP.NET Core下CellReport报表工具基本介绍和使用

leetcode:85. 最大矩形

FreeRTOS MPU使系统更健壮!

35 year old crisis? It has become a synonym for programmers

Application configuration management, basic principle analysis

35岁危机?内卷成程序员代名词了
随机推荐
华为云低时延技术的九大绝招
What is the main function of cloud disk? How to restore deleted pictures
What are the audio formats? Can the audio format be converted
LuChen technology was invited to join NVIDIA startup acceleration program
Project demo
【JUC系列】Executor框架之CompletionFuture
云监控系统 HertzBeat v1.1.0 发布,一条命令开启监控之旅!
Asp+access web server reports an error CONN.ASP error 80004005
多传感器融合track fusion
setInterval里面的函数不能有括号
【二叉树】——二叉树中序遍历
记录--关于JSP前台传参数到后台出现乱码的问题
RealNetworks vs. 微软:早期流媒体行业之争
Centos7 deploying mysql-5.7
Overview of cloud computing advantages of using cloud computing
Cloud native high availability and Disaster Recovery Series (I): pod break up scheduling
leetcode:1856. Maximum value of minimum product of subarray
How to register the cloud service platform and what are the advantages of cloud server
Domain name purchase method good domain name selection principle
The three-year action plan of the Ministry of industry and information technology has been announced, and the security industry has ushered in major development opportunities!