{"id":404607,"date":"2024-06-29T17:48:16","date_gmt":"2024-06-29T17:48:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=404607"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=404607","title":{"rendered":"<span>\u041e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e Spark DataFrame<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0432\u0438\u0434\u0443 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0441\u043b\u043e\u0436\u043d\u043e \u0438\u043b\u0438 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 <em>Pandas<\/em> \u043d\u0430 <em>Python<\/em>. \u0420\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 <em>Spark<\/em> \u0438 \u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 <em>DataFrame<\/em>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a60\/7ad\/c29\/a607adc29df856fe7a2ed9fba684929a.png\" width=\"623\" height=\"342\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a60\/7ad\/c29\/a607adc29df856fe7a2ed9fba684929a.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <em>Spark DataFrame<\/em>. \u041a\u043e\u0441\u043d\u0435\u043c\u0441\u044f \u0442\u0430\u043a\u0438\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u0430\u043a: \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <em>DataFrame<\/em> \u0438\u0437 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0438 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043d\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0438\u043b\u0438 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e Spark \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434 \u043d\u0430 \u0442\u0440\u0435\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: <em>Scala<\/em>, <em>Java<\/em> \u0438 <em>Python<\/em>. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 <em>Python<\/em>. \u041c\u044b \u043d\u0435 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u0435\u043c \u043c\u043d\u043e\u0433\u0438\u0445 \u043d\u044e\u0430\u043d\u0441\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0413\u0430\u0439\u0434\u0435 (<a href=\"https:\/\/spark.apache.org\/docs\/latest\/sql-programming-guide.html\" rel=\"noopener noreferrer nofollow\">https:\/\/spark.apache.org\/docs\/latest\/sql-programming-guide.html<\/a> ) <\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 DataFrame<\/h3>\n<p>Spark \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c DataFrame \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438:<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 \u0444\u0430\u0439\u043b\u0430 (JSON, CSV, ORC, Parquet \u0438 \u0434\u0440.);<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 RDD;<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 <em>HDFS<\/em>;<\/p>\n<p>4.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 <em>pandas DF<\/em>.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <em>DF<\/em> \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 DF \u0438\u0437 \u0444\u0430\u0439\u043b\u0430<\/h4>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 <em>CSV<\/em> \u0444\u0430\u0439\u043b\u0430. <\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0438\u043c\u043f\u043e\u0440\u0442 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code>from pyspark.sql.types import StructType, StructField, IntegerType, StringType  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0445\u0435\u043c\u0443: schema = StructType(fields=[     StructField(\"col_1\", IntegerType()),     StructField(\"col_2\", StringType ()) ]) \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c DF: df = spark.read.csv(\"\/path\/file_name\", schema=schema, sep=\";\")<\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u0444\u0430\u0439\u043b \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0432\u043e\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u0441\u043c\u0435\u0448\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 <em>DataFrame<\/em> \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b\u0438\u0441\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440: <em>inferSchema=True<\/em>.<\/p>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c: <em>header=True<\/em>.<\/p>\n<p>\u0421\u043e\u0431\u0438\u0440\u0430\u044f \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f <em>DF<\/em>:<\/p>\n<p><code>df = spark.read.csv(\"\/path\/file_name\", sep=\";\", inferSchema=True, header=True.)<\/code><\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0435\u0434\u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 DataFrame \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 show(5, vertical=True, truncate=False)<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 take()<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 collect()<\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code>df.show()<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/99b\/8c1\/90f\/99b8c190fe6c5701a36c7712887084b1.png\" width=\"113\" height=\"149\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/99b\/8c1\/90f\/99b8c190fe6c5701a36c7712887084b1.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 DataFrame \u0438\u0437 RDD \u0438 Pandas DataFrame<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435:<\/p>\n<p>df = spark.createDataFrame(rdd)<\/p>\n<p>df = spark.createDataFrame(pandasDF)<\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 schema<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b HDFS<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <em>Spark DF<\/em> \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446 <em>HDFS<\/em> \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e 2-\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0444\u043e\u0440\u043c\u043e\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f (\u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u0430):<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Pyspark \u0437\u0430\u043f\u0440\u043e\u0441;<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SQL \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>Pyspark API:<\/p>\n<pre><code>df = spark.table(\u2018schema_name.table_name\u2019)  SQL API: df = spark.sql(\u201c\u201d\u201d SELECT col_1, col_2 FROM schema_name.table_name \u201d\u201d\u201d)<\/code><\/pre>\n<h3>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 DataFrame<\/h3>\n<p><em>Spark DataFrame<\/em> \u0438\u043c\u0435\u0435\u0442 \u043e\u0431\u0448\u0438\u0440\u043d\u044b\u0439 (\u0445\u043e\u0442\u044c \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043a\u0440\u043e\u043c\u043d\u044b\u0439 \u0447\u0435\u043c <em>Pandas<\/em>) \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b. \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439:<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0432\u044b\u0431\u043e\u0440 \u043a\u043e\u043b\u043e\u043d\u043e\u043a,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430.<\/p>\n<p>\u00a0<\/p>\n<p>\u0414\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c \u0438\u043c\u043f\u043e\u0440\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<p><code>from pyspark.sql.functions import col, asc, desc, lenght<\/code><\/p>\n<h4>\u0412\u044b\u0431\u043e\u0440 \u043d\u0443\u0436\u043d\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a<\/h4>\n<p>\u0412\u044b\u0431\u0435\u0440\u0435\u043c \u0438\u0437 <em>df<\/em> \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c <em>\u201ccol_1\u201d<\/em> \u0438 <em>\u201ccol_2\u201d<\/em>:<\/p>\n<p><code>df2 = df.select(col(\u201ccol_2\u201d))<\/code><\/p>\n<p><code>df2.show()<\/code><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6c8\/18d\/6ca\/6c818d6caf7b035a92dbaeabba452619.png\" width=\"73\" height=\"162\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6c8\/18d\/6ca\/6c818d6caf7b035a92dbaeabba452619.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u041f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h4>\n<p>\u041f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u0443\u0435\u043c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 <em>\u201dcol_2\u201d<\/em> \u043d\u0430 <em>\u201cnew_col\u201d<\/em>:<\/p>\n<p><code>df2 = df.select(col(\u201ccol_1\u201d), col(\u201ccol_2\u201d).alias(\u201cnew_col\u201d))<\/code><\/p>\n<p><code>df2.show()<\/code><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/38d\/684\/02b\/38d68402b506bd43348d6d3b389c7e1e.png\" width=\"115\" height=\"161\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/38d\/684\/02b\/38d68402b506bd43348d6d3b389c7e1e.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f<\/h4>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b <em>where<\/em> \u0438\u043b\u0438 <em>filter<\/em>:<\/p>\n<p><code>df2 = df.filter(col(\u201ccol_1\u201d) > 5)<\/code><\/p>\n<p><code>df2.show()<\/code><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d93\/3a1\/16d\/d933a116d9678ab5bd0f4fec1808050f.png\" width=\"115\" height=\"94\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d93\/3a1\/16d\/d933a116d9678ab5bd0f4fec1808050f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b: \u0438 (&amp;), \u0438\u043b\u0438 (|), \u043d\u0435 (~) \u0438 \u0434\u0440.<\/p>\n<h4>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h4>\n<p>\u041e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e \u0434\u0432\u0443\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c, \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u043c\u0443 \u2013 \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e, \u043f\u043e \u0432\u0442\u043e\u0440\u043e\u043c\u0443 \u2013 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e:<\/p>\n<p><code>df2 = df.orderBy(col(\"col_1\").desc(), col(\"col_2\").asc())<\/code><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/30c\/637\/28a\/30c63728a140b9aa49681cca321f3497.png\" width=\"103\" height=\"144\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/30c\/637\/28a\/30c63728a140b9aa49681cca321f3497.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 <em>\u201ccol_2\u201d<\/em>:<\/p>\n<p><code>df2 = df.withColumn(\u201cnew_col\u201d, length(\u201ccol_2\u201d))<\/code><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/76d\/e9f\/2ac\/76de9f2ac519d6a00428f5ebbc3bf65c.png\" width=\"151\" height=\"138\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/76d\/e9f\/2ac\/76de9f2ac519d6a00428f5ebbc3bf65c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043a <em>RDD<\/em>.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043b\u043e\u0436\u0438\u043c <em>\u201ccol_1\u201d<\/em> \u0441 \u0434\u043b\u0438\u043d\u043e\u0439 <em>\u201ccol_2\u201d<\/em>. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code>def summator(a_int, b_str): return a_int + len(b_str)  rdd = df.rdd.map(lambda x: x[0], summator(x[0], x[1],)) df2 = rdd.toDF([\u201ccol_1\u201d, \u201dsumma\u201d]) <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/670\/9a8\/234\/6709a82340d40217991a9109800f60ef.png\" width=\"140\" height=\"141\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/670\/9a8\/234\/6709a82340d40217991a9109800f60ef.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446<\/h4>\n<p>\u0414\u043b\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <em>join<\/em>. \u0421\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b <em>df<\/em> \u0438 <em>df2<\/em> (\u0438\u0437 \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430), \u043a\u043b\u044e\u0447\u043e\u043c \u0434\u043b\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u2013 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435 <em>\u201ccol_1\u201d<\/em>.<\/p>\n<p><code>df_rez = df.join(df2, df.col_1 == df2.col_1, how=\u201dinner\u201d)<\/code><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7cc\/a2c\/fcc\/7cca2cfcc6a8538246c1d8b177ed12c3.png\" width=\"230\" height=\"138\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7cc\/a2c\/fcc\/7cca2cfcc6a8538246c1d8b177ed12c3.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c Spark DF \u0432 Pandas DataFrame, \u0430 \u0437\u0430\u0442\u0435\u043c \u0443\u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043d\u0443\u0436\u043d\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0435:<\/p>\n<p><code>pandas_df = df.toPandas()<\/code><\/p>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043b\u0438\u0431\u043e \u0432 \u0444\u0430\u0439\u043b \u043d\u0430 HDFS, \u043b\u0438\u0431\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 Parquet \u0438\u043b\u0438 ORC.<\/p>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 ORC:<\/p>\n<pre><code>t_f.write\\ .mode(\"overwrite\")\\ .format(\"orc\")\\ .saveAsTable(\u2018schema_name.table_name\u2019)  \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u0444\u0430\u0439\u043b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 csv: df.coalesce(1)\\ .write.format(\"com.databricks.spark.csv\")\\ .mode(\"append\")\\ .option(\"header\", \"true\")\\ .option(\"delimiter\", \"~\")\\ .option(\"quoteMode\", \"false\")\\ .save(\"path\")<\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041c\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 \u043e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e <em>Spark DataFrame<\/em>, \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043e\u0442\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 \u0441\u043e\u0431\u043e\u0439 \u0434\u043b\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code>df_final = df.select(col(\"col_1\"), col(\"col_2\").alias(\"new_col\"))\\ .join(df2.select(col(\"col_1\"), col(\"summa\")), df.col_1 == df2.col_1, how=\"inner\").select(df.col_1, col(\"new_col\"), df2.summa)<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9cb\/b3e\/7a0\/9cbb3e7a035231dfa008bedecff20d73.png\" width=\"154\" height=\"152\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9cb\/b3e\/7a0\/9cbb3e7a035231dfa008bedecff20d73.png\"\/><figcaption><\/figcaption><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/663690\/\"> https:\/\/habr.com\/ru\/articles\/663690\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0432\u0438\u0434\u0443 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0441\u043b\u043e\u0436\u043d\u043e \u0438\u043b\u0438 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 <em>Pandas<\/em> \u043d\u0430 <em>Python<\/em>. \u0420\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 <em>Spark<\/em> \u0438 \u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 <em>DataFrame<\/em>.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <em>Spark DataFrame<\/em>. \u041a\u043e\u0441\u043d\u0435\u043c\u0441\u044f \u0442\u0430\u043a\u0438\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u0430\u043a: \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <em>DataFrame<\/em> \u0438\u0437 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0438 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043d\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0438\u043b\u0438 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e Spark \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434 \u043d\u0430 \u0442\u0440\u0435\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: <em>Scala<\/em>, <em>Java<\/em> \u0438 <em>Python<\/em>. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 <em>Python<\/em>. \u041c\u044b \u043d\u0435 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u0435\u043c \u043c\u043d\u043e\u0433\u0438\u0445 \u043d\u044e\u0430\u043d\u0441\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0413\u0430\u0439\u0434\u0435 (<a href=\"https:\/\/spark.apache.org\/docs\/latest\/sql-programming-guide.html\" rel=\"noopener noreferrer nofollow\">https:\/\/spark.apache.org\/docs\/latest\/sql-programming-guide.html<\/a> ) <\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 DataFrame<\/h3>\n<p>Spark \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c DataFrame \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438:<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 \u0444\u0430\u0439\u043b\u0430 (JSON, CSV, ORC, Parquet \u0438 \u0434\u0440.);<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 RDD;<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 <em>HDFS<\/em>;<\/p>\n<p>4.\u00a0\u00a0\u00a0\u00a0\u00a0 \u0418\u0437 <em>pandas DF<\/em>.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <em>DF<\/em> \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 DF \u0438\u0437 \u0444\u0430\u0439\u043b\u0430<\/h4>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 <em>CSV<\/em> \u0444\u0430\u0439\u043b\u0430. <\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0438\u043c\u043f\u043e\u0440\u0442 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code>from pyspark.sql.types import StructType, StructField, IntegerType, StringType  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0445\u0435\u043c\u0443: schema = StructType(fields=[     StructField(\"col_1\", IntegerType()),     StructField(\"col_2\", StringType ()) ]) \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c DF: df = spark.read.csv(\"\/path\/file_name\", schema=schema, sep=\";\")<\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u0444\u0430\u0439\u043b \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0432\u043e\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u0441\u043c\u0435\u0448\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 <em>DataFrame<\/em> \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b\u0438\u0441\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440: <em>inferSchema=True<\/em>.<\/p>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c: <em>header=True<\/em>.<\/p>\n<p>\u0421\u043e\u0431\u0438\u0440\u0430\u044f \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f <em>DF<\/em>:<\/p>\n<p><code>df = spark.read.csv(\"\/path\/file_name\", sep=\";\", inferSchema=True, header=True.)<\/code><\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0435\u0434\u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 DataFrame \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 show(5, vertical=True, truncate=False)<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 take()<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 collect()<\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code>df.show()<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 DataFrame \u0438\u0437 RDD \u0438 Pandas DataFrame<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435:<\/p>\n<p>df = spark.createDataFrame(rdd)<\/p>\n<p>df = spark.createDataFrame(pandasDF)<\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 schema<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b HDFS<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <em>Spark DF<\/em> \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446 <em>HDFS<\/em> \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e 2-\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0444\u043e\u0440\u043c\u043e\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f (\u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u0430):<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Pyspark \u0437\u0430\u043f\u0440\u043e\u0441;<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SQL \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>Pyspark API:<\/p>\n<pre><code>df = spark.table(\u2018schema_name.table_name\u2019)  SQL API: df = spark.sql(\u201c\u201d\u201d SELECT col_1, col_2 FROM schema_name.table_name \u201d\u201d\u201d)<\/code><\/pre>\n<h3>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 DataFrame<\/h3>\n<p><em>Spark DataFrame<\/em> \u0438\u043c\u0435\u0435\u0442 \u043e\u0431\u0448\u0438\u0440\u043d\u044b\u0439 (\u0445\u043e\u0442\u044c \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043a\u0440\u043e\u043c\u043d\u044b\u0439 \u0447\u0435\u043c <em>Pandas<\/em>) \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b. \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439:<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0432\u044b\u0431\u043e\u0440 \u043a\u043e\u043b\u043e\u043d\u043e\u043a,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446,<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430.<\/p>\n<p>\u00a0<\/p>\n<p>\u0414\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c \u0438\u043c\u043f\u043e\u0440\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<p><code>from pyspark.sql.functions import col, asc, desc, lenght<\/code><\/p>\n<h4>\u0412\u044b\u0431\u043e\u0440 \u043d\u0443\u0436\u043d\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a<\/h4>\n<p>\u0412\u044b\u0431\u0435\u0440\u0435\u043c \u0438\u0437 <em>df<\/em> \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c <em>\u201ccol_1\u201d<\/em> \u0438 <em>\u201ccol_2\u201d<\/em>:<\/p>\n<p><code>df2 = df.select(col(\u201ccol_2\u201d))<\/code><\/p>\n<p><code>df2.show()<\/code><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h4>\u041f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h4>\n<p>\u041f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u0443\u0435\u043c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 <em>\u201dcol_2\u201d<\/em> \u043d\u0430 <em>\u201cnew_col\u201d<\/em>:<\/p>\n<p><code>df2 = df.select(col(\u201ccol_1\u201d), col(\u201ccol_2\u201d).alias(\u201cnew_col\u201d))<\/code><\/p>\n<p><code>df2.show()<\/code><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h4>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f<\/h4>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b <em>where<\/em> \u0438\u043b\u0438 <em>filter<\/em>:<\/p>\n<p><code>df2 = df.filter(col(\u201ccol_1\u201d) > 5)<\/code><\/p>\n<p><code>df2.show()<\/code><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b: \u0438 (&amp;), \u0438\u043b\u0438 (|), \u043d\u0435 (~) \u0438 \u0434\u0440.<\/p>\n<h4>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h4>\n<p>\u041e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e \u0434\u0432\u0443\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c, \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u043c\u0443 \u2013 \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e, \u043f\u043e \u0432\u0442\u043e\u0440\u043e\u043c\u0443 \u2013 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e:<\/p>\n<p><code>df2 = df.orderBy(col(\"col_1\").desc(), col(\"col_2\").asc())<\/code><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 <em>\u201ccol_2\u201d<\/em>:<\/p>\n<p><code>df2 = df.withColumn(\u201cnew_col\u201d, length(\u201ccol_2\u201d))<\/code><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043a <em>RDD<\/em>.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043b\u043e\u0436\u0438\u043c <em>\u201ccol_1\u201d<\/em> \u0441 \u0434\u043b\u0438\u043d\u043e\u0439 <em>\u201ccol_2\u201d<\/em>. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code>def summator(a_int, b_str): return a_int + len(b_str)  rdd = df.rdd.map(lambda x: x[0], summator(x[0], x[1],)) df2 = rdd.toDF([\u201ccol_1\u201d, \u201dsumma\u201d]) <\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446<\/h4>\n<p>\u0414\u043b\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <em>join<\/em>. \u0421\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b <em>df<\/em> \u0438 <em>df2<\/em> (\u0438\u0437 \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430), \u043a\u043b\u044e\u0447\u043e\u043c \u0434\u043b\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u2013 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435 <em>\u201ccol_1\u201d<\/em>.<\/p>\n<p><code>df_rez = df.join(df2, df.col_1 == df2.col_1, how=\u201dinner\u201d)<\/code><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h4>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c Spark DF \u0432 Pandas DataFrame, \u0430 \u0437\u0430\u0442\u0435\u043c \u0443\u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043d\u0443\u0436\u043d\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0435:<\/p>\n<p><code>pandas_df = df.toPandas()<\/code><\/p>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043b\u0438\u0431\u043e \u0432 \u0444\u0430\u0439\u043b \u043d\u0430 HDFS, \u043b\u0438\u0431\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 Parquet \u0438\u043b\u0438 ORC.<\/p>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 ORC:<\/p>\n<pre><code>t_f.write\\ .mode(\"overwrite\")\\ .format(\"orc\")\\ .saveAsTable(\u2018schema_name.table_name\u2019)  \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u0444\u0430\u0439\u043b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 csv: df.coalesce(1)\\ .write.format(\"com.databricks.spark.csv\")\\ .mode(\"append\")\\ .option(\"header\", \"true\")\\ .option(\"delimiter\", \"~\")\\ .option(\"quoteMode\", \"false\")\\ .save(\"path\")<\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041c\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 \u043e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e <em>Spark DataFrame<\/em>, \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043e\u0442\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 \u0441\u043e\u0431\u043e\u0439 \u0434\u043b\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code>df_final = df.select(col(\"col_1\"), col(\"col_2\").alias(\"new_col\"))\\ .join(df2.select(col(\"col_1\"), col(\"summa\")), df.col_1 == df2.col_1, how=\"inner\").select(df.col_1, col(\"new_col\"), df2.summa)<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/663690\/\"> https:\/\/habr.com\/ru\/articles\/663690\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-404607","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/404607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404607"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/404607\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}