spark:总结篇--43

我先吹会儿牛···最近又重新看了一遍《Spark大数据处理:技术、应用与性能优化》的spark框架和运算模型这两章,随着时间的推移每一次都能看到比之前更深一点的东西,我想这就是收获吧···

《Spark快速数据处理》这是我看的第一本关于spark的书,启蒙教材

《Spark大数据处理技术》这本书图书馆一直显示在订购中···

《Apache Spark源码剖析》估计我是当当网这本书的前几个买家,不过试着看了一点,还没达到能看懂源码的水平,我认为这本书关于运算模型和spark的框架讲的相当详细

《大数据Spark企业级实战》因为在出书前我就一直跟着看作者的博客···刚刚借到···讲的好详细,但不适合初学···

《快学scala》一直在看

《scala编程》很粗略的看了一下

《Scala程序设计:Java虚拟机多核编程实战》不推荐

《Scala By Example》

学校图书馆有关spark的书都在这了

《小象学院的spark视频》+《炼数成金的spark视频》

这半年学的东西···不过感觉自己水平还是太低···

、、、、、、、、、、、、、、、、、、、、、、、、、、、

ok吹牛感觉真爽···我之前做过一个很简单的小程序关于通过篮球运动员的球衣号码对运动员排序:

sendoh 7 M 1990 computer
yingmhd 10 M 1989 sofe
akria 11 M 1990 net
sjs 14 M 1991 computer
cz 3 W 1992 net
qz 6 W 1993 computer
xc 1 M 1995 computer

我想通过这个例子弄懂sortByKey的用法

package llf

import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.SparkContext._
import scala.collection.mutable.ListBuffer
import scala.util.Random

/**
 * Created by sendoh on 2015/5/14.
 */
object paixuna {
  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_location save_location")
      System.exit(0)
    }
    val jars = ListBuffer[String]()
    args(0).split(',').map(jars += _)
    val conf = new SparkConf().setAppName("paixuna").setMaster("local[2]").setSparkHome("/usr/local/spark-1.2.0-bin-hadoop2.4").setJars(jars)
    val sc = new SparkContext(conf)
    //
    //val random = new Random()
    val data = sc.textFile("hdfs://localhost:9000/datatnt/textPi.txt")
    data.cache()
    //println(data.count)
    val pdata = data.flatMap(_.split("\t")).map(_.split(' ')(1)).map(x => (x, 1))
      .sortByKey(false).saveAsTextFile("hdfs://localhost:9000/outputtnt/outPi")
  //先对数据按行切割,在对每一行数据按空格切割,取出每行第二个数据,标记为Key,排序,保存结果···
 }

}


///

《快学scala》第十四章课后习题:

1.JDK发行包有一个src.zip文件包含了JDK的大多数源代码。解压并搜索样例标签(用正则表达式case [^:]+:)。然后查找以//开头并包含[Ff]alls?thr的注释,捕获类似// Falls through或// just fall thru这样的注释。假定JDK的程序员们遵守Java编码习惯,在该写注释的地方写下了这些注释,有多少百分比的样例是会掉入到下一个分支的?

2.利用模式匹配,编写一个swap函数,接受一个整数的对偶,返回对偶的两个组成部件互换位置的新对偶

3.利用模式匹配,编写一个swap函数,交换数组中的前两个元素的位置,前提条件是数组长度至少为2

4.添加一个样例类Multiple,作为Item的子类。举例来说,Multiple(10,Article("Blackwell Toster",29.95))描述的是10个烤面包机。当然了,你应该可以在第二个参数的位置接受任何Item,无论是Bundle还是另一个Multiple。扩展price函数以应对新的样例。

5.我们可以用列表制作只在叶子节点存放值的树。举例来说,列表((3 8) 2 (5))描述的是如下这样一棵树:

 *
    / | \
   *  2  *
 /  \    |
3   8    5

6.制作这样的树更好的做法是使用样例类。我们不妨从二叉树开始。

sealed abstract class BinaryTree
case class Leaf(value : Int) extends BinaryTree
case class Node(left : BinaryTree,right : BinaryTree) extends BinaryTree

7.扩展前一个练习中的树,使得每个节点可以有任意多的后代,并重新实现leafSum函数。第五题中的树应该能够通过下述代码表示:

8.扩展前一个练习中的树,使得每个非叶子节点除了后代之外,能够存放一个操作符。然后编写一个eval函数来计算它的值。举例来说:

+
    / | \
   *  2  -
 /  \    |
3   8    5

9.编写一个函数,计算List[Option[Int]]中所有非None值之和。不得使用match语句。

10.编写一个函数,将两个类型为Double=>Option[Double]的函数组合在一起,产生另一个同样类型的函数。如果其中一个函数返回None,则组合函数也应返回None。例如:

def f(x : Double) = if ( x >= 0) Some(sqrt(x)) else None
def g(x : Double) = if ( x != 1) Some( 1 / ( x - 1)) else None
val h = compose(f,g)

//

package llf

/**
 * Created by sendoh on 2015/5/13.
 */
class answer14 {

}
//14.1
//50%?
//2
class Test2{
  def swap[S,T](tup: (S,T)) = {
    tup match {
      case (a ,b) => (b,a)
    }
  }
  println(swap[String,Int](("1",2)))
}
//3
class Test3{
  def swap(arr: Array[String]): Unit ={
    arr match{
      case Array(a, b, t, m) => Array(b, a) ++ t
      case _ => arr
    }
  }
  println(swap(Array("1","2","3","4")))
}
//4
abstract class Item
case class Multiple(num : Int,item : Item) extends Item
case class Article(description : String , price : Double) extends Item
case class Bundle(description : String , discount : Double , item : Item*) extends Item
object Test extends App{
  def price(it : Item) : Double = it match {
    case Article(_,p) => p
    case Bundle(_,disc,its @ _*) => its.map(price _).sum - disc
    case Multiple(n,it) => n * price(it)
  }
  val p = price(Multiple(10,Article("Blackwell Toster",29.95)))
  println(p)
}
//5
class Test5{
  val l: List[Any] = List(List(3, 8), 2, List(5))
  def leafSum(list: List[Any]): Int = {
    var total = 0
    list.foreach {
      lst =>
        lst match {
          case l: List[Any] => total += leafSum(l)
          case i: Int => total += i
        }
    }
    total
  }
  println(leafSum(l))
}
//6
class Test6{
  sealed abstract class BinaryTree
  case class Leaf(value : Int) extends BinaryTree
  case class Node(left : BinaryTree,right : BinaryTree) extends BinaryTree
  val r = Node(Leaf(3),Node(Leaf(3),Leaf(9)))
  def leafSum(tree: BinaryTree): Int = {
    tree match {
      case Node(a,b) => leafSum(a) + leafSum(b)
      case Leaf(v) => v
    }
  }
  println(leafSum(r))
}
//7
class Test7{
  sealed abstract class BinaryTree
  case class Leaf(value: Int) extends BinaryTree
  case class Node(tr: BinaryTree*) extends BinaryTree
  object Test extends App {
    val r = Node(Node(Leaf(3), Leaf(8)), Leaf(2), Node(Leaf(5)))
    def leafSum(tree: BinaryTree): Int = {
      tree match {
        case Node(r @ _*) => r.map(leafSum).sum
        case Leaf(v) => v
      }
    }
    println(leafSum(r))
  }
}
//8
class Test8{
  sealed abstract class BinaryTree
  case class Leaf(value: Int) extends BinaryTree
  case class Node(ch : Char , tr: BinaryTree*) extends BinaryTree
  object Test extends App {
    val r = Node('+' , Node('*',Leaf(3), Leaf(8)), Leaf(2), Node('-' , Leaf(5)))
    def eval(tree: BinaryTree): Int = {
      tree match {
        case Node(c : Char , r @ _*) => if( c == '+') r.map(eval).sum else if (c == '*') r.map(eval).reduceLeft(_ * _) else r.map(eval).foldLeft(0)(_ - _)
        case Leaf(v) => v
      }
    }
    println(eval(r))
  }
}
//9
class Test9{
  val l : List[Option[Int]] = List(Option(-1),None,Option(2))
  println(l.map(_.getOrElse(0)).sum)
}
//10
class Tese10{
  import scala.math.sqrt
  def f(x : Double) = if ( x >= 0) Some(sqrt(x)) else None
  def g(x : Double) = if ( x != 1) Some( 1 / ( x - 1)) else None
  val h = compose(f,g)
  def compose(f : (Double => Option[Double]), g : (Double => Option[Double])):(Double => Option[Double])={
    (x : Double) =>
      if (f(x) == None || g(x) == None) None
      else g(x)
  }
  println(h(2))
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值