spark:学习杂记+调用自行编译的函数--27

来源:互联网 发布:为什么会迷恋网络 编辑:程序博客网 时间:2024/05/18 05:39

1.映射

   构造映射:

  val scores = Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8)

   或者从空的映射开始:

  val scores = new scala.collection.mutable.HashMap[String, Int]

   获取映射的值:

  val bobsScore = scores.getOrElse("Bob", 0)//如果映射包含键“Bob”,返回对应的值,否则返回0
   更新映射的值:
  scores("Bob") = 10  或  scores += ("Bob" -> 10, "Fred" -> 7)
   移除某个键和对应的值:
  scores -= "Alice"
   更新过的新映射:
  val newScores = scores + ("Bob" -> 10, "Fred" -> 7)
   迭代映射:
  scores.keyset //一个类似set.(集合for (v <- scores.values) println(v))
   反转一个映射,即交换键和值的位置:
  for ((k, v) <- 映射) yield(v, k)
   在scala中没有可变的树形映射


2.元组
   (tuple):映射是键/值对偶的集合,对偶是元组的最简单形态

  val t = (1, 3.14, "Fred")  val Second = t._2   //Second = 3.14  val (first, second, third) = t // first = 1,second = 3.14, third = Fred
   拉链操作:
  val symbols = Array("<", "-", ">")  val counts = Array(2, 10, 2)  val pairs = symbols.zip(counts)  res : Array(("<", 2), ("-", 10), (">", 2))
&在scala中,import语句可以出现在任何地方,并不仅限于文件顶部,import语句的效果一直延伸到包含该语句的块末尾

3.重写方法(override)

   在scala中重写一个非抽象方法必须使用override修饰符

   重写字段:

class Person(val name: String){override def toString = getclass.getName + "[name =" + name + "]"}class SecretAgent(codename: String) extends Person(codename){override val name = "secret"override val toString = "Secret"}
&def 只能重写一个def,val只能重写一个val或不带参数的del,var只能重写另一个var

   抽象字段:

abstract class Person{val id: Int //带有抽象的getter方法的抽象字段var name: String //带有抽象的getter和setter的方法}
   读取行:
import scala.io.Sourceval Source = Source.fromFile("myFile.txt", "UTF-8")val lineITerator = Source.getlines.toArrayval comtents = Source.mkstring

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

自己写一个快速排序方法,在spark程序中调用

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package lllimport org.apache.log4j.{Level, Logger}import org.apache.spark.{SparkContext, SparkConf}import scala.collection.mutable.ListBuffer/** * Created by sendoh on 2015/4/18. */object sorta {  def main(args: Array[String]): Unit = {    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)    Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)    if (args.length != 3) {      println("Usage: java -jar code.jar dependency_jars file_locaion save_location")      System.exit(0)    }    val jars = ListBuffer[String]()    args(0).split(',').map(jars += _)    //    val conf = new SparkConf().setAppName("sorta").setMaster("local[2]").setSparkHome("/usr/local/spark-1.2.0-bin-hadoop2.4.0").setJars(jars)    val sc = new SparkContext(conf)    //    //val textsort = sort()    val data = sc.textFile("hdfs://localhost:9000/datatnt/textsorta.txt")    val result = data.flatMap(_.split(" ")).collect//.union(sort).saveAsObjectFile("hdfs://localhost:9000/outputtnt/textsorta1")    val res = sort(result)    println(res)  }  def sort(xs: Array[String]): Unit ={    def swap(i: Int, j: Int): Unit ={      val t = xs(i); xs(i) = xs(j); xs(j) = t    }    def sort1(l: Int, r: Int): Unit ={      val pivot = xs((l + r) / 2)      var i = l; var j = r      while (i <= j){        while (xs(i) < pivot)          i += l        while (xs(j) > pivot)          j -= l        if (i <= j){          swap(i, j)          i += l          j -= l        }      }      if (l < j)        sort1(l, j)      if (j < r)        sort1(i, r)    }      //sort1(0, xs.length - 1)    }  //}}


0 0