代码
/*
源文件内容
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