D25 Scala基础

spark的start-all.sh启动命令 与hadoop的start-all.sh冲突,现在重命名。勿忘!

①spark来进行wordcount
sc.textFile("/home/hadoop/words.txt").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).collect
②spark来进行wordcount后排序
sc.textFile("/home/hadoop/words.txt").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).sortBy(_._2, false).collect
③spark来进行wordcount后排序后保存。输出保存的路径必须是不存在的文件夹下。
sc.textFile("/home/hadoop/words.txt").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).sortBy(_._2, false).saveAsTextFile("/home/hadoop/out")
open perspective设定编辑和运行模式为Scala,这样窗口就是Scala的变成窗口。
简单的Scala编程基础
   
   
  1. package test
  2. object outPut {
  3. def main(args:Array[String]){
  4. //①输出函数
  5. println("hehe")
  6. //②使用val定义的变量值是不可变的,相当于java里用final修饰的变量
  7. val i =1
  8. //③使用var定义的变量是可变得,在Scala中鼓励使用val
  9. var s ="hello"
  10. //Scala编译器会自动推断变量的类型,必要的时候可以指定类型
  11. //变量名在前,类型在后
  12. val str:String="itcast"
  13. //④for循环遍历
  14. for(i <-1 to 10)
  15. println(i)
  16. //⑤支持混合类型表达式
  17. val z =if(i >1)1else"error"
  18. //打印z的值
  19. println(z)
  20. //⑥块编程
  21. val result ={
  22. if(i <0){
  23. -1
  24. }elseif(i >=1){
  25. 1
  26. }else{
  27. "error"
  28. }
  29. }
  30. //⑦for循环遍历字符串数组
  31. val arr =Array("a","b","c")
  32. for(i <- arr)
  33. println(i)
  34. //⑧高级for循环
  35. //每个生成器都可以带一个条件,注意:if前面没有分号
  36. for(i <-1 to 3; j <-1 to 3if i != j)
  37. print((10* i + j)+" ")
  38. //⑨构建集合
  39. val x =for(i <-1 to 10) yield i *2//构建集合val
  40. println(x)
  41. }
  42. }
方法与函数区别:
函数的层次更小,可以作为参数传入到方法内。方法的层次更高些。

   
   
  1. //定义一个普通的方法
  2. def m2 (x:Int, y:Int) : Int= x + y
  3. …………………………………………………………………………
  4. //定义一个方法,方法m2参数要求是一个函数,函数的参数必须是两个Int类型,返回值类型也是Int类型
  5. def m1(f:(Int,Int)=>Int) :Int={ f(2,6) }
  6. //定义一个函数f1,参数是两个Int类型,返回值是一个Int类型
  7. val f1 =(x:Int, y:Int)=> x + y
  8. //再定义一个函数f2
  9. val f2 =(m:Int, n:Int)=> m * n
  10. def main(args:Array[String]){
  11. //调用m1方法,并传入f1函数
  12. val r1 = m1(f1)
  13. println(r1)
  14. //调用m1方法,并传入f2函数
  15. val r2 = m1(f2)
  16. println(r2)
  17. }
定义方法:
   
定义函数:
 关于数组的操作
   
   
  1. val arr1 =ArrayBuffer[Int]()
  2. println(arr1)
  3. //追加整个数组用++=
  4. arr1 ++=Array(2,3)
  5. println(arr1)
  6. //追加若干元素用+=
  7. arr1 +=(2,3)
  8. println(arr1)
  9. //追加数组缓冲
  10. arr1++=ArrayBuffer(2,3)
  11. println(arr1)
  12. //在位置0处添加元素1
  13. arr1.insert(0,1)
  14. println(arr1)
  15. //删除位置0处的元素
  16. arr1.remove(0)
  17. println(arr1)
    
    
  1. //Map
  2. val myMap =Map(("hehe",1),("nima",2),("shabi",3))
  3. println(myMap.get("hehe"))
  4. //元组
  5. val t,(a, b, c, d)=("hadoop","spark",3.14,19941101)
  6. println(t._1)
  7. println(t._2)
  8. println(t._3)
  9. println(t._4)
  10. //zip将两个数组关联的操作
  11. val source =Array(20,30,40,50)
  12. val name =Array("ni","wo","ta","JJ")
  13. println(name.zip(source).toBuffer)
  14. //构建一个可变列表,初始有3个元素1,2,3
  15. val lst0 =ListBuffer[Int](1,2,3)
  16. //创建一个空的可变列表
  17. val lst1 =newListBuffer[Int]
  18. //向lst1中追加元素,注意:没有生成新的集合
  19. lst1 +=4
  20. lst1.append(5)
  21. //将lst1中的元素最近到lst0中, 注意:没有生成新的集合
  22. lst0 ++= lst1
  23. //将lst0和lst1合并成一个新的ListBuffer 注意:生成了一个集合
  24. val lst2= lst0 ++ lst1
  25. //将元素追加到lst0的后面生成一个新的集合
  26. val lst3 = lst0 :+5
关于对象
    
    
  1. classPerson(val name:String, val age:Int){
  2. println("执行主构造器")
  3. private var gender ="male"
  4. //用this关键字定义辅助构造器
  5. def this(name:String, age:Int, gender:String){
  6. //每个辅助构造器必须以主构造器或其他的辅助构造器的调用开始
  7. this(name, age)
  8. println("执行辅助构造器")
  9. this.gender = gender
  10. }
  11. def description()= name +" is "+ age +" years old "
  12. }
  13. def main(args:Array[String]){
  14. val p1 =newPerson("曾祥雨",22)
  15. println(p1.description())
  16. }

单机版WordCount
   
   
  1. object outPut {
  2. def main(args:Array[String]){
  3. val words =List("hello tom hello jerry","hello tom kitty hello hello")
  4. println(words)
  5. val lines = words.map(_.split(" ")).flatten
  6. println(lines)//List(hello, tom, hello, jerry, hello, tom, kitty, hello, hello)
  7. val word = lines.map((_,1))
  8. println(word)//List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (tom,1), (kitty,1), (hello,1), (hello,1))
  9. val group=word.groupBy(_._1)
  10. println(group)//Map(tom -> List((tom,1), (tom,1)), kitty -> List((kitty,1)), jerry -> List((jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1), (hello,1)))
  11. val num =group.map(_._1)
  12. println(num)//List(tom, kitty, jerry, hello)
  13. val res =group.map(t =>(t._1, t._2.size))
  14. println(res)//输出Map(tom -> 2, kitty -> 1, jerry -> 1, hello -> 5)
  15. val finalRes=res.toList.sortBy(_._2)//默认增序
  16. println(finalRes)//List((kitty,1), (jerry,1), (tom,2), (hello,5))
  17. }
  18. }
    
    
  1. 以上等价于下面几行代码
  2. val lines =List("hello tom hello jerry","hello jerry","hello kitty")
  3. println(lines)
  4. val res = lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_ + _._2))
  5. println(res)
  6. val finalRes = lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t =>(t._1, t._2.size)).toList.sortBy(_._2).reverse
  7.  
  8. println(finalRes)

常用的转换操作:
   
   
  1. 最常用的转换操作有两个:mapfiltermap(func)是将func应用到所有元素,得到一个新的RDD
  2. filter是将func返回为true的元素过滤出来,组成一个新的RDD
  3. 一些比较常用的转换如下:
  4. map(func)  返回一个新的分布式数据集,将数据源的每一个元素传递给函数 func 映射组成。
  5. filter(func)    返回一个新的数据集,从数据源中选中一些元素通过函数 func 返回true
  6. flatMap(func)  类似于 map,但是每个输入项能被映射成多个输出项(所以 func 必须返回一个Seq,而不是单个 item)。
  7. union(otherDataset)    两个RDD求并集
  8. intersection(otherDataset)  两个RDD求交集
  9. groupByKey()  作用于(K,V)的数据集,依据K对值进行归并,返回一个(K,Iterable)
  10. reduceByKey(func)作用于(K,V)的数据集,依据K对值使用func进行归约,返回一个(K,V)数据集
  11. sortByKey([asending])   返回一个依据K进行排序的数据集
  12. 最常用的动作就是reduce,将数据集归约为一个结果。
  13. 一些比较常用的动作如下:
  14. reduce(func)  按照func函数对数据集进行归约,func接受两个参数,返回一个结果,须满足结合律和交换律,以便于分布式计算。
  15. count()返回数据集的元素个数
  16. first()返回第一个元素
  17. take(n)  以数组形式返回集合的前n个元素
  18. saveAsTextFile(path)将数据集保存为文本文件
    
    
  1. val textFile = sc.textFile("hdfs://...")//读取hdfs文件,转换为以行为单位的文本集合
  2. val counts = textFile.flatMap(line => line.split(" "))//转换,将行字符串转换为单词,组成新的RDD
  3. .map(word =>(word,1))//转换,将单词转换为词频统计
  4. .reduceByKey(_ + _)//转换,根据key值进行归约
  5. counts.saveAsTextFile("hdfs://...")//保存

目标1:(初级)熟练使用scala编写Spark程序

 

 

目标2:(中级)动手编写一个简易Spark通信框架


 


目标3:(高级)为阅读Spark内核源码做准备


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值