Scala WordCount

1.最基本的WordCount

  

 var arr3 = Array("HIVE SPARK", "scala SPARK HBASE", "ZOOKEEPER","HIVE SPARK", "scala SPARK HBASE", "ZOOKEEPER")
    var arr4=arr3.flatMap(x => x.split(" "))
    var arr5=arr4.map(x=>(x,1))
    var arr6=arr5.groupBy(x=>(x._1))
    val arr7=arr6.map(x=>(x._1,x._2.length))
    print(arr7)

结果:

2.降序排序求top3

var arr1 = Array("HIVE SPARK", "scala SPARK HBASE", "ZOOKEEPER", "HIVE SPARK", "scala SPARK HBASE", "ZOOKEEPER SPARK HIVE HIVE", "ZOOKEEPER")
    var arr2=arr1.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(x=>(x._1,x._2.length)).toList.sortBy(-_._2).take(3)
    println(arr2.mkString)

结果:

 

你可能感兴趣的:(Scala,HaDoop学习历程)