设为首页 加入收藏

TOP

Scala   WordCount
2019-05-15 01:13:28 】 浏览:112
Tags: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   WordCount https://www.cppentry.com/bencandy.php?fid=116&id=224174

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇        .. 下一篇        ..