{"id":3441,"date":"2021-07-26T07:01:03","date_gmt":"2021-07-26T07:01:03","guid":{"rendered":"https:\/\/www.bearloga.space\/?page_id=3441"},"modified":"2021-07-26T07:01:04","modified_gmt":"2021-07-26T07:01:04","slug":"pyspark-rdd","status":"publish","type":"page","link":"https:\/\/www.bearloga.space\/en\/pyspark-rdd\/","title":{"rendered":"PySpark \u2014 RDD"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043b\u0438 PySpark \u0432 \u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 Python \u0434\u043b\u044f Apache Spark.&nbsp;\u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0435\u0439 Spark \u2014 RDD.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u0414\u0420 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442&nbsp;<strong>Resilient Distributed Dataset<\/strong>&nbsp;, \u044d\u0442\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0443\u0437\u043b\u0430\u0445 \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0435.&nbsp;\u0421\u0414\u0420 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0421\u0414\u0420 \u0432\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c.&nbsp;\u0421\u0414\u0420 \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0442\u043a\u0430\u0437\u043e\u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u044b\u043c\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043b\u044e\u0431\u043e\u0433\u043e \u0441\u0431\u043e\u044f \u043e\u043d\u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.&nbsp;\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043a \u044d\u0442\u0438\u043c \u0421\u0414\u0420 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043a \u044d\u0442\u0438\u043c RDD, \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438<\/li><li>\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0432 \u0434\u0435\u0442\u0430\u043b\u044f\u0445.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/strong>&nbsp;\u2014 \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043a \u0421\u0414\u0420 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0421\u0414\u0420.&nbsp;Filter, groupBy \u0438 map \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/strong>&nbsp;\u2014 \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043a RDD, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0440\u0443\u0435\u0442 Spark \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0443.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0432 PySpark, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0442\u044c&nbsp;<strong>PyDpark RDD<\/strong>&nbsp;.&nbsp;\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 PyDpark RDD \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class pyspark.RDD (\n   jrdd, \n   ctx, \n   jrdd_deserializer = AutoBatchedSerializer(PickleSerializer())\n)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e PySpark.&nbsp;\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0432 \u0444\u0430\u0439\u043b\u0435 Python \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043b\u043e\u0432\u0430 RDD, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u043b\u043e\u0432.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">words = sc.parallelize (\n   [\"scala\", \n   \"java\", \n   \"hadoop\", \n   \"spark\", \n   \"akka\",\n   \"spark vs hadoop\", \n   \"pyspark\",\n   \"pyspark and spark\"]\n)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u0441\u043b\u043e\u0432\u0430\u043c\u0438.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u043a\u043e\u043b-()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0421\u0414\u0420 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------count.py---------------------------------------\nfrom pyspark import SparkContext\nsc = SparkContext(\"local\", \"count app\")\nwords = sc.parallelize (\n   [\"scala\", \n   \"java\", \n   \"hadoop\", \n   \"spark\", \n   \"akka\",\n   \"spark vs hadoop\", \n   \"pyspark\",\n   \"pyspark and spark\"]\n)\ncounts = words.count()\nprint \"Number of elements in RDD -&gt; %i\" % (counts)\n----------------------------------------count.py---------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041a\u043e\u043c\u0430\u043d\u0434\u0430<\/strong>&nbsp;\u2014 \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f count () \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit count.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Number of elements in RDD \u2192 8\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c ()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0421\u0414\u0420 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------collect.py---------------------------------------\nfrom pyspark import SparkContext\nsc = SparkContext(\"local\", \"Collect app\")\nwords = sc.parallelize (\n   [\"scala\", \n   \"java\", \n   \"hadoop\", \n   \"spark\", \n   \"akka\",\n   \"spark vs hadoop\", \n   \"pyspark\",\n   \"pyspark and spark\"]\n)\ncoll = words.collect()\nprint \"Elements in RDD -&gt; %s\" % (coll)\n----------------------------------------collect.py---------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command<\/strong>\u00a0\u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f collect () \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit collect.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Elements in RDD -&gt; [\n   'scala', \n   'java', \n   'hadoop', \n   'spark', \n   'akka', \n   'spark vs hadoop', \n   'pyspark', \n   'pyspark and spark'\n]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0415\u043e\u0433\u0435\u0430\u0441\u043f (\u0435)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 foreach.&nbsp;\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e print \u0432 foreach, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 RDD.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------foreach.py---------------------------------------\nfrom pyspark import SparkContext\nsc = SparkContext(\"local\", \"ForEach app\")\nwords = sc.parallelize (\n   [\"scala\", \n   \"java\", \n   \"hadoop\", \n   \"spark\", \n   \"akka\",\n   \"spark vs hadoop\", \n   \"pyspark\",\n   \"pyspark and spark\"]\n)\ndef f(x): print(x)\nfore = words.foreach(f) \n----------------------------------------foreach.py---------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041a\u043e\u043c\u0430\u043d\u0434\u0430<\/strong>&nbsp;\u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f foreach (f) \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit foreach.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">scala\njava\nhadoop\nspark\nakka\nspark vs hadoop\npyspark\npyspark and spark\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0444\u0438\u043b\u044c\u0442\u0440 (F)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 RDD, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430.&nbsp;\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u0438\u0441\u043a\u0440\u0443.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------filter.py---------------------------------------\nfrom pyspark import SparkContext\nsc = SparkContext(\"local\", \"Filter app\")\nwords = sc.parallelize (\n   [\"scala\", \n   \"java\", \n   \"hadoop\", \n   \"spark\", \n   \"akka\",\n   \"spark vs hadoop\", \n   \"pyspark\",\n   \"pyspark and spark\"]\n)\nwords_filter = words.filter(lambda x: 'spark' in x)\nfiltered = words_filter.collect()\nprint \"Fitered RDD -&gt; %s\" % (filtered)\n----------------------------------------filter.py----------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041a\u043e\u043c\u0430\u043d\u0434\u0430<\/strong>&nbsp;\u2014 \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430 (f) \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit filter.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Fitered RDD -&gt; [\n   'spark', \n   'spark vs hadoop', \n   'pyspark', \n   'pyspark and spark'\n]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u043a\u0430\u0440\u0442\u0430 (f, preservePartitioning = False)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u043e\u0432\u044b\u0439 RDD \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 RDD.&nbsp;\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------map.py---------------------------------------\nfrom pyspark import SparkContext\nsc = SparkContext(\"local\", \"Map app\")\nwords = sc.parallelize (\n   [\"scala\", \n   \"java\", \n   \"hadoop\", \n   \"spark\", \n   \"akka\",\n   \"spark vs hadoop\", \n   \"pyspark\",\n   \"pyspark and spark\"]\n)\nwords_map = words.map(lambda x: (x, 1))\nmapping = words_map.collect()\nprint \"Key value pair -&gt; %s\" % (mapping)\n----------------------------------------map.py---------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041a\u043e\u043c\u0430\u043d\u0434\u0430<\/strong>&nbsp;\u2014 \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f \u043a\u0430\u0440\u0442\u044b (f, preservePartitioning = False) \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit map.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Key value pair -&gt; [\n   ('scala', 1), \n   ('java', 1), \n   ('hadoop', 1), \n   ('spark', 1), \n   ('akka', 1), \n   ('spark vs hadoop', 1), \n   ('pyspark', 1), \n   ('pyspark and spark', 1)\n]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c (\u0435)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0438 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0421\u0414\u0420.&nbsp;\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u043a\u0435\u0442 add \u0438\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u0435\u0433\u043e \u043a \u2018num\u2019, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------reduce.py---------------------------------------\nfrom pyspark import SparkContext\nfrom operator import add\nsc = SparkContext(\"local\", \"Reduce app\")\nnums = sc.parallelize([1, 2, 3, 4, 5])\nadding = nums.reduce(add)\nprint \"Adding all the elements -&gt; %i\" % (adding)\n----------------------------------------reduce.py---------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041a\u043e\u043c\u0430\u043d\u0434\u0430<\/strong>&nbsp;\u2014 \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f (f) \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit reduce.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Adding all the elements -&gt; 15\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c\u0441\u044f (\u0434\u0440\u0443\u0433\u043e\u0435, numPartitions = \u041d\u0435\u0442)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 RDD \u0441 \u043f\u0430\u0440\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043a\u043b\u044e\u0447\u0430\u043c\u0438 \u0438 \u0432\u0441\u0435\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430.&nbsp;\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u043f\u0430\u0440\u044b \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0432\u0443\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 RDD.&nbsp;\u041f\u043e\u0441\u043b\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 RDD \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c RDD \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u0438\u043c\u0435\u044e\u0449\u0438\u043c\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0435 \u043a\u043b\u044e\u0447\u0438 \u0438 \u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------join.py---------------------------------------\nfrom pyspark import SparkContext\nsc = SparkContext(\"local\", \"Join app\")\nx = sc.parallelize([(\"spark\", 1), (\"hadoop\", 4)])\ny = sc.parallelize([(\"spark\", 2), (\"hadoop\", 5)])\njoined = x.join(y)\nfinal = joined.collect()\nprint \"Join RDD -&gt; %s\" % (final)\n----------------------------------------join.py---------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041a\u043e\u043c\u0430\u043d\u0434\u0430<\/strong>&nbsp;\u2014 \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f (\u0434\u0440\u0443\u0433\u043e\u0435, numPartitions = None) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit join.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Join RDD -&gt; [\n   ('spark', (1, 2)),  \n   ('hadoop', (4, 5))\n]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u041a\u044d\u0448 ()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u044d\u0442\u043e\u0442 RDD \u0441 \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (MEMORY_ONLY).&nbsp;\u0412\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043b\u0438 RDD \u0438\u043b\u0438 \u043d\u0435\u0442.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">----------------------------------------cache.py---------------------------------------\nfrom pyspark import SparkContext \nsc = SparkContext(\"local\", \"Cache app\") \nwords = sc.parallelize (\n   [\"scala\", \n   \"java\", \n   \"hadoop\", \n   \"spark\", \n   \"akka\",\n   \"spark vs hadoop\", \n   \"pyspark\",\n   \"pyspark and spark\"]\n) \nwords.cache() \ncaching = words.persist().is_cached \nprint \"Words got chached &gt; %s\" % (caching)\n----------------------------------------cache.py---------------------------------------\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u041a\u043e\u043c\u0430\u043d\u0434\u0430<\/strong>&nbsp;\u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f cache () \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$SPARK_HOME\/bin\/spark-submit cache.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong>&nbsp;\u2014 \u0432\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Words got cached -&gt; True\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u042d\u0442\u043e \u0431\u044b\u043b\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0432 PySpark RDD.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043b\u0438 PySpark \u0432 \u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 Python \u0434\u043b\u044f Apache Spark.&nbsp;\u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-3441","page","type-page","status-publish","hentry","category-pyspark"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages\/3441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/comments?post=3441"}],"version-history":[{"count":0,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages\/3441\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/media?parent=3441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/categories?post=3441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/tags?post=3441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}