zoukankan      html  css  js  c++  java
  • scala07

     

     

    object Test234 {
      def main(args: Array[String]): Unit = {
       val data:Iterator[String] = Source.fromFile("app.txt").getLines()
       val data1:Iterator[((String,String,String,String),String)] = data.map(t=>{
          val strs = t.split(",")
          ((strs(0),strs(1),strs(2),strs(3)),strs(4))
        })

        val groupData:Map[(String,String,String,String),List[((String,String,String,String),String)]] =data1.toList.groupBy(_._1)

       val maxAndMin:Map[(String,String,String,String),(String,String)] = groupData.mapValues(t=>{
          val versions:List[String] = t.map(_._2)
          (versions.max,versions.min)
        })

        maxAndMin.foreach(println)
        println("*******************************")

        val allVersion:Map[(String,String,String,String),List[(String,String)]]= groupData.mapValues(t=>{
            val versions:List[String] = t.map(_._2).distinct
            if(versions.length>1){

              val sortVersion = versions.sorted
              //v1.0 v1.1  v1.2  v1.4 v1.9
              val tailVersions = sortVersion.tail
              //       v1.1  v1.2  v1.4 v1.9
              val resVersion:List[(String,String)] =sortVersion zip tailVersions
              resVersion
            }else{
              List((versions(0),versions(0)))
            }
          })

        val result: List[((String, String, String, String), (String, String))] = allVersion.toList.flatMap(t=>{
          var info = t._1
          var versions = t._2
          val res:List[((String,String,String,String),(String,String))] = versions.map((info,_))
          res
        })


        result.foreach(println)
      }
    }

    周考题

     

    scala的高级应用

    scala中的大括号的使用

    scala> var arr = Array(1,2,3,4,5)

    arr: Array[Int] = Array(1, 2, 3, 4, 5)

    scala> arr.map(_*10)

    res0: Array[Int] = Array(10, 20, 30, 40, 50)

    scala> arr.map{

         | case x=>x*10

         | }

    res1: Array[Int] = Array(10, 20, 30, 40, 50)

    scala> var arr = Array(("q",1),("a",1),("q",1))

    arr: Array[(String, Int)] = Array((q,1), (a,1), (q,1))

    scala> arr.groupBy{

         | case (a,b)=>a

         | }

    res2: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(q -> Array((q,1), (q,1)), a -> Array((a,1)))

    currying科里化

    科里化是一个转变的过程,把普通的方法专程特殊的方法

    def a(name:String,age:Int)={}    def a(name:String)(age:Int)=

    scala> def getMax(a:Int)(b:Int)={

         | if(a>b)

         | a

         | else

         | b}

    getMax: (a: Int)(b: Int)Int

    scala> getMax(1)(12)

    res6: Int = 12

    scala> def getMax(a:Int,b:Int)(c:Int)={

         | a

         | }

    getMax: (a: Int, b: Int)(c: Int)Int

    scala> getMax(1,2)(3)

    res7: Int = 1

    隐式参数

    scala> def getMax(a:Int)(b:Int=3,c:Int=4)={

         | a}

    getMax: (a: Int)(b: Int, c: Int)Int

    scala> getMax(1)

    <console>:13: error: missing argument list for method getMax

    Unapplied methods are only converted to functions when a function type is expected.

    You can make this conversion explicit by writing `getMax _` or `getMax(_)(_,_)` instead of `getMax`.

           getMax(1)

                 ^

    scala> getMax(2)

    <console>:13: error: missing argument list for method getMax

    Unapplied methods are only converted to functions when a function type is expected.

    You can make this conversion explicit by writing `getMax _` or `getMax(_)(_,_)` instead of `getMax`.

           getMax(2)

                 ^

    scala> def getMax(a:Int)(implicit b:Int=3,c:Int=4)={

         | a

         | }

    getMax: (a: Int)(implicit b: Int, implicit c: Int)Int

    scala> getMax(1)

    res16: Int = 1

    scala> def getMax(a:Int)(implicit b:Int,c:Int=4)={

         | a}

    getMax: (a: Int)(implicit b: Int, implicit c: Int)Int

    scala> getMax(1)

    <console>:13: error: could not find implicit value for parameter b: Int

           getMax(1)

    getMax中如果含有隐式参数,那么必须使用implicit进行修饰,implicit关键字修饰的变量必须给默认值,并且implicit关键字后面的变量都是隐式参数

    object test1111{
      implicit val a = 100000
      implicit val age = 30
      def getMax(b:Int)(implicit c:Int=2)={
        if(b>c)
          b
        else
          c
      }

      def main(args: Array[String]): Unit = {
        println(getMax(1))
      }
    }

    如果在隐式方法上面声明隐式变量,那么这个隐式变量的值默认会给隐式参数的值进行覆盖

    如果两个类型一样的属性会产生混乱

    隐式转换

    默认情况下将一个对象转换为另一个对象

    for(e<- 1.to(100)){}这个to方法是richint中的方法,这个richInt是默认转换的

    to方法下面会加上下划线

     

    scala的交互式命令行中,输入:implicit -v展示所有的scala中的隐式函数或者隐式方法

    intWrapper方法就是默认将int转换为richInt

    imported from scala.preDef

    object test1111{
      implicit val a = 100000
      implicit val age = 30
      def getMax(b:Int)(implicit c:Int=2)={
        if(b>c)
          b
        else
          c
      }

      def main(args: Array[String]): Unit = {
        implicit def xxx(file:File)=Source.fromFile(file)
        val file = new File("aa.txt")
        val dat:Iterator[String]=file.getLines()
        //file   ---- >   source.fromFile()

       implicit def double2Int(d:Double)=d.toInt
        val a:Int = 3.3


        val user2 = new User2("xxx")
        println(user2.getName())

        implicit def user1ToUser2(u:User1):User2={
          new User2(u.name)
        }

        val user1 = new User1("lzl")
        println(user1.getName())
      }
    }

    class User1(val name:String)
    class User2(val name:String){
      def getName(): String ={
        name
      }
    }

    引入隐式转换的时候如果在objectimport object.xxx/object._

    如果在class中  val c = new Class   import c.xxx/c._

    scala中的泛型

    java中的泛型是List<User>  scala中的泛型Array[String]


    object TypeTest {
      def main(args: Array[String]): Unit = {
        val mess = new Message[Int](1)
        val res:String = mess.getMessage()
      }
    }

    class Message[C](msg:C){
      def getMessage():C={
        msg
      }
    }

    scala中的比较器

    comparable comparator

    comparableimplements需要在类上面继承的

    comparator是在使用的时候临时new

    scala中存在两个比较器 ordered ordering

    ordered == comparable  ordering=comparator

    object OrderTest {
      def main(args: Array[String]): Unit = {
          val u1 = new UserVo("lzl",10)
          val u2 = new UserVo("ym",50)
          val u3 = new UserVo("nazha",90)

        println(u3>u1)
      }
    }
    class UserVo extends Ordered[UserVo]{
      def this(name:String,fv:Int){
        this()
        this.name = name
        this.fv = fv
      }
      var name:String = _
      var fv:Int = _
      override def compare(that: UserVo): Int = {
         that.fv- this.fv
      }
    }

    object OrderTest {
      def main(args: Array[String]): Unit = {
          val u1 = new UserVo("lzl",10)
          val u2 = new UserVo("ym",50)
          val u3 = new UserVo("nazha",90)

         val list = new util.ArrayList[UserVo]()
        list.add(u1)
        list.add(u2)
        list.add(u3)
        Collections.sort(list,new Ordering[UserVo] {
          override def compare(x: UserVo, y: UserVo): Int = {
            y.fv-x.fv
          }
        })

        import scala.collection.JavaConversions._
        for(e<-list){
          println(e)
        }
      }
    }
    class UserVo{
      def this(name:String,fv:Int){
        this()
        this.name = name
        this.fv = fv
      }
      var name:String = _
      var fv:Int = _

      override def toString: String = s"name=${name} ,fv=${fv}"
    }

    泛型的上下限

    object typeTest123{
      def main(args: Array[String]): Unit = {
        val i = new Info[A](new A)
        print(i.getInfo())
      }
    }

    class Info[T<:B](x:T){
      def getInfo()={
        x
      }
    }

    class A
    class B extends  A
    class C extends B

    泛型的上下限<:上限  >:下限

    scala中的类型转换

    1. 基础数据类型
    2. 引用数据类型

    基础数据类型间的转换一般使用toType 可以通过to进行基础数据类型间的转换

    引用数据类型的转换一般使用asInstanceOf[Type] 强制转换

    isInstanceOf 判断一下是不是一个数据类型

    a.isInstanceOf[Type] 判断a是不是这个类型的数据

     

    type关键字

    val a:Array[String] = Array("a","v")

    type xxx = Array[String]

    val b:xxx = Array("a","b")

    type MyType = scala.collection.mutable.ArrayBuffer[String]

    val buffer:MyType = ArrayBuffer[String]("a")
    val buffer1:scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer[String]("a")

     

     

    scala知识体系

    scala环境搭建

    scala的基础语法 函数的定义  方法的定义  属性的定义

    val func=(x:int,y:Int)=>{}

    val func:(Int,Int)=>Int=(x,y)=>x+y

    基础数据类型 八种基本数据类型 + Unit

    循环  for  while  yield关键字

     

    集合框架

    元组 () new tupleN

    数组 Array  ArrayBuffer

    集合 List  ListBuffer  ::

    Map 两种可变和不可变的都是一个名字  元素是对偶元组

    set 可变和不可变的几乎一样只不过方法少了几个

    一般使用set都是为了去重

    ++ += -= --+ ++= max  min sum  reverse sorted sortBy sortWith

    insert remove put  

    for(e<-)

    下划线的使用 1.必须使用一次  2.必须和别人一起使用  3.必须立即使用

    集合上面的方法

    map  filter  flatMap  groupBy  sortBy  reduce reduceLeft right

    mkString fold foldLeft foldRight  aggregate foreach  zip  head  tail  

    grouped union diff intersact

    面向对象

    object class abstact trait

    伴生对象 apply  object()

    构造器

    this

    private [this/package]

    extends with  动态混入特质

     

    匹配模式

     类型 守卫 元组  数组  集合  map

    偏函数 def pf:partialFunction[-A,+B]{ case x=>xxxxxx  }

    样例类和样例对象  序列化 不用new  必须含有参数列表 toString equals hashcode

    样例类是单例的

     

    akka编程  并发和通信问题  tcp

    actor actorSystem proxy self sender()  !  scheduler定时器(轮询)

    class myactor extends actor  recerve方法负责接收数据  偏函数

    prestart在执行的时候先执行这个方法

     

    大括号的使用,其实集合中的方法都可以将()换为{},这种模式是匹配模式

    arr.groupBy{case (a,b)=>a}

    arr.groupBy(_._1)

    科里化:将参数分为不同的括号中

    隐式参数,在默认不给值得时候会存在一个默认值

    implicit后面得所有参数都是隐式参数

    隐式转换 implicit修饰得方法就是隐式方法,默认会被调用,将一个类型得数据转换为另一个类型得数据

    数据类型得转换 to转换得是基础数据类型  asInstanceOf转换为一个引用数据类型

    isInstanceOf判断两个变量是不是一个类型得

    泛型:泛型不会特指某一个指定得类型,只是将所有得类型都统一规划

    上下限  >:  <:

    scala中得比较器 ordered == comparable  ordering == comparator

     

     

    spark得课程体系

    sparkcore:spark得核心

    sparksql底层使用得是sparkcoresql解析为core阶段得任务,进行执行

    spark-streaming定时执行sparkcore阶段得任务

    spark得安装集群模式

    spark任务得提交  spark-submit提交一个jar得任务  spark-shell交互式命令行

    RDD 弹性分布式数据集(scala得本地集合进行分布式存储和计算)

    RDD得特性,产生得原因,使用得方式,创建得方式,rdd上面的方法

    算子:rdd上面得方法

    spark原理,怎么运行得再源码曾进行分析

    DAG有向无环图

    spark得高级特性 依赖关系(宽窄依赖) 缓存  持久化

    checkpoint持久化 累加器  广播变量  spark on yarn

    spark HA集群

     

    sparksql通过spark建立数据仓库进行sql查询分析

    sparkStreaming:定时得执行spark任务保证实时性

    需要一个消息中间件 kafka

    mongodb  redis

  • 相关阅读:
    C语言常用函数
    信号处理函数误用不可重入函数导致的进程死锁情况
    Shell脚本学习
    文件I/O详解
    字体标记的使用
    无序列表标记
    换行标记的使用
    HTML文件的基本结构
    预格式化标记的使用
    特殊标记的使用
  • 原文地址:https://www.cnblogs.com/JBLi/p/11527172.html
Copyright © 2011-2022 走看看