{"id":378,"date":"2020-12-01T12:10:16","date_gmt":"2020-12-01T04:10:16","guid":{"rendered":"https:\/\/blog.frost-s.tk\/?p=378"},"modified":"2021-01-11T00:06:38","modified_gmt":"2021-01-10T16:06:38","slug":"spark-rdd","status":"publish","type":"post","link":"https:\/\/blog.frost-s.com\/index.php\/2020\/12\/01\/spark-rdd\/","title":{"rendered":"spark RDD"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>RDD<\/strong><strong>\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<p><strong>RDD<\/strong>\uff0c\u5168\u79f0\u4e3aResilient Distributed Datasets\uff08\u5f39\u6027\u5206\u5e03\u5f0f\u6570\u636e\u96c6\uff09\uff0c\u662f\u4e00\u4e2a<strong>\u5bb9\u9519\u7684<\/strong>\u3001<strong>\u5e76\u884c\u7684<\/strong>\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u663e\u5f0f\u5730\u5c06\u6570\u636e\u5b58\u50a8\u5230\u78c1\u76d8\u548c\u5185\u5b58\u4e2d\uff0c\u5e76\u80fd\u63a7\u5236\u6570\u636e\u7684\u5206\u533a\u3002\u540c\u65f6\uff0cRDD\u8fd8\u63d0\u4f9b\u4e86\u4e00\u7ec4\u4e30\u5bcc\u7684\u64cd\u4f5c\u6765\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u3002\u5728\u8fd9\u4e9b\u64cd\u4f5c\u4e2d\uff0c\u8bf8\u5982map\u3001flatMap\u3001filter\u7b49\u8f6c\u6362\u64cd\u4f5c\u5b9e\u73b0\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\u6a21\u5f0f\uff0c\u5f88\u597d\u5730\u5951\u5408\u4e86Scala\u7684\u96c6\u5408\u64cd\u4f5c\u3002\u9664\u6b64\u4e4b\u5916\uff0cRDD\u8fd8\u63d0\u4f9b\u4e86\u8bf8\u5982join\u3001groupBy\u3001reduceByKey\u7b49\u66f4\u4e3a\u65b9\u4fbf\u7684\u64cd\u4f5c\uff08\u6ce8\u610f\uff0creduceByKey\u662faction\uff0c\u800c\u975etransformation\uff09\uff0c\u4ee5\u652f\u6301\u5e38\u89c1\u7684\u6570\u636e\u8fd0\u7b97\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\u6765\u8bb2\uff0c\u9488\u5bf9\u6570\u636e\u5904\u7406\u6709\u51e0\u79cd\u5e38\u89c1\u6a21\u578b\uff0c\u5305\u62ec\uff1aIterative Algorithms\uff0cRelational Queries\uff0cMapReduce\uff0cStream Processing\u3002\u4f8b\u5982Hadoop MapReduce\u91c7\u7528\u4e86MapReduces\u6a21\u578b\uff0cStorm\u5219\u91c7\u7528\u4e86Stream Processing\u6a21\u578b\u3002RDD\u6df7\u5408\u4e86\u8fd9\u56db\u79cd\u6a21\u578b\uff0c\u4f7f\u5f97Spark\u53ef\u4ee5\u5e94\u7528\u4e8e\u5404\u79cd\u5927\u6570\u636e\u5904\u7406\u573a\u666f\u3002<\/p>\n\n\n\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a <strong>\u53ea\u8bfb<\/strong>\u7684\uff0c\u53ef\u5206\u533a\u7684\u5206\u5e03\u5f0f\u6570\u636e\u96c6\uff1b\u6570\u636e\u96c6\u53ef\u5168\u90e8\u6216\u90e8\u5206\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5728\u4e00\u4e2aApp\u591a\u6b21\u8ba1\u7b97\u95f4\u91cd\u7528\uff0c RDD\u662fSpark\u7684\u6838\u5fc3\u3002<\/p>\n\n\n\n<p><strong>\u8840\u7edf\u5bb9\u9519<\/strong>\uff1a\u6839\u636e\u8840\u7edf\uff08\u7236\u5b50\u95f4\u4f9d\u8d56\u5173\u7cfb\uff09\u91cd\u8ba1\u7b97\u6062\u590d\u4e22\u5931\u6570\u636e<\/p>\n\n\n\n<p><strong>RDD<\/strong><strong>\u64cd\u4f5c<\/strong>\uff1a Transformation\u7b97\u5b50\u548cAction\u7b97\u5b50\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"286\" src=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-51-761x286.png\" alt=\"\u8ba1\u7b97\u673a\u751f\u6210\u4e86\u53ef\u9009\u6587\u5b57:\n1\u81ea\u8fc7\u5ae9\uff1a\u6398\u7a7a\u80a9]\n\u300a[\u4e8cI[\u4e8cI[\u4e8cI[\u4e8c\" class=\"wp-image-410\"\/><\/figure>\n\n\n\n<p><strong>\u539f\u751f\u6570\u636e\u7a7a\u95f4\u8f6c<\/strong><strong>RDD<\/strong><\/p>\n\n\n\n<p>\u539f\u751f\u7684SCALA\u6570\u636e\u96c6\u5408\u53ef\u4ee5\u8f6c\u6362\u4e3aRDD\u8fdb\u884c\u64cd\u4f5c<\/p>\n\n\n\n<p>\u5305\u542b\u4e00\u4e0b\u4e24\u79cd\u65b9\u5f0f<\/p>\n\n\n\n<p>makeRDD<\/p>\n\n\n\n<p>parallelize<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"871\" height=\"287\" src=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-52-871x287.png\" alt=\"\u8ba1\u7b97\u673a\u751f\u6210\u4e86\u53ef\u9009\u6587\u5b57:\nObjectScalaToRdd{\ndefmain(args:Array[String]):Unit=\nvalconf=newSparkConf()\nconf.setMaster(&quot;local&quot;\uff09\nconf.setAppName(&quot;test&quot;)\nvalsc=newSparkContext()onf)\nprint\/n(sc.makeRDD(1\uff0cto\uff08100)).sum0)\nsc.stop()\" class=\"wp-image-411\" srcset=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-52.png 871w, https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-52-768x253.png 768w\" sizes=\"(max-width: 871px) 100vw, 871px\" \/><\/figure>\n\n\n\n<p><strong>\u5b58\u50a8\u6587\u4ef6\u8f6c<\/strong><strong>RDD<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"915\" height=\"390\" src=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-53-915x390.png\" alt=\"\u8ba1\u7b97\u673a\u751f\u6210\u4e86\u53ef\u9009\u6587\u5b57:\nobjectFileToRdd{\ndefmain(args:Array[String]):Unit\nvalconf=newSparkConfO\nconf.setMaster(&quot;IocaI\uff09\nconf.setAppName(&quot;test&quot;)\nvalsc=newSparkContext(conf)\nyaLtext=sctextfiIe(&quot;hdfs:\/\/master:8020\/tmp\/scmlog&quot;)\ntext,flatMap(_.split(&quot;&quot;)).map(k=&gt;(k,l)).reduceByKey(_+_);\nsc.stop()\" class=\"wp-image-412\" srcset=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-53.png 915w, https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-53-768x327.png 768w\" sizes=\"(max-width: 915px) 100vw, 915px\" \/><\/figure>\n\n\n\n<p><strong>Partition<\/strong><strong>\uff08\u5206\u533a\uff09<\/strong><\/p>\n\n\n\n<p>\u4e00\u4efd\u5f85\u5904\u7406\u7684\u539f\u59cb\u6570\u636e\u4f1a\u88ab\u6309\u7167\u76f8\u5e94\u7684\u903b\u8f91\u5207\u5206\u6210n\u4efd\uff0c\u6bcf\u4efd\u6570\u636e\u5bf9\u5e94\u5230RDD\u4e2d\u7684\u4e00\u4e2aPartition\uff0cPartition\u7684\u6570\u91cf\u51b3\u5b9a\u4e86task\u7684\u6570\u91cf\uff0c\u5f71\u54cd\u7740\u7a0b\u5e8f\u7684\u5e76\u884c\u5ea6\uff0c\u6240\u4ee5\u7406\u89e3Partition\u662f\u4e86\u89e3spark\u80cc\u540e\u8fd0\u884c\u539f\u7406\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n\n\n\n<p>\u5206\u533a\u6570\u7684\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<ol type=\"1\"><li>\u5728local\u6a21\u5f0f\u4e0b\u901a\u8fc7\u8bbe\u7f6elocal[*]\uff0c\u8bbe\u7f6e\u5206\u533a\u6570<\/li><\/ol>\n\n\n\n<p>local[2]:2\u4e2a<\/p>\n\n\n\n<p>local[*]\uff1a\u62ff\u5230\u5f53\u524dCPU\u7684\u5185\u6838\u6570\uff0c\u6bd4\u5982\u662f\u53cc\u6838\u7684\u5c31\u662f2&nbsp; 4\u6838\u5c31\u662f4<\/p>\n\n\n\n<ol type=\"1\"><li>\u901a\u8fc7\u81ea\u5b9a\u4e49\u5206\u533a\u6570<\/li><li>\u901a\u8fc7\u8bbe\u7f6e\u521d\u59cb\u5316\u5206\u533a\u6570<\/li><\/ol>\n\n\n\n<p>sc.makeRDD(1 to 1000,5)<\/p>\n\n\n\n<ol type=\"1\"><li>\u53ef\u901a\u8fc7\u7b97\u5b50\u6765\u8fdb\u884c\u4fee\u6539\u5206\u533a\u6570.repartition(3)<\/li><li>\u5982\u679c\u4f7f\u7528\u7684\u662fscala\u96c6\u5408\u7684\u8bdd\uff0c\u5728\u7279\u5b9a\u7684\u683c\u5f0f\u4e0b\uff0c\u4f1a\u6839\u636e\u6570\u91cf\u91cf\u6765\u521b\u5efa\u5206\u533amakeRdd<\/li><li>\u8bfb\u53d6HDFS\u4e0a\u7684\u6570\u636e\u65f6\u6839\u636e\u5757\u7684\u6570\u91cf\u6765\u5212\u5206\u5206\u533a\u6570<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"539\" height=\"224\" src=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-54.png\" alt=\"\u8ba1\u7b97\u673a\u751f\u6210\u4e86\u53ef\u9009\u6587\u5b57:\nvalunit:sc.parallelize(1to100\uff0c20\uff09\n\uff0c\u5915\u5b58\u522d\u5206\u533a\n\/\u4e61\u60ebni\u5c81ar000s.size\u4e00\u4e00\u53e6\u4e00\u4e00\u5e90\u5c392\u58c5\u300a\u533a\u7131\uff0e\nvarnpO1=unit.getNumPartitIons\" class=\"wp-image-413\"\/><\/figure>\n\n\n\n<p><strong>Spark<\/strong><strong>\u6838\u5fc3\u6982\u5ff5 <\/strong><strong>\u2013 <\/strong><strong>\u5bbd\u4f9d\u8d56\u548c\u7a84\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p>RDD\u7236\u5b50\u4f9d\u8d56\u5173\u7cfb\uff1a\u7a84\uff08 Narrow\uff09\u4f9d\u8d56\u548c\u5bbd\uff08 Wide\uff09\u4f9d\u8d56\u3002<\/p>\n\n\n\n<p><strong>\u7a84\u4f9d\u8d56<\/strong>:\u6307\u7236RDD\u7684\u6bcf\u4e00\u4e2a\u5206\u533a\u6700\u591a\u88ab\u4e00\u4e2a\u5b50RDD\u7684\u5206\u533a\u6240\u7528\u3002<\/p>\n\n\n\n<p><strong>\u5bbd\u4f9d\u8d56<\/strong>:\u6307\u5b50RDD\u7684\u5206\u533a\u4f9d\u8d56\u4e8e\u7236RDD\u7684\u6240\u6709\u5206\u533a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1169\" height=\"369\" src=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-55.png\" alt=\"\u8ba1\u7b97\u673a\u751f\u6210\u4e86\u53ef\u9009\u6587\u5b57:\nNarrowDependencies\uff1a\nmap\uff0cfilter\njoin\u201di\u5c71input\u3001\nco-partitioned\nWideDependencies\uff1a\ngroupByKey\njoinwithinputsnot\nco-partitioned\ngroupBy\nmap\n,Stage2\n.J01n\nStage3\" class=\"wp-image-414\" srcset=\"https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-55.png 1169w, https:\/\/blog.frost-s.com\/wp-content\/uploads\/2020\/12\/image-55-768x242.png 768w\" sizes=\"(max-width: 1169px) 100vw, 1169px\" \/><\/figure>\n\n\n\n<p><strong>Stage:<\/strong><\/p>\n\n\n\n<p>\u4e00\u4e2aJob\u4f1a\u88ab\u62c6\u5206\u4e3a\u591a\u7ec4Task\uff0c\u6bcf\u7ec4\u4efb\u52a1\u88ab\u79f0\u4e3a\u4e00\u4e2aStage\u5c31\u50cfMap Stage\uff0c Reduce Stage\u3002Stage\u7684\u5212\u5206\u5728RDD\u7684\u8bba\u6587\u4e2d\u6709\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u7b80\u5355\u7684\u8bf4\u662f\u4ee5shuffle\u548cresult\u8fd9\u4e24\u79cd\u7c7b\u578b\u6765\u5212\u5206\u3002\u5728Spark\u4e2d\u6709\u4e24\u7c7btask\uff0c\u4e00\u7c7b\u662fshuffleMapTask\uff0c\u4e00\u7c7b\u662fresultTask\uff0c\u7b2c\u4e00\u7c7btask\u7684\u8f93\u51fa\u662fshuffle\u6240\u9700\u6570\u636e\uff0c\u7b2c\u4e8c\u7c7btask\u7684\u8f93\u51fa\u662fresult\uff0cstage\u7684\u5212\u5206\u4e5f\u4ee5\u6b64\u4e3a\u4f9d\u636e\uff0cshuffle\u4e4b\u524d\u7684\u6240\u6709\u53d8\u6362\u662f\u4e00\u4e2astage\uff0cshuffle\u4e4b\u540e\u7684\u64cd\u4f5c\u662f\u53e6\u4e00\u4e2astage\u3002\u6bd4\u5982 rdd.parallize(1 to 10).foreach(println) \u8fd9\u4e2a\u64cd\u4f5c\u6ca1\u6709shuffle\uff0c\u76f4\u63a5\u5c31\u8f93\u51fa\u4e86\uff0c\u90a3\u4e48\u53ea\u6709\u5b83\u7684task\u662fresultTask\uff0cstage\u4e5f\u53ea\u6709\u4e00\u4e2a\uff1b\u5982\u679c\u662frdd.map(x =&gt; (x, 1)).reduceByKey(_ + _).foreach(println), \u8fd9\u4e2ajob\u56e0\u4e3a\u6709reduce\uff0c\u6240\u4ee5\u6709\u4e00\u4e2ashuffle\u8fc7\u7a0b\uff0c\u90a3\u4e48reduceByKey\u4e4b\u524d\u7684\u662f\u4e00\u4e2astage\uff0c\u6267\u884cshuffleMapTask\uff0c\u8f93\u51fashuffle\u6240\u9700\u7684\u6570\u636e\uff0creduceByKey\u5230\u6700\u540e\u662f\u4e00\u4e2astage\uff0c\u76f4\u63a5\u5c31\u8f93\u51fa\u7ed3\u679c\u4e86\u3002\u5982\u679cjob\u4e2d\u6709\u591a\u6b21shuffle\uff0c\u90a3\u4e48\u6bcf\u4e2ashuffle\u4e4b\u524d\u90fd\u662f\u4e00\u4e2astage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RDD\u7b80\u4ecb RDD\uff0c\u5168\u79f0\u4e3aResilient Distributed Datasets\uff08\u5f39\u6027\u5206\u5e03\u5f0f\u6570\u636e\u96c6\uff09\uff0c\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":457,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,19],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/378"}],"collection":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":1,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/378\/revisions\/416"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media\/457"}],"wp:attachment":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}