{"id":271786,"date":"2016-01-08T20:21:02","date_gmt":"2016-01-08T17:21:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=271786"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=271786","title":{"rendered":"Spark local mode: \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435"},"content":{"rendered":"<p>       <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/e89\/c5c\/790\/e89c5c79016441a193d6f5e1eb70b059.jpg\" alt=\"image\"\/><br \/>  \u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<br \/>  4 \u044f\u043d\u0432\u0430\u0440\u044f \u0432\u044b\u0448\u043b\u0430 \u043d\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <a href=\"http:\/\/spark.apache.org\/releases\/spark-release-1-6-0.html\">Apache Spark 1.6<\/a> \u0441 <s>bug fix<\/s> \u043d\u043e\u0432\u044b\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430 \u0425\u0430\u0431\u0440\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0435\u043c\u0430\u043b\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u043e\u0442 <a href=\"http:\/\/habrahabr.ru\/company\/mlclass\/blog\/250811\/\">\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f<\/a> \u0434\u043e <a href=\"http:\/\/habrahabr.ru\/company\/bitrix\/blog\/273279\/\">\u043e\u043f\u044b\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445<\/a>. Spark \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c \u0438 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0434\u0430\u0436\u0435 \u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 Spark \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0433\u0440\u0435\u0445 \u043d\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u043d\u0430 \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435 \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 (\u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430) \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044b\u0447\u043d\u044b\u0445 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0434\u0430\u0436\u0435 \u043d\u0435\u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e. \u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 iPython (<a href=\"http:\/\/jupyter.readthedocs.org\/en\/latest\/install.html\">Jupyter<\/a>) notebook \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0435 \u043e\u0442\u0447\u0435\u0442\u044b. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0444\u0430\u0439\u043b\u0430, \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 Python \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/apache\/spark\/tree\/master\/examples\/src\/main\/python\">\u0442\u0443\u0442<\/a>.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435: \u0444\u0430\u0439\u043b(-\u044b) \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e GB \u0441 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043d\u043e\u0443\u0442\u0431\u0443\u043a \u0441\u043e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 RAM &lt; 1GB. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL- \u0438\u043b\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0444\u0430\u0439\u043b\u0430\u043c. \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0444\u0430\u0439\u043b\u0430\u0445 \u043b\u0435\u0436\u0438\u0442 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0437\u0430 \u043c\u0435\u0441\u044f\u0446 (\u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u0430\u0445 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438):<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/cf0\/92e\/a83\/cf092ea838a34bd097cccd0913cc180e.jpg\" alt=\"image\"\/><\/p>\n<p>  \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u043b\u043e\u0432 \u0432 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 \u0441\u043b\u043e\u0432\u043e \u00ab\u043d\u0435\u0434\u0432\u0438\u0436\u0438\u043c\u043e\u0441\u0442\u044c\u00bb. \u0422. \u0435. \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043b\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u0435\u043c \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0441\u043b\u043e\u0432 \u0438 \u0441\u0442\u0440\u043e\u0438\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/70a\/cea\/7d2\/70acea7d2e86476599aa2a353a528758.jpg\" alt=\"image\"\/><\/p>\n<p>  \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 Spark \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0430 \u0434\u043b\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c \u0438 \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c:<br \/>  1. <a href=\"http:\/\/spark.apache.org\/downloads.html\">\u0421\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c Spark<\/a> (\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u0432\u0435\u0440\u0441\u0438\u0438 1.6) \u0438 \u0440\u0430\u0437\u0430\u0440\u0445\u0438\u0432\u0438\u0440\u0443\u0435\u043c \u0432 \u043b\u044e\u0431\u0443\u044e \u043f\u0430\u043f\u043a\u0443.<\/p>\n<p>  2. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 Java (\u0435\u0441\u043b\u0438 \u043d\u0435\u0442)<br \/>   \u2014 \u0434\u043b\u044f Windows \u0438 MAC \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c 7 \u0432\u0435\u0440\u0441\u0438\u044e \u0441 java.com<br \/>   \u2014 \u0434\u043b\u044f Linux: $ sudo apt-get update \u0438 $ sudo apt-get install openjdk-7-jdk + \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0432 .bashrc \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 JAVA: JAVA_HOME=&quot;\/usr\/lib\/jvm\/java-7-openjdk-i386&quot;<br \/>  \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 Python, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <a href=\"https:\/\/www.continuum.io\/downloads\">Anaconda<\/a>.<\/p>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c pySpark (\u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c spark-shell \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0432 Scala \u043a\u0430\u043a \u043d\u0430 \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435): \u0437\u0430\u0445\u043e\u0434\u0438\u043c \u0432 \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0430\u0440\u0445\u0438\u0432 Spark \u0438 \u0432 \u043f\u0430\u043f\u043a\u0435 bin \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c pyspark (\u043f\u0440\u0438\u043c\u0435\u0440: <a href=\"http:\/\/spark.apache.org\/docs\/latest\/quick-start.html\">spark.apache.org\/docs\/latest\/quick-start.html<\/a>). \u041f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/847\/d9b\/d93\/847d9bd937c94921bbf72cf37a97d650.jpg\" alt=\"image\"\/><\/p>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u00ab\u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u00bb \u043d\u0430\u0448 \u0444\u0430\u0439\u043b \u0434\u043b\u044f SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (\u0432 \u0432\u0435\u0440\u0441\u0438\u0438 Spark 1.6 \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0444\u0430\u0439\u043b\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0434\u0435\u043b\u0430\u0442\u044c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b <a href=\"https:\/\/issues.apache.org\/jira\/browse\/SPARK-11197\">\u0431\u0435\u0437 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/a>). \u0422. \u0435. \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c DataFrame (\u0443 DataFrame \u0442\u043e\u0436\u0435 \u043a\u0443\u0447\u0430 <a href=\"http:\/\/spark.apache.org\/docs\/latest\/sql-programming-guide.html#dataframe-operations\">\u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439<\/a>) \u0438 \u0438\u0437 \u043d\u0435\u0433\u043e \u2014 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<br \/>  1. \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; from pyspark.sql import SQLContext, Row &gt;&gt;&gt; sqlContext = SQLContext(sc) <\/code><\/pre>\n<p>  2. \u0417\u0430\u0432\u043e\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e text \u043a\u0430\u043a \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0447\u0442\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0435:  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; text = sc.textFile('\u043f\u0443\u0442\u044c \u043a \u0444\u0430\u0439\u043b\u0443') &gt;&gt;&gt; text.first() u'2015-09-01\\tu'\u0434\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439'\\t101753' <\/code><\/pre>\n<p>  \u0412 \u043d\u0430\u0448\u0435\u043c \u0444\u0430\u0439\u043b\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u0435\u0439. \u0414\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Map \u0438 Split, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u044e: map(lambda l: l.split(&#8216;\\t&#8217;)). \u0412\u044b\u0431\u0435\u0440\u0435\u043c \u0438\u0437 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b. \u0414\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043b\u043e\u0432 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441 (\u0441\u0442\u043e\u043b\u0431\u0435\u0446 query) \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043b\u043e\u0432 \u0432 \u043d\u0435\u043c (\u0441\u0442\u043e\u043b\u0431\u0435\u0446 wc): map(lambda l: Row(query=l[1], wc=len(l[1].split(&#8216; &#8216;)))).<\/p>\n<p>  \u041c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0447\u0442\u043e\u0431\u044b \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0435 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u043d\u0435\u0439:<br \/>  map(lambda l: Row(date=l[0], query=l[1], stat=l[2], wc=len(l[1].split(&#8216; &#8216;))))<\/p>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u044d\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u0447\u043a\u0443  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; schema = text.map(lambda l: l.split('\\t')).map(lambda l: Row(query=l[1], wc=len(l[1].split(' ')))) <\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 schema \u0432 DataFrame, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 (\u043f\u0440\u0438\u043c\u0435\u0440\u044b <a href=\"http:\/\/spark.apache.org\/docs\/latest\/sql-programming-guide.html#dataframe-operations\">spark.apache.org\/docs\/latest\/sql-programming-guide.html#dataframe-operations<\/a>):  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; df = sqlContext.createDataFrame(schema) &gt;&gt;&gt; df.show() +--------------------+---+ | query| wc| +--------------------+---+ |\u0434\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439...| 2| | \u0441\u0431\u0435\u0440\u0431\u0430\u043d\u043a \u043e\u043d\u043b\u0430\u0439\u043d| 2| |\u044d\u0445\u043e \u043c\u043e\u0441\u043a\u0432\u044b \u0441\u043b\u0443\u0448\u0430\u0442\u044c| 3| ... <\/code><\/pre>\n<p>  3. \u041f\u0435\u0440\u0435\u0432\u0435\u0434\u0435\u043c DataFrame \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0447\u0442\u043e\u0431\u044b \u0434\u0435\u043b\u0430\u0442\u044c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b:  <\/p>\n<pre><code class=\"python\"> &gt;&gt;&gt; df.registerTempTable('queryTable') <\/code><\/pre>\n<p>  4. \u0421\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u0438 \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e output:  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; output = sqlContext.sql('SELECT wc, COUNT(*) FROM queryTable GROUP BY wc').collect() <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0444\u0430\u0439\u043b\u0430 \u0432 2GB \u043f\u0440\u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 RAM \u0432 700MB \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430\u043d\u044f\u043b 9 \u043c\u0438\u043d\u0443\u0442. \u0425\u043e\u0434 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u0438\u0434\u0435\u0442\u044c \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0438\u0434\u0430 (\u2026 \u0438\u0437 53):<br \/>  INFO TaskSetManager: Finished task 1.0 in stage 8.0 (TID 61) in 11244 ms on localhost (1\/53)<\/p>\n<p>  \u041c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f:  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; outputRealty = sqlContext.sql('SELECT wc, COUNT(*) FROM queryTable WHERE query like &quot;%\u043d\u0435\u0434\u0432\u0438\u0436\u0438\u043c\u043e\u0441\u0442\u044c%&quot; GROUP BY wc').collect() <\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0433\u0438\u0441\u0442\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 output \u0432 \u0444\u0430\u0439\u043b &#8216;output.txt&#8217; \u0438 \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0432 Excel:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; with open('output.txt', 'w') as f: ...         f.write('wc \\t count \\n') ...         for line in output: ...             f.write(str(line[0]) + '\\t' + str(line[1]) + '\\n') <\/code><\/pre>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/274705\/\"> http:\/\/habrahabr.ru\/post\/274705\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/e89\/c5c\/790\/e89c5c79016441a193d6f5e1eb70b059.jpg\" alt=\"image\"\/><br \/>  \u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<br \/>  4 \u044f\u043d\u0432\u0430\u0440\u044f \u0432\u044b\u0448\u043b\u0430 \u043d\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <a href=\"http:\/\/spark.apache.org\/releases\/spark-release-1-6-0.html\">Apache Spark 1.6<\/a> \u0441 <s>bug fix<\/s> \u043d\u043e\u0432\u044b\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430 \u0425\u0430\u0431\u0440\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0435\u043c\u0430\u043b\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u043e\u0442 <a href=\"http:\/\/habrahabr.ru\/company\/mlclass\/blog\/250811\/\">\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f<\/a> \u0434\u043e <a href=\"http:\/\/habrahabr.ru\/company\/bitrix\/blog\/273279\/\">\u043e\u043f\u044b\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445<\/a>. Spark \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c \u0438 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0434\u0430\u0436\u0435 \u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 Spark \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0433\u0440\u0435\u0445 \u043d\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u043d\u0430 \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435 \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 (\u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430) \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044b\u0447\u043d\u044b\u0445 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0434\u0430\u0436\u0435 \u043d\u0435\u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e. \u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 iPython (<a href=\"http:\/\/jupyter.readthedocs.org\/en\/latest\/install.html\">Jupyter<\/a>) notebook \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0435 \u043e\u0442\u0447\u0435\u0442\u044b. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0444\u0430\u0439\u043b\u0430, \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 Python \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/apache\/spark\/tree\/master\/examples\/src\/main\/python\">\u0442\u0443\u0442<\/a>.  <\/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-271786","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271786","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=271786"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271786\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=271786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=271786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=271786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}