天天看點

Hadoop --Aggregate 包使用 Streaming

Hadoop --Aggregate 包使用 Streaming 

Hadoop 中有個稱為 Aggregate 的包.它把一些常用的功能包括在裡面了,比如求和,求平均數等.每個功能對應一個函數.隻需要在使用時聲明用哪個即可. 它的使用方式是在 reducer 腳本中直接寫上函數的名稱.

它具體的功能有: DoubleValueSun 一個 double 值序列的和 LongValueMax 一個 long 序列的最大值 LongValueMin 一個 long 序列的最小值 LongValueSum 一個 long 序列的和 StringValueMax 一個字元串序列的字母排序的最大值 StringValueMin 一個字元串序列的字母排序的最小值 UniqValueCount 每個鍵的唯一值的個數 ValueHistogram 求每個值的:個數,最小值,中間值,平均值,最大值,标準方差

執行個體一: ----------------------------------------------------- 求每年的專利個數. attributecount.php

Hadoop --Aggregate 包使用 Streaming

[[email protected] bin]# ./hadoop jar ../contrib/streaming/hadoop-streaming-1.2.0.jar -input ./apat63_99.txt -output ./test -mapper 'php attributecount.php 1' -reducer aggregate -file attributecount.php 

結果: [root@localhost bin]# ./hadoop fs -cat ./test/part-00000 | head -n 5 "GYEAR" 1 1963 45679 1964 47375 1965 62857

可以看出.reducer 裡寫上 aggregate 就相當于告訴 hadoop 去使用這個包.而 PHP 腳本中的 LongValueSum 表示使用該函數去處理資料.

執行個體二 ----------------------------------------------------- 求每年授權專利的國家數.也就是每年授權這些專利所屬國家的唯一個數 uniquecount.php

Hadoop --Aggregate 包使用 Streaming

[[email protected] bin]# ./hadoop jar ../contrib/streaming/hadoop-streaming-1.2.0.jar -input ./apat63_99.txt -output ./testunique -mapper 'php uniquecount.php 1 4' -reducer aggregate -file uniquecount.php 

看結果: [[email protected] bin]# ./hadoop fs -cat ./testunique/part-00000 | head -n 5 "GYEAR" 1 1963 64 1964 58 1965 67

執行個體三 ----------------------------------------------------- 使用 ValueHistogram 算出:唯一值個數,最小個數,中值個烽,最大個數,平均個數,标準方差 valuehistogram.php

Hadoop --Aggregate 包使用 Streaming

[[email protected] bin]# ./hadoop jar ../contrib/streaming/hadoop-streaming-1.2.0.jar -input ./apat63_99.txt -output ./testhistogram -mapper 'php valuehistogram.php 1 4' -reducer aggregate -file valuehistogram.php 

看結果: [[email protected] bin]# ./hadoop fs -cat ./testhistogram/part-00000 | head -n 5 "GYEAR" 1 1 1 1 1.0 0.0 1963 64 1 5 37174 713.734375 4610.076525402627 1964 58 1 7 38410 816.8103448275862 4997.413601595352 1965 67 1 5 50331 938.1641791044776 6104.779230296307

可以看出,這裡算出的唯一個數和前面的 UniqValueCount 算出的值一樣.

解讀上面的結果: 第一列為年份;第二列為取得專利的國家數;第三列為最小值;第四列為中值,第五列為最大值. 1963 年,接收專利最少的國家是接收了 1 個.接收專利最多的國家接收了 37174 個.按專利數排序,排在最中間的國家的專利數是 7.也就是說有一半的國家專利數低于 7 個. 一個國家接收專利的平均數在 64 年是 816.8 标準方差是 4997.4, 而中值: 7 和平均值 816.8 相差太多,說明分布是極度不均勻的.

繼續閱讀