Scala-单词统计案例

代码/*源文件内容1.txthello world hadoop hivesqoop hadoop hello world2.txthello world hadoop hivesqoop hadoop hello worldhello world hadoop hivesqoop hadoop hello world3.txthello world hadoop h...
摘要由CSDN通过智能技术生成

代码


/*
源文件内容
1.txt
hello world hadoop hive
sqoop hadoop hello world
2.txt
hello world hadoop hive
sqoop hadoop hello world
hello world hadoop hive
sqoop hadoop hello world
3.txt
hello world hadoop hive
sqoop hadoop hello world
hello world hadoop hive
sqoop hadoop hello world
hello world hadoop hive
sqoop hadoop hello world

*/
class WordCountActor extends Actor{
  override def act(): Unit = {
    loop{
      react{
        case FilePath(fileNamePath) => {
          //获取到了文件的路径之后,读取文件的内容
          val fileContent: BufferedSource = Source.fromFile(fileNamePath,"UTF-8")
          val arr: mutable.Buffer[String] = fileContent.getLines().toBuffer
          //ArrayBuffer(hello world hadoop hive, sqoop hadoop hello world)
          println("arr---------------------------------")
          println(arr.size)//2
          println(arr)


          val arr2: mutable.Buffer[Array[String]] = arr.map(_.split(" "))
          // map(f:=>) 将集合中的每个元素映射到某个函数上
          //arr2((hello, world, hadoop, hive),(sqoop, hadoop, hello, world))
          println("arr2---------------------------------")
          println(arr2.size)//2
          println(arr2)//2
          for(a<-arr2){
            println(a.size)
            println(a.toBuffer)
          }
          val arr33: mutable.Buffer[String] = arr2.flatten
          //flatten 压平: 效果就是取出集合中的每个元素的子元素组成新的集合,相当于将两层嵌套压平为一层
          //ArrayBuffer(hello, world, hadoop, hive, sqoop, hadoop, hello, world)
          println("arr33---------------------------------")
          println(arr33.size)//8
          println(arr33)//8
          for(a<-arr33){
            println(a)
          }

          //val arr33: mutable.Buffer[String]  = arr.map(_.split(" ")).flatten = arr.flatMap(_.split(" "))
          val arr3: mutable.Buffer[String] = arr.flatMap(_.split(" "))
          //flatMap(f:=>)效果就是将集合中的每个元素映射到某个函数上
          // 取出函数返回集合的元素的子元素组成新的集合返回
          //相当于map() 与 flatten 先后执行
          //ArrayBuffer(hello, world, hadoop, hive, sqoop, hadoop, he
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值