{"id":135077,"date":"2024-10-31T07:29:27","date_gmt":"2024-10-30T23:29:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/135077\/"},"modified":"2024-10-31T07:29:27","modified_gmt":"2024-10-30T23:29:27","slug":"spark%e5%88%86%e5%b8%83%e5%bc%8f%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96%e6%8c%87%e5%8d%97%ef%bc%88spark%e6%80%8e%e9%ba%bc%e5%88%86%e5%b8%83%e5%bc%8f%e8%ae%80%e5%8f%96","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/135077\/","title":{"rendered":"Spark\u5206\u5e03\u5f0f\u6280\u5de7\uff1a\u6578\u64da\u5eab\u8b80\u53d6\u6307\u5357\uff08spark\u600e\u9ebc\u5206\u5e03\u5f0f\u8b80\u53d6\u6578\u64da\u5eab\uff09"},"content":{"rendered":"<h1 id=\"spark%e5%88%86%e5%b8%83%e5%bc%8f%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96%e6%8c%87%e5%8d%97-iqEUsHtEos\">Spark\u5206\u5e03\u5f0f\u6280\u5de7\uff1a\u6578\u64da\u5eab\u8b80\u53d6\u6307\u5357<\/h1>\n<p>Apache Spark \u662f\u4e00\u500b\u5f37\u5927\u7684\u5206\u5e03\u5f0f\u8a08\u7b97\u6846\u67b6\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5927\u6578\u64da\u8655\u7406\u548c\u5206\u6790\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u6578\u64da\u6210\u70ba\u4e86\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u5de5\u7a0b\u5e2b\u9762\u81e8\u7684\u6311\u6230\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528 Spark \u7684\u5206\u5e03\u5f0f\u7279\u6027\u4f86\u9ad8\u6548\u8b80\u53d6\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u6280\u5de7\u548c\u793a\u4f8b\u3002<\/p>\n<h2 id=\"1-spark%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%9e%b6%e6%a7%8b-iqEUsHtEos\">1. Spark\u7684\u57fa\u672c\u67b6\u69cb<\/h2>\n<p>Spark \u7684\u6838\u5fc3\u662f\u5176\u5206\u5e03\u5f0f\u8a08\u7b97\u6a21\u578b\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u5728\u591a\u500b\u7bc0\u9ede\u4e0a\u4e26\u884c\u8655\u7406\u6578\u64da\u3002Spark \u7684\u8a08\u7b97\u904e\u7a0b\u4e3b\u8981\u7531\u4ee5\u4e0b\u5e7e\u500b\u7d44\u4ef6\u7d44\u6210\uff1a<\/p>\n<ul>\n<li><strong>Driver Program<\/strong>\uff1a\u8ca0\u8cac\u5354\u8abf\u6574\u500b\u8a08\u7b97\u904e\u7a0b\uff0c\u4e26\u5c07\u4efb\u52d9\u5206\u914d\u7d66\u5404\u500b\u5de5\u4f5c\u7bc0\u9ede\u3002<\/li>\n<li><strong>Cluster Manager<\/strong>\uff1a\u8ca0\u8cac\u7ba1\u7406\u96c6\u7fa4\u8cc7\u6e90\uff0c\u78ba\u4fdd\u5404\u500b\u7bc0\u9ede\u7684\u8cc7\u6e90\u5206\u914d\u5408\u7406\u3002<\/li>\n<li><strong>Worker Nodes<\/strong>\uff1a\u5be6\u969b\u57f7\u884c\u8a08\u7b97\u4efb\u52d9\u7684\u7bc0\u9ede\u3002<\/li>\n<\/ul>\n<h2 id=\"2-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e9%85%8d%e7%bd%ae-iqEUsHtEos\">2. \u6578\u64da\u5eab\u9023\u63a5\u914d\u7f6e<\/h2>\n<p>\u5728\u4f7f\u7528 Spark \u8b80\u53d6\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u914d\u7f6e\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002Spark \u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ec MySQL\u3001PostgreSQL\u3001Oracle \u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 JDBC \u9023\u63a5 MySQL \u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>val jdbcUrl = \"jdbc:mysql:\/\/localhost:3306\/your_database\"\nval connectionProperties = new java.util.Properties()\nconnectionProperties.put(\"user\", \"your_username\")\nconnectionProperties.put(\"password\", \"your_password\")\n<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8spark%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab-iqEUsHtEos\">3. \u4f7f\u7528Spark\u8b80\u53d6\u6578\u64da\u5eab<\/h2>\n<p>\u4e00\u65e6\u914d\u7f6e\u597d\u9023\u63a5\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 Spark \u7684 DataFrame API \u4f86\u8b80\u53d6\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u5f9e MySQL \u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>val df = spark.read\n  .jdbc(jdbcUrl, \"your_table\", connectionProperties)\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5f9e\u6307\u5b9a\u7684\u6578\u64da\u5eab\u8868\u4e2d\u8b80\u53d6\u6578\u64da\u4e26\u5c07\u5176\u52a0\u8f09\u5230 DataFrame \u4e2d\u3002<\/p>\n<h2 id=\"4-%e5%88%86%e5%b8%83%e5%bc%8f%e8%ae%80%e5%8f%96%e7%9a%84%e6%8a%80%e5%b7%a7-iqEUsHtEos\">4. \u5206\u5e03\u5f0f\u8b80\u53d6\u7684\u6280\u5de7<\/h2>\n<p>\u70ba\u4e86\u63d0\u9ad8\u6578\u64da\u8b80\u53d6\u7684\u6548\u7387\uff0c\u53ef\u4ee5\u63a1\u7528\u4ee5\u4e0b\u5e7e\u7a2e\u6280\u5de7\uff1a<\/p>\n<h3 id=\"4-1-%e5%88%86%e5%8d%80%e8%ae%80%e5%8f%96-iqEUsHtEos\">4.1 \u5206\u5340\u8b80\u53d6<\/h3>\n<p>\u4f7f\u7528\u5206\u5340\u8b80\u53d6\u53ef\u4ee5\u5c07\u6578\u64da\u5206\u6210\u591a\u500b\u90e8\u5206\uff0c\u4e26\u884c\u8b80\u53d6\u3002\u9019\u6a23\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u8b80\u53d6\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8a2d\u7f6e\u5206\u5340\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>val df = spark.read\n  .option(\"numPartitions\", 10) \/\/ \u8a2d\u7f6e\u5206\u5340\u6578\n  .jdbc(jdbcUrl, \"your_table\", connectionProperties)\n<\/code><\/pre>\n<h3 id=\"4-2-%e4%bd%bf%e7%94%a8%e6%8e%a8%e6%96%b7%e6%a8%a1%e5%bc%8f-iqEUsHtEos\">4.2 \u4f7f\u7528\u63a8\u65b7\u6a21\u5f0f<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u63a8\u65b7\u6a21\u5f0f\u4f86\u81ea\u52d5\u63a8\u65b7\u6578\u64da\u7684\u7d50\u69cb\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u624b\u52d5\u914d\u7f6e\u7684\u5de5\u4f5c\u91cf\uff1a<\/p>\n<pre><code>val df = spark.read\n  .option(\"inferSchema\", \"true\") \/\/ \u81ea\u52d5\u63a8\u65b7\u6578\u64da\u7d50\u69cb\n  .jdbc(jdbcUrl, \"your_table\", connectionProperties)\n<\/code><\/pre>\n<h3 id=\"4-3-%e9%81%a9%e7%95%b6%e7%9a%84%e6%9f%a5%e8%a9%a2%e5%84%aa%e5%8c%96-iqEUsHtEos\">4.3 \u9069\u7576\u7684\u67e5\u8a62\u512a\u5316<\/h3>\n<p>\u5728\u5f9e\u6578\u64da\u5eab\u8b80\u53d6\u6578\u64da\u6642\uff0c\u4f7f\u7528\u9069\u7576\u7684 SQL \u67e5\u8a62\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u50c5\u9078\u64c7\u9700\u8981\u7684\u5217\u548c\u884c\uff0c\u907f\u514d\u8b80\u53d6\u4e0d\u5fc5\u8981\u7684\u6578\u64da\uff1a<\/p>\n<pre><code>val df = spark.read\n  .jdbc(jdbcUrl, \"(SELECT column1, column2 FROM your_table WHERE condition) AS subquery\", connectionProperties)\n<\/code><\/pre>\n<h2 id=\"5-%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86%e8%88%87%e8%bd%89%e6%8f%9b-iqEUsHtEos\">5. \u6578\u64da\u8655\u7406\u8207\u8f49\u63db<\/h2>\n<p>\u8b80\u53d6\u6578\u64da\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 Spark \u7684\u5404\u7a2e\u8f49\u63db\u64cd\u4f5c\u4f86\u8655\u7406\u6578\u64da\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>filter<\/code>\u3001<code>select<\/code> \u548c <code>groupBy<\/code> \u7b49\u64cd\u4f5c\u4f86\u9032\u884c\u6578\u64da\u6e05\u6d17\u548c\u5206\u6790\uff1a<\/p>\n<pre><code>val filteredDF = df.filter(\"column1 &gt; 100\")\nval selectedDF = filteredDF.select(\"column1\", \"column2\")\nval groupedDF = selectedDF.groupBy(\"column1\").count()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-iqEUsHtEos\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528 Apache Spark \u7684\u5206\u5e03\u5f0f\u7279\u6027\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u548c\u8655\u7406\u6578\u64da\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u3001\u4f7f\u7528\u5206\u5340\u8b80\u53d6\u3001\u63a8\u65b7\u6a21\u5f0f\u4ee5\u53ca\u512a\u5316\u67e5\u8a62\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8b80\u53d6\u7684\u6027\u80fd\u3002\u9019\u4e9b\u6280\u5de7\u5c0d\u65bc\u8655\u7406\u5927\u898f\u6a21\u6578\u64da\u96c6\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 Spark \u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u4e86\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u548c\u64f4\u5c55\u60a8\u7684\u6578\u64da\u8655\u7406\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1Spark\u5206\u5e03\u5f0f\u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u9ad8\u6548\u8b80\u53d6\u6578\u64da\u5eab\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6027\u80fd\uff0c\u5be6\u73fe\u5927\u6578\u64da\u5206\u6790\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-135077","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=135077"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135077\/revisions"}],"predecessor-version":[{"id":135078,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135077\/revisions\/135078"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=135077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=135077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=135077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}