MapReduce之么半群

2021-10-07 13:18:12 字數 3310 閱讀 5995

首先先簡單介紹一些概念

么元么元即單位元,是集合裡的一種特別的元,與該集合裡的運算有關。當它和其他元素結合時,並不會改變那些元素。

么半群么半群是乙個存在么元的半群

么半群具有以下性質:

因為在之前的mapeduce學習過程中並沒有怎麼使用mapeduce的組合器,因此,在這次學習的過程中通過將不具有結合性的mapreduce運算轉化為可結合的運算,對於充分利用組合起和提公升mapreduce作業的效能具有重要作用。

么半群和非么半群示例

整數集最大值

集合s=上的求最大值運算定義了乙個滿**換律的么半群,其單位元為0:

max(a,max(b,c))=max(max(a,b),c)

max(a,0)=max(0,a)=a

max(a,b) ∈

\in∈ s

整數集減法

整數集上的減法運算不能構成乙個么半群,因為不滿足結合律:

(1-2)-3 ≠

\ne​

= 1-(2-3)

(1-2)-3=-4

1-(2-3)=2

同理,整數集的加法和乘法可以構成乙個可交換的么半群,整數集均值和中位數並不能滿足么半群。

在這篇部落格中,以計算均值為例,通過使用組合起與么半群來提公升計算效能。

整數集的均值

整數集的計算過程如下

mean(1,2,3,4,5)≠

\ne​

=mean(mean(1,2,3),mean(4,5))

mean(1,2,3,4,5)=1+2

+3+4

+55\frac

51+2+3

+4+5

​=3mean(mean(1,2,3),mean(4,5))=mean(2,4.5)=2

+4.5

2\frac

22+4.5

​=3.25

這個結果當然有問題啊,所以可以這樣修改

mean(mean(1,2,3),mean(4,5))=mean(mean(6,3),mean(9,2)=mean(15,5)=3

其中mean(6,3)表示 1+2+3=6,總共3個數字

樣例資料

key1,1

key1,2

key1,3

key1,4

key1,5

key2,2

key2,4

key2,6

key2,8

key2,10

key3,3

key3,6

key3,9

key3,12

key3,15

key4,4

key4,8

key4,12

key4,16

key4,20

key5,5

key5,10

key5,15

key5,20

key5,25

通過mapreduce,來實現對不同的key求其均值

該階段主要是將不同的key及其對應的值提取出來,並記錄每個不同的key出現的次數

private final static pairoflongint reducev=new pairoflongint();

private final static text reducek=new text();

@override

public void map(longwritable key,text value,context context) catch (interruptedexception e) catch (ioexception e)

}} 其中pairoflongint設計如下

public class pairoflongint implements writablecomparable

public pairoflongint(long left,int right)

public void set(long left,int right)

public long getleft()

public void setleft(long left)

public int getright()

public void setright(int right)

public int compareto(pairoflongint o)

return 0;

}if(leftcombiner階段任務

combiner階段編碼

public class meanmonoizedcombiner extends reducer

try catch (interruptedexception e) catch (ioexception e) }}

reducer階段任務

該階段求最終的結果

reducer階段編碼

public class meanmonoizedreducer extends reducer

try catch (interruptedexception e) catch (ioexception e) }}

驅動程式如下
public class driver ;

job job=new job(conf,"meanmonoized");

job.setjarbyclass(driver.class);

job.setcombinerclass(meanmonoizedcombiner.class);

job.setreducerclass(meanmonoizedreducer.class);

job.setmapoutputkeyclass(text.class);

job.setmapoutputvalueclass(pairoflongint.class);

job.setoutputkeyclass(text.class);

job.setoutputvalueclass(doublewritable.class);

fileinputformat.addinputpath(job,new path(otherargs[0]));

fileoutputformat.setoutputpath(job,new path(otherargs[1]));

system.exit(job.waitforcompletion(true)?0:1);}}

最終結果如下

抽象代數基本概念(四) 半群 么半群的同構與同態

設 s 和 t 是兩個半群。如果存在乙個從s 到t 的雙射 使得 a,b s forall a,b in s a,b s 有 a b a b 則稱半群 s 與 t 同構。記為 s cong t 簡記為s cong t。稱為從s 到t 的乙個同構 對映 設 m e 和 m e 是兩個么半群。如果存在乙...

抽象代數基本概念(三) 子半群 子么半群與理想

設 s 是乙個半群,b 是s 的乙個非空子集。如果對於 a,b b forall a,b in b a,b b 都有a b in b,則稱代數系 b 是 s 的乙個子半群。簡稱b 是s 的乙個子半群。b 的乘法與 s 的乘法是一樣的,否則,即使b 是s 的子集,b 也不是 s 的乙個子半群。設 s ...

代數結構筆記 半群,群,環與域

初版日期 2020 6 27 最後更新日期 2020 7 17 更新次數 2 介紹群,環,域等命題之間的關係.首先要明白二元運算子在集合上有么元,零元的定義,它們都是代數常量.二元運算子在集合上可以有封閉性,結合律,交換律,冪等律,吸收律,消去律等特性 圖一 群與環之間的關係 有限群的階數指的是集合...