Scala分步完成wordcount例子
2017-08-20 00:00
253 查看
package day0811
import scala.annotation.varargs
object liujingwordcount {
def main(args: Array[String]): Unit = {
val arr=Array("hello word ketty","hello jeryy","hello tomcat","tomcat good")
//[hello word ketty, hello jeryy, hello tomcat, tomcat good]
val arr0=arr.flatMap { x => x.split(" ") }
//[(hello,1), (word,1), (ketty,1), (hello,1), (jeryy,1), (hello,1), (tomcat,1), (tomcat,1), (good,1)]
val arr1=arr0.map { x => (x,1) }
// Map(good -> [Lscala.Tuple2;@1d318953, jeryy -> [Lscala.Tuple2;@1f2eb71e, ketty -> [Lscala.Tuple2;@33f82290, tomcat -> [Lscala.Tuple2;@67a53697, hello -> [Lscala.Tuple2;@fa328aa, word -> [Lscala.Tuple2;@20c2b8eb)
val arr2=arr1.groupBy(t=>t._1)
//Map(good -> 1, jeryy -> 1, ketty -> 1, tomcat -> 2, hello -> 3, word -> 1)
val arr3=arr2.map(t=>(t._1,t._2.length))
//List((gooa,1), (jeryy,1), (ketty,1), (word,1), (tomcat,2), (hello,3))
val arr4=arr3.toList.sortBy(t=>t._2)
for(i <- 0 to arr4.length-1){
println(arr4(i))
}
}
}
import scala.annotation.varargs
object liujingwordcount {
def main(args: Array[String]): Unit = {
val arr=Array("hello word ketty","hello jeryy","hello tomcat","tomcat good")
//[hello word ketty, hello jeryy, hello tomcat, tomcat good]
val arr0=arr.flatMap { x => x.split(" ") }
//[(hello,1), (word,1), (ketty,1), (hello,1), (jeryy,1), (hello,1), (tomcat,1), (tomcat,1), (good,1)]
val arr1=arr0.map { x => (x,1) }
// Map(good -> [Lscala.Tuple2;@1d318953, jeryy -> [Lscala.Tuple2;@1f2eb71e, ketty -> [Lscala.Tuple2;@33f82290, tomcat -> [Lscala.Tuple2;@67a53697, hello -> [Lscala.Tuple2;@fa328aa, word -> [Lscala.Tuple2;@20c2b8eb)
val arr2=arr1.groupBy(t=>t._1)
//Map(good -> 1, jeryy -> 1, ketty -> 1, tomcat -> 2, hello -> 3, word -> 1)
val arr3=arr2.map(t=>(t._1,t._2.length))
//List((gooa,1), (jeryy,1), (ketty,1), (word,1), (tomcat,2), (hello,3))
val arr4=arr3.toList.sortBy(t=>t._2)
for(i <- 0 to arr4.length-1){
println(arr4(i))
}
}
}
相关文章推荐
- 分别用Java、Scala、spark-shell开发wordcount程序及测试代码
- Scala中使用两种方式对单词进行次数统计(wordCount)
- storm-wordcount例子
- [Spark][Python]Wordcount 例子
- scala版本的wordCount
- hadoop基础----hadoop实战(五)-----myeclipse开发MapReduce---WordCount例子---解析MapReduce的写法
- scala编写wordCount
- hadoop 2.7.4 下运行WordCount例子笔记
- hadoop2.4配置与wordcount例子
- 任务类MapReduce例子1--wordcount
- scala-eclipse 编写spark简单程序 WordCount
- Flume+Kakfa+Spark Streaming整合(运行WordCount小例子)
- hadoop基础----hadoop实战(三)-----hadoop运行MapReduce---对单词进行统计--经典的自带例子wordcount
- WordCount的Scala实现
- Spark:用Java和Scala实现WordCount
- [虚拟机VM][Ubuntu12.04]运行WordCount例子
- java实现kafka整合spark streaming完成wordCount,updateStateByKey完成实时状态更新
- 1.3.0MapReduce运行自带的wordcount例子
- Spark实战----(1)使用Scala开发本地测试的Spark WordCount程序
- ubuntu下wordcount例子