{"id":1606,"date":"2021-01-13T16:00:24","date_gmt":"2021-01-13T08:00:24","guid":{"rendered":"https:\/\/www.specialwu.com\/?p=1606"},"modified":"2021-01-13T18:38:50","modified_gmt":"2021-01-13T10:38:50","slug":"sparkcore-rdd%e7%ae%97%e5%ad%90%e7%b3%bb%e5%88%97","status":"publish","type":"post","link":"http:\/\/www.specialwu.com\/?p=1606","title":{"rendered":"sparkcore-rdd\u7b97\u5b50\u7cfb\u5217"},"content":{"rendered":"<blockquote><p>\n  RDD\u7684\u8f6c\u6362\u7b97\u5b50\n<\/p><\/blockquote>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/spark.apache.org\/docs\/latest\/rdd-programming-guide.html#resilient-distributed-datasets-rdds\" title=\"\u70b9\u51fb\u67e5\u770bsparkrdd\u5b98\u65b9\u6587\u6863\">\u70b9\u51fb\u67e5\u770bsparkrdd\u5b98\u65b9\u6587\u6863<\/a><\/p>\n<table>\n<thead>\n<tr>\n<th>\u7b97\u5b50\u540d\u79f0<\/th>\n<th>\u5b98\u7f51\u89e3\u91ca<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>map(func)<\/td>\n<td>Return a new distributed dataset formed by passing each element of the source through a function func.<\/td>\n<td>map (f: T => U) : RDD[U] \u5176\u4e2df\u5b9a\u4e49\u4e86\u7c7b\u578b\u4e3aT\u7684\u5143\u7d20\u5230\u7c7b\u578b\u4e3aU\uff0c\u5982\u679c\u8ba1\u7b97key\u503c\u53ef\u5199\u4e3amap(_,1)<\/td>\n<\/tr>\n<tr>\n<td>filter(func)<\/td>\n<td>Return a new dataset formed by selecting those elements of the source on which func returns true.<\/td>\n<td>filter(f: T => Boolean)\u5176\u4e2df\u5b9a\u4e49\u4e86\u7c7b\u578b\u4e3aT\u7684\u5143\u7d20\u662f\u5426\u7559\u4e0b\uff0c\u8fc7\u6ee4\u8f93\u5165RDD\u4e2d\u7684\u5143\u7d20\uff0c\u5c06f\u8fd4\u56detrue\u7684\u5143\u7d20\u7559\u4e0b,\u6bd4\u5982\u53ef\u7528\u6765\u627e\u51fa\u54cd\u5e94\u7801404\u7684\u7528\u6237rdd.filter(_<span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\">404)<\/span><\/td>\n<\/tr>\n<tr>\n<td>flatMap(func)<\/td>\n<td>Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).<\/td>\n<td>flatMap(f: T =>TraversableOnce[U]): RDD[U],\u5c06\u51fd\u6570f\u4f5c\u7528\u5728RDD\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e76\u5c55\u5f00\uff08flatten\uff09\u8f93\u51fa\u7684\u6bcf\u4e2a\u7ed3\u679c, flatMap = flatten + map,\u5148\u6620\u5c04\uff08map\uff09\uff0c\u518d\u62cd\u6241\uff08flatten \uff09\u5c06\u4e8c\u7ef4\u96c6\u5408\u53d8\u6210\u4e00\u7ef4\u96c6\u5408<\/td>\n<\/tr>\n<tr>\n<td>mapPartitions(func)<\/td>\n<td>Similar to map, but runs separately on each partition (block) of the RDD, so func must be of type Iterator<T> => Iterator<U> when running on an RDD of type T.<\/td>\n<td>mapPartitions(x:Iterator[Int])<\/td>\n<\/tr>\n<tr>\n<td>mapPartitionsWithIndex(func)<\/td>\n<td>Similar to mapPartitions, but also provides func with an integer value representing the index of the partition, so func must be of type (Int, Iterator<T>) => Iterator<U> when running on an RDD of type T.<\/td>\n<td>mapPartitionsWithIndex()\u4f20\u5165\u7684\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u4e3a\u5206\u533aId\uff0c\u53e6\u4e00\u4e2a\u4e3a\u5206\u533a\u6570\u636e\uff0c\u8be5\u65b9\u6cd5\u53ef\u7528\u6765\u67e5\u770b\u5404\u5206\u533a\u7684\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>sample(withReplacement, fraction, seed)<\/td>\n<td>Sample a fraction fraction of the data, with or without replacement, using a given random number generator seed.<\/td>\n<td>\u6839\u636e\u7ed9\u5b9a\u7684\u968f\u673a\u79cd\u5b50seed\uff0c\u968f\u673a\u62bd\u6837\u51fa\u6570\u91cf\u4e3afrac\u7684\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>union(otherDataset)<\/td>\n<td>Return a new dataset that contains the union of the elements in the source dataset and the argument.<\/td>\n<td>\u5c06\u591a\u4e2aRDD\u5408\u5e76\u4e3a\u4e00\u4e2aRDD<\/td>\n<\/tr>\n<tr>\n<td>distinct([numPartitions]))<\/td>\n<td>Return a new dataset that contains the distinct elements of the source dataset.<\/td>\n<td>\u53bb\u91cd<\/td>\n<\/tr>\n<tr>\n<td>groupByKey([numPartitions])<\/td>\n<td>When called on a dataset of (K, V) pairs, returns a dataset of (K, Iterable<V>) pairs.Note: If you are grouping in order to perform an aggregation (such as a sum or average) over each key, using reduceByKey or aggregateByKey will yield much better performance.Note: By default, the level of parallelism in the output depends on the number of partitions of the parent RDD. You can pass an optional numPartitions argument to set a different number of tasks.<\/td>\n<td>\u6309Key\u8fdb\u884c\u5206\u7ec4\uff0c\u8fd4\u56de[K,Iterable[V]]\uff0cnumPartitions\u8bbe\u7f6e\u5206\u533a\u6570\uff0c\u63d0\u9ad8\u4f5c\u4e1a\u5e76\u884c\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>reduceByKey(func, [numPartitions])<\/td>\n<td>When called on a dataset of (K, V) pairs, returns a dataset of (K, V) pairs where the values for each key are aggregated using the given reduce function func, which must be of type (V,V) => V. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.<\/td>\n<td>\u6309Key\u8fdb\u884c\u5206\u7ec4\uff0c\u4f7f\u7528\u7ed9\u5b9a\u7684func\u51fd\u6570\u805a\u5408value\u503c, numPartitions\u8bbe\u7f6e\u5206\u533a\u6570\uff0c\u63d0\u9ad8\u4f5c\u4e1a\u5e76\u884c\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>sortByKey([ascending], [numPartitions])<\/td>\n<td>When called on a dataset of (K, V) pairs where K implements Ordered, returns a dataset of (K, V) pairs sorted by keys in ascending or descending order, as specified in the boolean ascending argument.<\/td>\n<td>\u5bf9\u4e24\u4e2aRDD(\u5982:(K,V)\u548c(K,W))\u76f8\u540cKey\u7684\u5143\u7d20\u5148\u5206\u522b\u505a\u805a\u5408\uff0c\u6700\u540e\u8fd4\u56de(K,Iterator<V>,Iterator<W>)\u5f62\u5f0f\u7684RDD,numPartitions\u8bbe\u7f6e\u5206\u533a\u6570\uff0c\u63d0\u9ad8\u4f5c\u4e1a\u5e76\u884c\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>join(otherDataset, [numPartitions])<\/td>\n<td>When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (V, W)) pairs with all pairs of elements for each key. Outer joins are supported through leftOuterJoin, rightOuterJoin, and fullOuterJoin.<\/td>\n<td>\u5bf9\u4e24\u4e2aRDD\u5148\u8fdb\u884ccogroup\u64cd\u4f5c\u5f62\u6210\u65b0\u7684RDD\uff0c\u518d\u5bf9\u6bcf\u4e2aKey\u4e0b\u7684\u5143\u7d20\u8fdb\u884c\u7b1b\u5361\u5c14\u79ef\uff0cnumPartitions\u8bbe\u7f6e\u5206\u533a\u6570\uff0c\u63d0\u9ad8\u4f5c\u4e1a\u5e76\u884c\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>intersection(otherDataset)<\/td>\n<td>Return a new RDD that contains the intersection of elements in the source dataset and the argument.<\/td>\n<td>\u4ea4\u96c6<\/td>\n<\/tr>\n<tr>\n<td>repartition(numPartitions)<\/td>\n<td>Reshuffle the data in the RDD randomly to create either more or fewer partitions and balance it across them. This always shuffles all data over the network.<\/td>\n<td>\u901a\u8fc7\u521b\u5efa\u66f4\u8fc7\u6216\u66f4\u5c11\u7684\u5206\u533a\u5c06\u6570\u636e\u968f\u673a\u7684\u6253\u6563\uff0c\u8ba9\u6570\u636e\u5728\u4e0d\u540c\u5206\u533a\u4e4b\u95f4\u76f8\u5bf9\u5747\u5300\u3002\u8fd9\u4e2a\u64cd\u4f5c\u7ecf\u5e38\u662f\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u6570\u6253\u6563\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code>\u6784\u5efardd\n<\/code><\/pre>\n<pre><code class=\"language-scala line-numbers\">val conf = new SparkConf()\n      .setAppName(\"demo1\")\n      .setMaster(\"local[2]\")\n    val sc = new SparkContext(conf)\n    sc.setCheckpointDir(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\mapdata\\\\\")\n    val rdd: RDD[String] = sc.textFile(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\mapdata\\\\access.log\")\n<\/code><\/pre>\n<blockquote><p>\n  def map(f: T => U): RDD[U]\n<\/p><\/blockquote>\n<p>1.map<br \/>\n    1.1\u58f0\u660e def map(f: T => U): RDD[U]<br \/>\n    1.2\u53c2\u6570 \u4e00\u4e2a\u4e00\u5143\u51fd\u6570\uff0c\u53c2\u6570\u662f\u539fRDD\u7684\u5143\u7d20\u7c7b\u578b\uff0c\u8fd4\u56de\u503c\u53ef\u4ee5\u6539\u53d8\u7c7b\u578b<br \/>\n    1.3\u8fd4\u56de\u503c  \u65b0\u7684RDD\uff0c\u6cdb\u578b\u662ff\u7684\u8fd4\u56de\u503c\u7c7b\u578b<br \/>\n    1.4\u4f5c\u7528 \u5c06\u539fRDD\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u5230f\u4e2d\uff0c\u5c06\u8fd4\u56de\u503c\u6536\u96c6\u5230\u4e00\u4e2a\u65b0\u7684RDD\u4e2d<\/p>\n<pre><code class=\"language-scala line-numbers\">rdd1.map((x=&gt;{\n      new Tuple2(x._1,1)\n    }))\n<\/code><\/pre>\n<blockquote><p>\n  filter def filter(f: T => Boolean): RDD[T]\n<\/p><\/blockquote>\n<p>2.filter<br \/>\n    2.1.\u58f0\u660edef filter(f: T => Boolean): RDD[T]<br \/>\n   2.2.\u53c2\u6570 \u4e00\u5143\u51fd\u6570\uff0c\u53c2\u6570\u662f\u539frdd\u7684\u4e2d\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u503c\u662fBoolean<br \/>\n  2.3.\u8fd4\u56de\u503c  \u548c\u539fRDD\u6cdb\u578b\u4e00\u81f4\u7684RDD<br \/>\n 2.4.\u4f5c\u7528  \u8fc7\u6ee4\u5143\u7d20 (f\u8fd4\u56deTrue\u7684\u5143\u7d20\u5c06\u88ab\u7559\u4e0b)<\/p>\n<pre><code class=\"language-scala line-numbers\">rdd1.filter(x=&gt;{\n  x._3==404\n})\nrdd1.filter(_._3 == 404)\n<\/code><\/pre>\n<blockquote><p>\n  def flatMap(f: T => TraversableOnce[U]): RDD[U]\n<\/p><\/blockquote>\n<p>3.flatMap<br \/>\n    1.\u58f0\u660edef flatMap(f: T => TraversableOnce[U]): RDD[U]<br \/>\n    2.\u53c2\u6570 \u4e00\u4e2a\u4e00\u5143\u51fd\u6570\uff0c\u53c2\u6570\u662f\u539fRDD\u7684\u5143\u7d20\u7c7b\u578b\uff0c\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u96c6\u5408<br \/>\n   3.\u8fd4\u56de\u503c \u662f\u4e00\u4e2af\u7684\u8fd4\u56de\u503c\u6cdb\u578b\u7684RDD<br \/>\n   4.\u4f5c\u7528 \u5c06\u4e8c\u7ef4\u96c6\u5408\u53d8\u6210\u4e00\u7ef4\u96c6\u5408<\/p>\n<pre><code class=\"language-scala line-numbers\">    val conf = new SparkConf().setAppName(\"0113\").setMaster(\"local[2]\")\n    val sc = new SparkContext(conf)\n    val list = List(\"give you\", \"give me\", \"a job\")\n    val rdd=sc.parallelize(list)\n    val rdd2=rdd.flatMap(x=&gt;{\n     val s = x.split(\" \")\n      s\n    })\n    rdd2.foreach(println)\n    val rdd3: RDD[String] = rdd1.flatMap(_._1.replace(\".\", \"\").split(\"\"))\n<\/code><\/pre>\n<blockquote><p>\n  mapPartitions\n<\/p><\/blockquote>\n<p>4.mapPartitions<br \/>\n    4.1.\u58f0\u660e  def mapPartitions(<br \/>\n          f: Iterator[T] => Iterator[U],<br \/>\n          preservesPartitioning: Boolean = false): RDD[U]<br \/>\n    4.2.\u53c2\u6570<br \/>\n      \u7b2c\u4e00\u4e2a\u53c2\u6570<br \/>\n         \u4e00\u4e2a\u4e00\u5143\u51fd\u6570\uff0c\u53c2\u6570\u662f\u4e00\u4e2a\u539fRDD\u6cdb\u578b\u7684\u8fed\u4ee3\u5668\uff0c<br \/>\n         \u8fd9\u4e2a\u8fed\u4ee3\u5668\u6bcf\u6b21\u4f20\u5165\u4e00\u4e2a\u5206\u533a\u7684\u5168\u90e8\u5143\u7d20<br \/>\n         \u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6cdb\u578b\u4e0d\u9650<br \/>\n    4.3.\u8fd4\u56de\u503c  RDD[U]  \u6cdb\u578b\u4e3af\u8fd4\u56de\u503c\u6cdb\u578b\u7684RDD<br \/>\n    4.4.\u4f5c\u7528<br \/>\n         \u4e4b\u524dmap\u662f\u6bcf\u6b21\u62ff\u5230\u4e00\u4e2a\u5143\u7d20<br \/>\n         mapPartitions\u4e00\u6b21\u6027\u62ff\u5230\u4e00\u4e2a\u5206\u533a\u7684\u5143\u7d20<br \/>\n         \u5728\u5c06\u5904\u7406\u5b8c\u7684\u7ed3\u679c\u653e\u56de\u5230\u65b0\u7684RDD\u4e2d<\/p>\n<pre><code>\u5982\u679c\u5728map\u8fc7\u7a0b\u4e2d\u9700\u8981\u9891\u7e41\u521b\u5efa\u989d\u5916\u7684\u5bf9\u8c61(\u4f8b\u5982\u5c06rdd\u4e2d\u7684\u6570\u636e\u901a\u8fc7jdbc\u5199\u5165\u6570\u636e\u5e93,map\u9700\u8981\u4e3a\u6bcf\u4e2a\u5143\u7d20\u521b\u5efa\u4e00\u4e2a\u94fe\u63a5\u800cmapPartition\u4e3a\u6bcf\u4e2apartition\u521b\u5efa\u4e00\u4e2a\u94fe\u63a5),\u5219mapPartitions\u6548\u7387\u6bd4map\u9ad8\u7684\u591a\u3002\n<\/code><\/pre>\n<pre><code class=\"language-scala line-numbers\">val data = sc.parallelize(1 to 4)\n\n    data.mapPartitions(x=&gt;mapParrtitionsF(x)).count()\n    data.map(x=&gt;mapF(x)).count()\n\n    def mapF(x:Int):Int={\n      println(\"\u8c03\u7528\u4e86mapF\u65b9\u6cd5\")\n      x\n    }\n\n    def mapParrtitionsF(x:Iterator[Int]):Iterator[Int]={\n      println(\"\u8c03\u7528\u4e86mapParrtitionsF\u65b9\u6cd5\")\n      x\n    }\n\u8c03\u7528\u4e86mapParrtitionsF\u65b9\u6cd5\n\u8c03\u7528\u4e86mapParrtitionsF\u65b9\u6cd5\n\n\u8c03\u7528\u4e86mapF\u65b9\u6cd5\n\u8c03\u7528\u4e86mapF\u65b9\u6cd5\n\u8c03\u7528\u4e86mapF\u65b9\u6cd5\n\u8c03\u7528\u4e86mapF\u65b9\u6cd5\n<\/code><\/pre>\n<blockquote><p>\n  mapPartitionsWithIndex\n<\/p><\/blockquote>\n<p>5.mapPartitionsWithIndex<br \/>\n    5.1.\u58f0\u660e  def mapPartitionsWithIndex(<br \/>\n            \u5206\u533a\u7d22\u5f15<br \/>\n          f: (Int, Iterator[T]) => Iterator[U],<br \/>\n          preservesPartitioning: Boolean = false): RDD[U]<br \/>\n    5.2.\u53c2\u6570   \u4e0emapPartitions\u7c7b\u4f3c\uff0c\u591a\u4e86\u4e00\u4e2aint\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u63a5\u6536\u4f20\u8fdb\u6765\u7684\u5206\u533a\u7d22\u5f15<br \/>\n    5.3.\u8fd4\u56de\u503c RDD[U]<br \/>\n    5.4.\u4f5c\u7528  \u4e0emapPartitions\u7c7b\u4f3c<\/p>\n<pre><code class=\"language-scala line-numbers\">al data = sc.parallelize(1 to 5,3)\n\n    data.mapPartitionsWithIndex((x,iter)=&gt;{\n      var result=List[String]()\n      while (iter.hasNext){\n        result ::=(x+\"-\"+iter.next())\n      }\n      result.toIterator\n    }).foreach(println)\n0-1\n1-3\n1-2\n\n2-5\n2-4\n<\/code><\/pre>\n<blockquote><p>\n  sample\n<\/p><\/blockquote>\n<p>\u8ba1\u7b97\u673a\u7684\u968f\u673a\u7b97\u6cd5\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u5982\u679c\u8f93\u5165\u53c2\u6570\u4e0d\u540c&#8211;>>\u8f93\u51fa\u7ed3\u679c\u4e0d\u540c<\/p>\n<pre><code>1.\u58f0\u660e  def sample(\n      withReplacement: Boolean, \/\/\u662f\u5426\u653e\u56de\n      fraction: Double, \/\/\u62bd\u6837\u6bd4\u4f8b \u4e0d\u662f\u975e\u5e38\u7cbe\u51c6\n      seed: Long = Utils.random.nextLong \/\/\u968f\u673a\u79cd\u5b50\n     ): RDD[T]\n2.\u53c2\u6570\n3.\u8fd4\u56de\u503c \u548c\u539fRDD\u7c7b\u578b\u76f8\u540c\u7684RDD\n4.\u4f5c\u7528  \u5728\u6d77\u91cf\u6570\u636e\u4e2d\u8fdb\u884c\u62bd\u6837\n<\/code><\/pre>\n<pre><code class=\"language-scala line-numbers\"> val list1 = 1 to 1000\n    val listRDD = sc.parallelize(list1)\n    val sampleRDD = listRDD.sample(false, 0.2)\n\n    sampleRDD.foreach(num =&gt; print(num + \" \"))\n    println(\"\u7b2c\u4e00\u6b21\u62bd\u6837 \" + sampleRDD.count())\n    println(\"\u7b2c\u4e8c\u6b21\u62bd\u6837\" + sc.parallelize(list1).sample(false, 0.2).count())\n\n    1 3 19 20 22 25 33...\n    \u7b2c\u4e00\u6b21\u62bd\u6837 181\n    \u7b2c\u4e8c\u6b21\u62bd\u6837197\n<\/code><\/pre>\n<blockquote><p>\n  union   RDDA \u222a RDDB\n<\/p><\/blockquote>\n<p>7.union   RDDA \u222a RDDB<br \/>\n    1.\u58f0\u660e   def union(other: RDD[T]): RDD[T]<br \/>\n    2.\u53c2\u6570    \u53e6\u5916\u4e00\u4e2a\u548c\u539fRDD\u7c7b\u578b\u76f8\u540c\u7684RDD<br \/>\n    3.\u8fd4\u56de\u503c  \u548c\u539fRDD\u7c7b\u578b\u76f8\u540c<br \/>\n    4.\u4f5c\u7528   \u5408\u5e76RDD<\/p>\n<pre><code class=\"language-scala line-numbers\">val pairs1 = Seq((\"A\",1), (\"B\",1), (\"C\",1), (\"D\", 1), (\"A\", 2), (\"C\", 3))\n\n    val rdd5 = sc.makeRDD(pairs1, 3)\n\n    val pairs2 = Seq((\"A\",4), (\"D\",1), (\"E\", 1))\n\n    val rdd6 = sc.makeRDD(pairs2, 2)\n\n    rdd5.union(rdd6).foreach(println)\n(A,1)\n(B,1)\n(D,1)\n(E,1)\n(A,4)\n<\/code><\/pre>\n<blockquote><p>\n  intersection   RDDA \u2229 RDDB\n<\/p><\/blockquote>\n<p>8.intersection   RDDA \u2229 RDDB<br \/>\n    1.\u58f0\u660e    def intersection(other: RDD[T]): RDD[T]<br \/>\n    2.\u53c2\u6570   \u53e6\u5916\u4e00\u4e2a\u548c\u539fRDD\u7c7b\u578b\u76f8\u540c\u7684RDD<br \/>\n    3.\u8fd4\u56de\u503c  \u548c\u539fRDD\u7c7b\u578b\u76f8\u540c<br \/>\n    4.\u4f5c\u7528  \u4ea4\u96c6<\/p>\n<pre><code class=\"language-scala line-numbers\">    val pairs1 = Seq((\"A\",1), (\"B\",1), (\"C\",1), (\"D\", 1), (\"A\", 2), (\"C\", 3))\n\n    val rdd5 = sc.makeRDD(pairs1, 3)\n\n    val pairs2 = Seq((\"A\",1), (\"D\",1), (\"E\", 1))\n\n    val rdd6 = sc.makeRDD(pairs2, 2)\n\n    rdd5.intersection(rdd6).foreach(println)\n(A,1)\n(D,1)\n<\/code><\/pre>\n<blockquote><p>\n  distinct\n<\/p><\/blockquote>\n<pre><code class=\"language-scala line-numbers\">sc.makeRDD(Array(1, 2, 1, 1, 2, 3, 4, 5))\n      .distinct()\n         .foreach(println)\n1\n4\n3\n2\n5\n<\/code><\/pre>\n<blockquote><p>\n  partitionBy\n<\/p><\/blockquote>\n<p>0 partitionBy<br \/>\n    1.\u58f0\u660e def partitionBy(partitioner: Partitioner): RDD[(K, V)]<br \/>\n    2.\u53c2\u6570  \u662f\u4e00\u4e2a\u5206\u533a\u5668\u5bf9\u8c61\u7684\u5b9e\u4f8b<br \/>\n   3.\u8fd4\u56de\u503c  \u548c\u539fRDD\u7c7b\u578b\u76f8\u540c<br \/>\n    4.\u4f5c\u7528   \u6309\u7167\u81ea\u5b9a\u7684\u5206\u533a\u5668\uff0c\u5bf9RDD\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5206\u533a<\/p>\n<pre><code class=\"language-scala line-numbers\">\/\/\u4f7f\u7528partitionBy\u91cd\u5206\u533a\nvar rdd2 = rdd1.partitionBy(new org.apache.spark.HashPartitioner(2))\n<\/code><\/pre>\n<blockquote><p>\n  groupByKey\n<\/p><\/blockquote>\n<p>\u5bf9\u6570\u7ec4\u8fdb\u884c group by key\u64cd\u4f5c<br \/>\ngroupByKey([numTasks]): \u5728\u4e00\u4e2a\u7531\uff08K,V\uff09\u5bf9\u7ec4\u6210\u7684\u6570\u636e\u96c6\u4e0a\u8c03\u7528\uff0c\u8fd4\u56de\u4e00\u4e2a\uff08K\uff0cSeq[V])\u5bf9\u7684\u6570\u636e\u96c6\u3002<br \/>\n\u6ce8\u610f\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4f7f\u75288\u4e2a\u5e76\u884c\u4efb\u52a1\u8fdb\u884c\u5206\u7ec4\uff0c\u4f60\u53ef\u4ee5\u4f20\u5165numTask\u53ef\u9009\u53c2\u6570\uff0c\u6839\u636e\u6570\u636e\u91cf\u8bbe\u7f6e\u4e0d\u540c\u6570\u76ee\u7684Task<br \/>\n  mr\u4e2d\uff1a&lt;k1, v1>&#8212;>map\u64cd\u4f5c&#8212;>&lt;k2, v2>&#8212;>shuffle&#8212;>&lt;k2, [v21, v22, v23&#8230;]>&#8212;>&lt;k3, v3><br \/>\ngroupByKey\u7c7b\u4f3c\u4e8eshuffle\u64cd\u4f5c<br \/>\n\u548creduceByKey\u6709\u70b9\u7c7b\u4f3c\uff0c\u4f46\u662f\u6709\u533a\u522b\uff0creduceByKey\u6709\u672c\u5730\u7684\u89c4\u7ea6\uff0c\u800cgroupByKey\u6ca1\u6709\u672c\u5730\u89c4\u7ea6\uff0c\u6240\u4ee5\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c \u5c3d\u91cf\u614e\u7528groupByKey\uff0c\u5982\u679c\u4e00\u5b9a\u8981\u7528\u7684\u8bdd\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e2agroupByKey\uff0c\u5728\u81ea\u5b9a\u4e49\u7684gbk\u4e2d\u6dfb\u52a0\u672c\u5730\u9884\u805a\u5408\u64cd\u4f5c<\/p>\n<pre><code class=\"language-scala line-numbers\"> val list = List(\"give you\", \"give me\", \"a job\")\n    val rdd=sc.parallelize(list)\n    val rdd9=rdd.map(x=&gt;{\n      new Tuple2(x,1)\n    })\n    rdd9.foreach(println)\n    val rdd10=rdd9.groupByKey()\n    rdd10.foreach(x=&gt;{\n      println(x._1+\"sss\"+x._2)\n    })\n(give me,1)\n(give you,1)\n(a job,1)\n\ngive yousssCompactBuffer(1)\na jobsssCompactBuffer(1)\ngive messsCompactBuffer(1)\n<\/code><\/pre>\n<blockquote><p>\n  reduceByKey\n<\/p><\/blockquote>\n<pre><code class=\"language-scala line-numbers\"> val list = List(\"give you\", \"give me\", \"a job\")\n    val rdd=sc.parallelize(list)\n    val rdds=rdd.flatMap(x=&gt;{\n      x.split(\" \")\n    })\n    val rdd9=rdds.map(x=&gt;{\n      new Tuple2(x,1)\n    }).reduceByKey((v1,v2)=&gt;{\n      v1+v2\n    }).foreach(println)\n(job,1)\n(you,1)\n(a,1)\n(give,2)\n(me,1)\n<\/code><\/pre>\n<blockquote><p>\n  sortbykey\n<\/p><\/blockquote>\n<pre><code class=\"language-scala line-numbers\">    val list = List(\"give you\", \"give me\", \"a job\")\n    val rdd=sc.parallelize(list)\n    val rdds=rdd.flatMap(x=&gt;{\n      x.split(\" \")\n    })\n    val rdd9=rdds.map(x=&gt;{\n      new Tuple2(x,1)\n    })\n    rdd9.foreach(println)\n    rdd9.sortByKey().foreach(println)\n(give,1)\n(give,1)\n(me,1)\n(you,1)\n(a,1)\n(job,1)\n\n(job,1)\n(me,1)\n(you,1)\n(a,1)\n(give,1)\n(give,1)\n<\/code><\/pre>\n<blockquote><p>\n  join\n<\/p><\/blockquote>\n<pre><code class=\"language-scala line-numbers\"> val pairs1 = Seq((\"A\",1), (\"B\",1), (\"C\",1), (\"D\", 1), (\"A\", 2), (\"C\", 3))\n\n    val rdd5 = sc.makeRDD(pairs1, 3)\n\n    val pairs2 = Seq((\"A\",1), (\"D\",1), (\"E\", 1))\n\n    val rdd6 = sc.makeRDD(pairs2, 2)\n\n    val joinrdd=rdd5.join(rdd6)\n    joinrdd.foreach(println)\n(A,(1,1))\n(A,(2,1))\n(D,(1,1))\n<\/code><\/pre>\n<blockquote><p>\n  repartition\n<\/p><\/blockquote>\n<pre><code class=\"language-scala line-numbers\"> val list = List(\"give you\", \"give me\", \"a job\")\n    val rdd=sc.parallelize(list)\n    val rdds=rdd.flatMap(x=&gt;{\n      x.split(\" \")\n    })\n    val rdd9=rdds.map(x=&gt;{\n      new Tuple2(x,1)\n    })\n    rdd9.foreach(x=&gt;{\n      println(x+\"\u5f53\u524d\u5206\u533a\"+TaskContext.getPartitionId())\n    })\n    println(\"=======\")\n    rdd9.repartition(3).foreach(x=&gt;{\n      println(x+\"\u5f53\u524d\u5206\u533a\"+TaskContext.getPartitionId())\n    })\n(give,1)\u5f53\u524d\u5206\u533a1\n(me,1)\u5f53\u524d\u5206\u533a1\n(a,1)\u5f53\u524d\u5206\u533a1\n(job,1)\u5f53\u524d\u5206\u533a1\n(give,1)\u5f53\u524d\u5206\u533a0\n(you,1)\u5f53\u524d\u5206\u533a0\n\n(give,1)\u5f53\u524d\u5206\u533a1\n(give,1)\u5f53\u524d\u5206\u533a1\n(job,1)\u5f53\u524d\u5206\u533a1\n(a,1)\u5f53\u524d\u5206\u533a0\n(you,1)\u5f53\u524d\u5206\u533a2\n(me,1)\u5f53\u524d\u5206\u533a2\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>RDD\u7684\u8f6c\u6362\u7b97\u5b50 \u70b9\u51fb\u67e5\u770bsparkrdd\u5b98\u65b9\u6587\u6863 \u7b97\u5b50\u540d\u79f0 \u5b98\u7f51\u89e3\u91ca \u4f5c\u7528 map(func) Return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.specialwu.com\/index.php?rest_route=\/wp\/v2\/posts\/1606"}],"collection":[{"href":"http:\/\/www.specialwu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.specialwu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.specialwu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.specialwu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1606"}],"version-history":[{"count":9,"href":"http:\/\/www.specialwu.com\/index.php?rest_route=\/wp\/v2\/posts\/1606\/revisions"}],"predecessor-version":[{"id":1616,"href":"http:\/\/www.specialwu.com\/index.php?rest_route=\/wp\/v2\/posts\/1606\/revisions\/1616"}],"wp:attachment":[{"href":"http:\/\/www.specialwu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.specialwu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1606"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.specialwu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}