Scala_day2

package com.song.scala.day02

import scala.collection.mutable

object Test02 {
  def main(args: Array[String]): Unit = {
    val arrs = Array(1, 23, 4, 5)
    val arrmap = arrs.map(x => x)
    println(arrmap.toBuffer)
    arrmap.foreach(println)
    /*
    集合:是一种用来存储各种数据的容器
    scala集合可以分为可变和不可变的集合,不可变集合可以安全的并发方法
    可变集合可以在适当的地方被更新或扩展。这意味着可以修改,添加 移除一个集合的元素
    不可变集合,相比之下,永远不会改变。不过你仍然可以模拟添加,移除或更新操作。但是这些操作将在每一种情况下都返回一个新的集合,同时是原来的集合不会发生改变
     scala集合两个主要的包:
     不可变集合:scala.collention.immutable
     可变集合:scala.collection.mutable,scala默认采用不可变集合
     scala的集合有三大类:序列Seq,集Set,映射Map,所有的集合都扩展自iterable特质

     */


    //数组
    //    数组元素要求类型一致
    //定长数组,数组不可扩容
    //变长数组,数组可扩容
    val array = new Array(4) //长度
    val arr = Array(1, 2, 3)

    //变长数组需引入包
    import scala.collection.mutable.ArrayBuffer
    val muta = ArrayBuffer[Int]()
    muta.append(1)
    muta += 3
    muta += (6, 2)
    muta.insert(34, 45) //第一个为下标
    muta.remove(0, 2) //0开始移除两个

    //yield 将函数的返回值封装其他类型
    val res = for (i <- muta if i % 2 == 0) yield i * 10

    muta.filter(x => x % 2 == 0).map(x => x * 10)

    //paixu
    muta.sortWith((x, y) => x < y)
    muta.sortWith(_ > _)
    muta.sortBy(x => x)

    val arr4 = Array(("a", 3), ("b", 1), ("c", 2))
    val sotedArr = arr4.sortBy(_._2) //按第二个元素排序
    val sortedarr = arr4.sortWith(_._2 > _._2)
    println(sortedarr.toBuffer)
    //映射(Map) (重要)
    //    在scala中,把哈希表这种数据结构叫做映射
    //    scala中的Map存储的内容是键值对(key-value),Scala中不可变的map是有序的,可变的的Map是无序的
    //    Scala,有可变Map(scala,collection,mutable,Map)和不可变Map(scala.collection.immutable.Map)
    //构建映射
    //    在Scala中,有两种Map,一个是Immutable包下的Map,该MAP中的内容不可变,另一个是mutable包下的Map,该Map下的内容可变

    val user = Map(("tim", 4), ("herry", 8), ("shuke", 5))
    val godden = Map(("tim" -> 4), ("herry" -> 8), ("shuke" -> 5))

    //可变的
    import scala.collection.mutable.Map
    val god = Map(("tim" -> 4), ("herry" -> 8), ("shuke" -> 5))
    god += ("beta" -> 4, "tik" -> 9)
    //删除
    god -= ("beta")
    god.remove("tik")
    //遍历
    for ((key, value) <- god) println(key + "---------" + value)
    for (v <- god.values) println("---------" + v)
    for ((key, value) <- god) yield (key, value) //yield 封装前面的值
    new mutable.LinkedHashMap[String, Int]

    //元组
    val tup = ("fdsf", true)
    tup._1 //元组是从1开始的
    //元组的声明的第二种方式
    val tup1 = new Tuple1(3) //1是一种类型

    /*val tup,(a,b,c)=(1,2,3)
    //取值
    a
     */
    val str = tup.toString()
    tup.swap //交换两个数字的位置

    //拉链操作
    val arr1 = Array(53, 56, 78)
    val arr2 = Array(34, 54, 22)
    //    arr1 zip  arr2      //长度较长的直接截掉
    //  (arr1 zip  arr2).map(x=>x._1+x._3)  //将zip后的数据add


    //    List列表:可变和不可变
    //    一个列表要么为空,要么为head元素加tail的列表
    val seq = Seq(1, 2, 3, 4)
    val list = Seq(43, 4, 7)
    //加进去
    8 :: List(3, 4)
    3 :: 5 :: Nil //Nil为空的列表,所以会生成一个列表
    //加新的元素
    seq :+ Array(3, 4, 6)
    //将另一个list加入另一个
    val ints = List(34, 45, 75)
    val lists = list ++ seq

  //可变的
    import  scala.collection.mutable.ListBuffer
    val listb=ListBuffer[Int]()
    listb+=1
    listb+=2
    //声明2
    var listb2 = new ListBuffer[Int]
    listb2.append(34)

    //list加到另一个list2
    list++listb2  //生成一个新的列表
    //后面追加
    listb2:+=5

    //set 无序 不重复  可变不可变

    //不可变
    val set=Set(3,43,5)
    //加数据
    set+56
    val set1=Set(56,78,97)
    set++set1
    set.getClass  //返回什么类型
    //可变的
    val set2 = new mutable.HashSet[Int]()
    set2+=45
    set2+=455
    set2-=45
    set2.remove(455)

    //intersect  diff  union  并交差





  }

  def m1(x: Int): Int = {
    //方法中,会将最后一行代码的值最为返回,但是,如果赋值的过程,是不能作为返回值
    val sum = x * x
    sum

  }


}

懒加载 java and scala

package com.song.scala.day02;

/**
 * 一般是单例模式的懒汉模式
 */
public class LazyDemo {
    private static MyProperty prop;

    public static MyProperty getProp() {
        if (prop == null) {
            prop = new MyProperty();
            System.out.println("getProperty"+prop);
        }
        return prop;
    }

    public static void main(String[] args) {


    }
}

class MyProperty {

}
package com.song.scala.day02

class  ScalazyDemo{

}
object ScalazyDemo1 {

  def init():Unit={
    println("init")
  }

  def main(args: Array[String]): Unit = {
    val  prop=init(); //没有使用lazy修饰符

    println("after init")

    println(prop)

  }
}


object ScalazyDemo2 {

  def init():Unit={
    println("init")
  }

  def main(args: Array[String]): Unit = {
     lazy val  prop=init(); //没有使用lazy修饰符

    println("after init")  //第一步执行

    println(prop)

  }
}

练习

package com.song.scala.day02

object test_2 {
  def main(args: Array[String]): Unit = {
    //创建一个List
    val list1 = List(1, 2, 3, 4, 5, 6, 8)

    //将list1中每个元素乘以二
    val list0 = list1.map(x => x * 2)

    //将list1中偶数取出形成新的集合
    val list2 = list1.filter(x => x % 2 == 0)

    //排序形成新的集合
    val list3 = list1.sorted //默认升序 降序reverse
    val list4 = list1.sortBy(x => x)
    val list5 = list1.sortWith((x, y) => x > y)
    //反转排序
    val list6 = list1.sorted.reverse
    //将list1中的元素4个一组,类型为Iterator
    val iterator: Iterator[List[Int]] = list1.grouped(4)
    //将iterator转换为list
    val list7: List[List[Int]] = iterator.toList
    //将多个list压扁为一个
    val list8=list7.reduce((x,y)=>x:::y)   //::: 表示两个元素集合
    println(list8)
        //--reduce函数
    val flatten = list7.flatten
    println(flatten)

    //每个字符串按空格切分,再压平

    val lines = List("hello tim  hello jerry", "Hello  suke hello")
    val wordsarr = lines.map(x=>x.split(" "))
  //  println(wordsarr.map(_.toBuffer))  //list里有多个arrayBuffer,所以我们直接可以将切分的flatten
    val word = wordsarr.flatten
    println(word)
    //flatmap
    val word2=  lines.flatMap(x=>x.split(" "))  //先map再flat
    println(word2)
      
       //    ---------------------------------------------
    //并行计算求和     //并发:同一时间段
    val arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    /* println(arr.sum)  //数组求和
     println(arr.reduce((x, y) => x + y))
     println(arr.reduce(_ + _))  //reduceleft  单线程操作
    */
    //    按照特定的顺序
    println(arr.reduceLeft(_ + _))
    //多线程累加
    println(arr.par.reduce(_ + _)) //  想减时就结果不同了
    while (true) {
      val i: Int = 1                //根据线程数值也不一样
      println((arr).reduce(_ - _))
    }

    //折叠(fold):有初始值(无特定顺序)
    println(arr.fold(0)(_ + _))   //第一参数 是初始值
    //折叠:有初始值(有特定顺序)
    arr.par.foldLeft(10)(_+_)   //当调用特定排序顺序时,此时多线程不起作用

    //聚合
    val arr1=List(List(2,3,4),List(3,4,5),List(6,7,8))
    println(arr1.reduce((x, y) => List(x.sum + y.sum)))
    //  def aggregate[B](z : => B)(seqop : scala.Function2[B, A, B], combop : scala.Function2[B, B, B]) : B = { /* compiled code */ }
    /*
    第一参数代表初始值
    第二个参数列表的第一个参数,局部聚合(线程内聚合)
    第三个参数列表的第二个参数,全局聚合(线程间聚合)
     */
    val sum: Int = arr1.flatten.sum
     arr1.aggregate(0)(_+_.sum,_+_)  //初始值为0

  //求聚合值,arr sum and   arr nums
    val arr2=Array(1,2,3,45,6,7)
    arr2.aggregate((0,0))  ((x,y)=>(x._1+y,x._2+1),  (m,n)=>(m._1+n._1,m._2+n._2))


  }

}

WordCount

package com.song.scala.day02

object WordCount {
  def main(args: Array[String]): Unit = {
    val lines = List("hello tim  hello jerry", "Hello  suke hello")
    //将数据切分,生成一个个单词

    val word: List[String] = lines.flatMap(x => x.split(" "))

    //将空字符串过滤
    val words = word.filter(!_.isEmpty)
    //将每个单词生成一个元组(word,1)
    val tuples: List[(String, Int)] = words.map((_, 1))

    //将相同的单词进行分组
    val grouped: Map[String, List[(String, Int)]] = tuples.groupBy(_._1)
    //将相同单词的数据进行个数统计
    //  val sumd: Map[String, Int] = grouped.map(x=>(x._1,x._2.size))
    val sumd = grouped.mapValues(_.size) //只操作value key留着呢
    println(sumd)

    //降序排序  map,list是没有排序的方法的
    val sorted: List[(String, Int)] = sumd.toList.sortBy(_._2)
    println(sorted)


  }
}
 //一句代码计数
lines.flatMap(_.split(" ")).filter(!_.isEmpty).map((_,1)).groupBy(_._1).mapValues(_.size).toList.sortBy(_._2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I’m not sure where I first came across the Scala language. Maybe on a fo- rum for programming language enthusiasts such as Lambda the Ultimate, or maybe in more pedestrian quarters: Reddit, or the like. Although I was intrigued at first blush, I owe my deeper exploration and enthusiasm for the language to two individuals: David Pollak, creator of the Lift web frame- work, and Steve Jenson, a former colleague at Twitter and generally brilliant programmer. Following David and Steve, I arrived to Scala in the late-middle stage of the language’s history to date. By 2008, Scala had spent five years evolving from its initial release, and had formed around it a tight-knit community of academics, tinkerers, and even a few consultants. The mailing lists were full of spirited debates, announcements of exciting libraries, and a general camaraderie and shared joy for seeing what this powerful new tool could do. What Scala lacked, at that point, was a collection of success stories around major production deployments. The decision to use Scala at Twitter, where I then worked, was not an easy one to make. Our infrastructure was buckling under the weight of extreme growth. Picking a relative unknown as our language of choice for building the high-performance distributed systems that would keep our fledgling service alive was risky. Still, the benefits that Scala offered were (and are) compelling, and our engineers were quickly able to produce proto- types that proved out the language’s effectiveness. Intheinterveningtime, I’veseenahearteningnumberofcompanieslarge and small adopting Scala. In that time, too, the question of Scala’s complex- ity has been raised. From the outside, Scala’s many features might appear to be a kind of complexity. To understand Scala, though, is to understand its goal of being a scalable language. You can be writing real-world code in Scala in an afternoon. As your understanding of the language and, indeed, of the art and science of programming as a whole expands, there’s more of Scala there to wield to your advantage. That’s not complexity. It’s flexibility. To be clear: Scala will challenge you. That’s part of the joy of using it. Youwon’tunderstandthefullpowerofitstypesystembytheendofyourfirst day. You won’t understand the zen of objects being functions and functions being objects in your first week. Each feature of the language is another light bulb waiting to switch on over your head. I’m certain you’ll enjoy the experience of being gradually illuminated as you read this book and write code. I’ve watched programmers learn Scala on the job and succeed. It can be done, and it can be fun. As Scala programmers like me have grown to better understand what this powerful language can do, so too has Scala evolved to meet program- mers’ needs. Scala 2.8 smoothes out some rough spots in the collection libraries and adds useful features like named and default arguments to meth- ods. While Scala has been a perfectly productive language to work with for some time, as of 2.8 it feels even more solid and polished. The new 2.8 release is icing on the cake. In my experience, Scala was ready for production deployments two years ago. Today, it’s even better, and I can’t imagine building a new system with- out it. Presently, I’m doing just that. For me, Scala has gone from being a risky gamble to a trusted tool in two short years. I look forward to taking advantage of the latest features in Scala 2.8, and to using this book as the definitive reference for it, direct from the creator of the language I’ve grown to depend on. Alex Payne Portland, Oregon October 27, 2010
### 回答1: Spark 基础环境是指安装和配置 Spark 所需的软件和硬件环境。Spark 运行需要 Java 环境和 Hadoop 环境,同时也需要配置 Spark 的相关参数,如内存大小、CPU 核数等。在安装和配置好基础环境后,我们才能使用 Spark 进行数据处理和分析。 ### 回答2: Spark是一个快速、可扩展且容错的大数据处理框架,提供了丰富的API和工具,可以处理大规模的数据集。 搭建Spark基础环境包括以下几个步骤: 1. 安装Java:Spark是基于Java开发的,因此首先需要安装Java开发环境。可以从Oracle官网下载并安装适合操作系统的Java版本。 2. 下载Spark:在Apache Spark官网下载最新版本的Spark压缩包,并解压到指定目录。 3. 配置环境变量:将Spark的bin目录添加到系统的环境变量中。这样可以方便地在任意位置运行Spark的命令。 4. 配置Spark集群:如果需要在多台机器上运行Spark应用程序,需要进行集群配置。首先,在每台机器上安装好Java,并将Spark解压到相同的目录。然后,编辑Spark的配置文件,设置集群的主节点和从节点。 5. 验证安装:通过在终端运行spark-shell命令,验证Spark是否正确安装。spark-shell命令会启动一个Scala解释器,并连接到Spark集群。 6. 运行第一个Spark应用程序:编写一个简单的Spark应用程序,如WordCount,用于统计文本文件中单词的个数。将程序保存为Scala文件,并使用spark-submit命令来运行。 以上就是搭建Spark基础环境的主要步骤。搭建好Spark环境后,可以使用Spark提供的丰富API和工具来进行大数据处理和分析,如数据清洗、转换、机器学习等。Spark的功能强大且易于使用,适用于各种大规模数据处理场景。 ### 回答3: Spark是一个快速通用的集群计算系统,它提供了高效的数据处理和分析能力。要运行Spark,我们需要配置和搭建一些基础环境。 首先,我们需要安装Java JDK。Spark运行在Java虚拟机上,因此我们需要安装适当版本的Java开发工具包。通常建议使用Oracle JDK的最新稳定版本,然后设置JAVA_HOME环境变量。 其次,我们需要安装Spark本身。Spark官方网站提供了预编译的二进制发行版,我们可以从网站上下载并解压缩到我们喜欢的位置。然后,我们可以设置SPARK_HOME环境变量,以便在终端窗口中使用Spark命令。 接下来,我们需要选择一个合适的集群管理器来运行Spark应用程序,比如Standalone模式、Hadoop YARN和Apache Mesos等。我们需要根据自己的需求进行选择和配置。例如,在Standalone模式下,我们需要启动一个Spark Master和多个Spark Worker来管理和运行任务。 最后,在运行Spark应用程序之前,我们需要通过编写一个Spark应用程序来使用Spark的功能。Spark提供了Java、Scala和Python等多种编程语言的API。我们可以使用任何一种编程语言来编写应用程序并在Spark上运行。 总之,Spark基础环境搭建包括安装Java JDK、安装Spark本身、选择和配置集群管理器,以及编写Spark应用程序。搭建好这些基础环境后,我们就可以开始使用Spark进行快速、高效的集群计算了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值