//统计各个单词出现的次数
val words = List("spark","hive","hadoop","hbase","zookeeper","hive","hive",
"hive","hbase","hbase","spark","hive","spark","hive")
val wordsplit:List[(String,Int)] = words.flatMap(w=>w.split(",")
.map((_,1)))
val wordcount= wordsplit.groupBy(identity).mapValues(_.size)
//结果
Map((hive,1) -> 6, (zookeeper,1) -> 1, (hbase,1) -> 3, (spark,1) -> 3, (hadoop,1) -> 1)
val wordcount1= wordsplit.groupBy(x=>x).mapValues(_.size)
//结果
Map((hive,1) -> 6, (zookeeper,1) -> 1, (hbase,1) -> 3, (spark,1) -> 3, (hadoop,1) -> 1)
identity 函数的作用就是将传入的参数“直接”当做返回值回传给调用者,这在正常使用中,可以说是毫无作用,但他在groupBy和sortBy等函数中的作用,在于避免程序员书写相同且容易出错的逻辑