scala知识点(一)

1.drop,dropRight,dropWhile

drop: drop(n: Int): List[A] 丢弃前n个元素,返回剩下的元素

dropRight: dropRight(n: Int): List[A] 丢弃最后n个元素,返回剩下的元素

dropWhile: dropWhile(p: (A) ⇒ Boolean): List[A] 从左向右丢弃元素,直到条件p不成立

1 val nums = List(1,1,1,1,4,4,4,4)
2 val left = nums.drop(4)   // List(4,4,4,4)
3 val right = nums.dropRight(4) // List(1,1,1,1)
4 val tailNums = nums.dropWhile( _ == nums.head)  // List(4,4,4,4)

还有更多的操作 http://blog.csdn.net/pzw_0612/article/details/45936165

2. GraphX 图数据建模和存储

原文: http://blog.csdn.net/pelick/article/details/47293495

(1)背景:简单分析一下GraphX是怎么为图数据建模和存储的。

(2)入口:可以看 GraphLoader 的函数,

1 def edgeListFile(
2   sc: SparkContext,
3   path: String,
4   canonicalOrientation: Boolean = false,
5   numEdgePartitions: Int = -1,
6   edgeStorageLevel: StorageLevel = StorageLevel.MEMORY_ONLY,
7   vertexStorageLevel: StorageLevel = StorageLevel.MEMORY_ONLY)
8 : Graph[Int, Int]

  path可以是本地路径(文件或文件夹),也可以是hdfs路径,本质上是使用 sc.textFile 来生成HadoopRDD的, numEdgePartitions 是分区数。
    Graph的存储是分EdgeRDD和VertexRDD两块,可以分别设置StorageLevel。默认是内存。
    这个函数接受边文件,即’1 2’, ‘4 1’这样的点到点的数据对组成的文件。把这份文件按分区数和存储level转化成一个可以操作的图。

(3)流程:

  •     sc.textFile 读文件,生成原始的RDD
  •     每个分区(的计算节点)把每条记录放进 PrimitiveVector 里,这个结构是spark里为primitive数据优化的存储结构。
  •     把 PrimitiveVector 里的数据一条条取出,转化成 EdgePartition ,即 EdgeRDD 的分区实现。这个过程中生成了面向列存的结构:src点的array,dst点的array,edge的属性array,以及两个正反向map(用于对应点的local id和global id)。
  •     对 EdgeRDD 做一次count触发这次边建模任务,真正persist起来。
  •     用 EdgePartition 去生成一个 RoutingTablePartition ,里面是vertexId到partitionId的对应关系,借助 RoutingTablePartition 生成 VertexRDD 。
  •     由 EdgeRDD 和 VertexRDD 生成 Graph 。前者维护了边的属性、边两头顶点的属性、两头顶点各自的global vertexID、两头顶点各自的local Id(在一个edge分区里的array index)、用于寻址array的正反向map。后者维护了点存在于哪个边的分区上的Map。

以下是代码,比较清晰地展现了内部存储结构。

private[graphx]
class EdgePartition[
  @specialized(Char, Int, Boolean, Byte, Long, Float, Double) ED: ClassTag, VD: ClassTag](
  localSrcIds: Array[Int],
  localDstIds: Array[Int],
  data: Array[ED],
  index: GraphXPrimitiveKeyOpenHashMap[VertexId, Int],
  global2local: GraphXPrimitiveKeyOpenHashMap[VertexId, Int],
  local2global: Array[VertexId],
  vertexAttrs: Array[VD],
  activeSet: Option[VertexSet])
  extends Serializable {

/**
 * Stores the locations of edge-partition join sites for each vertex attribute in a particular
 * vertex partition. This provides routing information for shipping vertex attributes to edge
 * partitions.
 */
private[graphx]
class RoutingTablePartition(
    private val routingTable: Array[(Array[VertexId], BitSet, BitSet)]) extends Serializable {

(4)GraphLoader:graphLoader是graphx中专门用于图的加载和生成,最重要的函数就是edgeListFile

(5)IllegalArgumentException此异常表明向方法传递了一个不合法或不正确的参数。

(6)GraphX提供了ConnectedComponents和StronglyConnected-Components算法,使用它们可以快速计算出相应的连通图

(7)val graph = GraphLoader.edgeListFile(sc, "/home/spark/spark/graphx/data/followers.txt")//加载边时顶点是边上出现的点

(8)val bobsScore = scores.getOrElse("Bob", 0) 检查是否包含指定的键。

原文地址:https://www.cnblogs.com/nolonely/p/5391207.html