{"id":135079,"date":"2024-10-31T07:31:34","date_gmt":"2024-10-30T23:31:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/135079\/"},"modified":"2024-10-31T07:31:34","modified_gmt":"2024-10-30T23:31:34","slug":"spark%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88%e3%80%82%ef%bc%88spark%e5%af%ab%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/135079\/","title":{"rendered":"Spark\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\uff0c\u5feb\u901f\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\uff08spark\u5beb\u6578\u64da\u9032\u5165\u6578\u64da\u5eab\uff09"},"content":{"rendered":"<h1 id=\"spark%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-WNDGVpAMKs\">Spark\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\uff0c\u5feb\u901f\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0cApache Spark\u4f5c\u70ba\u4e00\u500b\u5f37\u5927\u7684\u5206\u4f48\u5f0f\u8a08\u7b97\u6846\u67b6\uff0c\u5df2\u7d93\u6210\u70ba\u8655\u7406\u5927\u6578\u64da\u7684\u9996\u9078\u5de5\u5177\u4e4b\u4e00\u3002Spark\u4e0d\u50c5\u80fd\u5920\u9032\u884c\u5feb\u901f\u7684\u6578\u64da\u8655\u7406\uff0c\u9084\u80fd\u5920\u9ad8\u6548\u5730\u5c07\u6578\u64da\u5beb\u5165\u5404\u7a2e\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Spark\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u7684\u6700\u4f73\u5be6\u8e10\u548c\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87spark%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5-WNDGVpAMKs\">\u70ba\u4ec0\u9ebc\u9078\u64c7Spark\u9032\u884c\u6578\u64da\u5beb\u5165<\/h2>\n<p>Apache Spark\u7684\u512a\u52e2\u5728\u65bc\u5176\u9ad8\u6548\u7684\u5167\u5b58\u8a08\u7b97\u80fd\u529b\u548c\u652f\u6301\u591a\u7a2e\u6578\u64da\u6e90\u7684\u9748\u6d3b\u6027\u3002\u7121\u8ad6\u662f\u7d50\u69cb\u5316\u6578\u64da\u9084\u662f\u975e\u7d50\u69cb\u5316\u6578\u64da\uff0cSpark\u90fd\u80fd\u5920\u8f15\u9b06\u8655\u7406\u3002\u6b64\u5916\uff0cSpark\u7684RDD\uff08\u5f48\u6027\u5206\u4f48\u5f0f\u6578\u64da\u96c6\uff09\u548cDataFrame API\u4f7f\u5f97\u6578\u64da\u8655\u7406\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u548c\u76f4\u89c0\u3002<\/p>\n<h2 id=\"spark%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5-WNDGVpAMKs\">Spark\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>\u5728\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fddSpark\u80fd\u5920\u8207\u76ee\u6a19\u6578\u64da\u5eab\u5efa\u7acb\u9023\u63a5\u3002\u9019\u901a\u5e38\u9700\u8981\u4f7f\u7528\u76f8\u61c9\u7684JDBC\u9a45\u52d5\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u9023\u63a5MySQL\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.setProperty(\"user\", \"your_username\")\nconnectionProperties.setProperty(\"password\", \"your_password\")\n<\/code><\/pre>\n<h2 id=\"%e5%b0%87%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-WNDGVpAMKs\">\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab<\/h2>\n<p>\u4e00\u65e6\u5efa\u7acb\u4e86\u9023\u63a5\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528Spark\u7684DataFrame API\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c07DataFrame\u5beb\u5165MySQL\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>import org.apache.spark.sql.SparkSession\n\nval spark = SparkSession.builder()\n  .appName(\"Spark Write to Database\")\n  .getOrCreate()\n\n\/\/ \u5275\u5efa\u4e00\u500bDataFrame\nval data = Seq((1, \"Alice\"), (2, \"Bob\"), (3, \"Cathy\"))\nval df = spark.createDataFrame(data).toDF(\"id\", \"name\")\n\n\/\/ \u5c07DataFrame\u5beb\u5165MySQL\u6578\u64da\u5eab\ndf.write\n  .mode(\"append\") \/\/ \u53ef\u4ee5\u9078\u64c7\"overwrite\"\u6216\"append\"\n  .jdbc(jdbcUrl, \"your_table\", connectionProperties)\n<\/code><\/pre>\n<h2 id=\"%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96-WNDGVpAMKs\">\u6027\u80fd\u512a\u5316<\/h2>\n<p>\u5728\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u6642\uff0c\u6027\u80fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u512a\u5316\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li><strong>\u6279\u91cf\u5beb\u5165\uff1a<\/strong>\u4f7f\u7528\u6279\u91cf\u5beb\u5165\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e\u9069\u7576\u7684\u6279\u91cf\u5927\u5c0f\u4f86\u5be6\u73fe\u3002<\/li>\n<li><strong>\u5206\u5340\u5beb\u5165\uff1a<\/strong>\u6839\u64da\u6578\u64da\u7684\u7279\u6027\uff0c\u5c07\u6578\u64da\u5206\u5340\u53ef\u4ee5\u63d0\u9ad8\u5beb\u5165\u901f\u5ea6\u3002\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684`repartition`\u65b9\u6cd5\u4f86\u9032\u884c\u5206\u5340\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5408\u9069\u7684\u5beb\u5165\u6a21\u5f0f\uff1a<\/strong>\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u5beb\u5165\u6a21\u5f0f\uff0c\u5982&#8221;append&#8221;\u6216&#8221;overwrite&#8221;\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-WNDGVpAMKs\">\u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u6578\u64da\u5beb\u5165\u904e\u7a0b\u4e2d\uff0c\u932f\u8aa4\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u5efa\u8b70\u5728\u5beb\u5165\u6578\u64da\u6642\u5be6\u65bd\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u4ee5\u4fbf\u80fd\u5920\u6355\u7372\u548c\u8655\u7406\u7570\u5e38\u60c5\u6cc1\u3002\u53ef\u4ee5\u4f7f\u7528try-catch\u584a\u4f86\u6355\u7372\u7570\u5e38\uff0c\u4e26\u6839\u64da\u9700\u8981\u9032\u884c\u91cd\u8a66\u6216\u8a18\u9304\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-WNDGVpAMKs\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Apache Spark\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u662f\u4e00\u500b\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u6b63\u78ba\u7684\u9023\u63a5\u8a2d\u7f6e\u3001\u6578\u64da\u5beb\u5165\u65b9\u6cd5\u4ee5\u53ca\u6027\u80fd\u512a\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u5beb\u5165\u7684\u6548\u7387\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u96f2\u670d\u52d9\u5668\u548cVPS\u89e3\u6c7a\u65b9\u6848\u81f3\u95dc\u91cd\u8981\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u96f2\u670d\u52d9\u5668<\/a>\u7684\u8cc7\u8a0a\uff0c\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u60a8\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Spark\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u7684\u9ad8\u6548\u89e3\u6c7a\u65b9\u6848\uff0c\u5be6\u73fe\u5feb\u901f\u6578\u64da\u8655\u7406\u8207\u7121\u7e2b\u96c6\u6210\uff0c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u6548\u7387\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-135079","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135079","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=135079"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135079\/revisions"}],"predecessor-version":[{"id":135080,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135079\/revisions\/135080"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=135079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=135079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=135079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}