当前位置:网站首页>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不能修改
边栏推荐
- 【问题解决】虚拟机配置静态ip
- Attack and defense enlightenment: chromium component risk analysis and convergence
- 为什么要用lock 【readonly】object?为什么不要lock(this)?
- 35岁危机?内卷成程序员代名词了
- Let's talk about BOM and DOM (5): dom of all large Rovers and the pits in BOM compatibility
- 基于三维GIS系统的智慧水库管理应用
- 智能视觉组A4纸识别样例
- Microsoft Security, which frequently swipes the network security circle, gives us some enlightenment this time?
- leetcode:84. The largest rectangle in the histogram
- C语言学生管理系统——可检查用户输入合法性,双向带头循环链表
猜你喜欢

Rockscache schematic diagram of cache operation

成为 TD Hero,做用技术改变世界的超级英雄 | 来自 TDengine 社区的邀请函

RealNetworks vs. Microsoft: the battle in the early streaming media industry

记录--关于JSP前台传参数到后台出现乱码的问题

数据同步工具 DataX 已经正式支持读写 TDengine

虚拟文件系统

leetcode:84. 柱状图中最大的矩形

你有一个机会,这里有一个舞台

Interpreting top-level design of AI robot industry development

Internet cafe management system and database
随机推荐
How to build an app at low cost
Tencent launched the "reassuring agricultural product plan" to support 100 landmark agricultural product brands!
With a goal of 50million days' living, pwnk wants to build a "Disneyland" for the next generation of young people
How to make a website? What should I pay attention to when making a website?
35岁危机?内卷成程序员代名词了
Le système de surveillance du nuage hertzbeat v1.1.0 a été publié, une commande pour démarrer le voyage de surveillance!
为什么要用lock 【readonly】object?为什么不要lock(this)?
About Stacked Generalization
File system notes
leetcode:1856. Maximum value of minimum product of subarray
文件系统笔记
leetcode:剑指 Offer 26:判断t1中是否含有t2的全部拓扑结构
雲監控系統 HertzBeat v1.1.0 發布,一條命令開啟監控之旅!
【二叉树】——二叉树中序遍历
[binary tree] - middle order traversal of binary tree
如何低成本构建一个APP
On BOM and DOM (2): DOM node hierarchy / attributes / Selectors / node relationships / detailed operation
项目Demo
【二叉数学习】—— 树的介绍
Virtual file system