楼主: Lisrelchen
3252 14

【GitBook】Spark and Scala Recipes [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-2-21 03:11:02 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Yummy!

A scratchpad notebook for assorted collection of Spark and Scala examples.


本帖隐藏的内容

https://satishlalam.gitbooks.io/spark-and-scala-recipes/content/functions.html

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Recipes Recipe SCALA Spark Park collection notebook examples

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2017-2-21 03:11:42
  1. Quickly create 'n to m' collection of numbers

  2. 100 to 200 //res0: scala.collection.immutable.Range.Inclusive = Range(100, 101, 102, 103, 104, 105..200)
  3. loop over 'n to m' range

  4. for(i <- 100 to 200) {
  5.   println(i)
  6. }
  7. // prints 100 to 200
  8. create a list and append

  9. val list = List(1,2,3,4,5)

  10. // append to list
  11. list  = list :+ 6  // error since list is val.

  12. var list2 = List(1,2,3)
  13. list2 = list2 :+ 4  // now list is a new List (1,2,3,4)
  14. list2 = 0 +: list2 // now list is a new List(0,1,2,3,4)
  15. Yield a Collection from foreach

  16. scala> val kvp = for(i <- 1 to 5) yield {
  17.      | (i, i*i)
  18.      | }
  19. kvp: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,1), (2,4), (3,9), (4,16), (5,25))
  20. Sequences and Sets

  21. // sequence
  22. //    Indexed - Array
  23. //    Linear - List, Vector (LinkedLists of 32 element arrays)
  24. // set - no dups
  25. Convert a Seq to Map

  26. scala> val kvpMap = kvp.map(k => k._1 -> k._2).toMap
  27. kvpMap: scala.collection.immutable.Map[Int,Int] = Map(5 -> 25, 1 -> 1, 2 -> 4, 3 -> 9, 4 -> 16)
  28. Flatten a list of tuples

  29. val a = List(1,3,5)
  30. val b = List(2,4,6)
  31. val listOfTuples = a.zip(b)  // List( (1,2), (3,4), (5,6))
  32. val flatList = listOfTuples.flatMap(x => List(x._1, x._2)) // List(1,2,3,4,5,6)
  33. Seperate Even and Odd Numbers in a list

  34. val list = (1 to 10)
  35. val g = list.groupBy(x => x % 2)
  36. // returns: Map(1 -> Vector(1, 3, 5, 7, 9), 0 -> Vector(2, 4, 6, 8, 10))
  37. takeWhile
复制代码

藤椅
Lisrelchen 发表于 2017-2-21 03:14:41
  1. Convert a list of lines to a single line

  2. val lines : List[String] = List("this is line 1" , "this is line 2", "this is line 3")
  3. // concat without delimiter
  4. lines.mkString
  5. // output: this is line 1this is line 2this is line 3  

  6. // concat with delimiter
  7. lines.foldLeft("")((a,b)=> (a + " " + b))
  8. // output: this is line 1 this is line 2 this is line 3
  9. Gotcha

  10. // foldLeft vs foldRight
  11. // foldLeft is more performant due to iterative implementation. FoldRight uses recursions

  12. Internal code:
  13. def foldLeft[B](z: B)(f: (B, A) => B): B = {
  14.     var acc = z
  15.     var these = this
  16.     while (!these.isEmpty) {
  17.       acc = f(acc, these.head)
  18.       these = these.tail
  19.     }
  20.     acc
  21.   }

  22. override /*IterableLike*/
  23.   def foldRight[B](z: B)(f: (A, B) => B): B =
  24.     if (this.isEmpty) z
  25.     else f(head, tail.foldRight(z)(f))
  26. Count Words in a list of lines

  27. import scala.io.Source

  28. val lines = Source.fromFile("myfile").getLines()
  29. val line = lines.foldLeft("")((a,b) => (a + " " + b)).trim // a single string of all lines
  30. line.split(' ').length
  31. Count Characters in a list of lines

  32. import scala.io.Source

  33. Source.fromFile("myfile")
  34.         .getLines()
  35.         .foldLeft("")((a,b) => (a + " " + b))
  36.         .filter(_ != ' ')
  37.         .length
  38. Count occurence of a particular word or character in a list of lines
  39. import scala.io.Source

  40. val word = "scala"
  41. val char = "s"

  42. // fullText is of type String
  43. val fullText = Source.fromFile("myfile")
  44.         .getLines()
  45.         .foldLeft("")((a,b) => (a + " " + b))


  46. // count words
  47. fullText.split(' ').count(_ == word)

  48. // count chars
  49. fullText.count(_ == char)
复制代码

板凳
Lisrelchen 发表于 2017-2-21 03:15:16
  1. Read all lines from a text file

  2. import scala.io.Source
  3. Source.fromFile("myfile")).foreach(println)
  4. Get Current Working Directory

  5. val path = new java.io.File(".").getCanonicalPath
复制代码

报纸
Lisrelchen 发表于 2017-2-21 03:15:57
  1. Load an Xml file

  2. import scala.xml.{NodeSeq, XML}

  3. // from path
  4. var xmlNode = XML.loadFile("/path/to/my.xml")

  5. // from resource (ex: under  src/main/resources/my.xml)
  6. var xmlNode = XML.load(getClass.getResourceAsStream("/my.xml"))
复制代码

地板
Lisrelchen 发表于 2017-2-21 03:17:04
  1. Measure time for a block

  2. def time[R](block: => R): R = {  
  3.     val t0 = System.nanoTime()
  4.     val result = block    // call-by-name
  5.     val t1 = System.nanoTime()
  6.     println("Elapsed time: " + (t1 - t0) + "ns")
  7.     result
  8. }

  9. val result = time { // code block
  10. }
复制代码

7
franky_sas 发表于 2017-2-21 11:17:29
已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

8
smartlife 在职认证  发表于 2017-2-21 11:27:41
已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

9
钱学森64 发表于 2017-2-21 12:38:54
谢谢分享

10
MouJack007 发表于 2017-2-21 14:34:01
谢谢楼主分享!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-4 05:33