{"id":507,"date":"2021-07-16T09:29:11","date_gmt":"2021-07-16T01:29:11","guid":{"rendered":"https:\/\/blog.frost-s.tk\/?p=507"},"modified":"2021-12-16T23:01:44","modified_gmt":"2021-12-16T15:01:44","slug":"spark-sql-%e6%98%af%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96%e6%89%a7%e8%a1%8c%e7%9a%84","status":"publish","type":"post","link":"https:\/\/blog.frost-s.com\/index.php\/2021\/07\/16\/spark-sql-%e6%98%af%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96%e6%89%a7%e8%a1%8c%e7%9a%84\/","title":{"rendered":"Spark sql \u662f\u5982\u4f55\u4f18\u5316\u6267\u884c\u7684"},"content":{"rendered":"\n<p>Spark SQL \u7aef\u5230\u7aef\u7684\u5b8c\u6574\u4f18\u5316\u6d41\u7a0b\u4e3b\u8981\u5305\u62ec\u4e24\u4e2a\u9636\u6bb5\uff1aCatalyst \u4f18\u5316\u5668\u548c Tungsten\u3002\u5176\u4e2d\uff0cCatalyst \u4f18\u5316\u5668\u53c8\u5305\u542b\u903b\u8f91\u4f18\u5316\u548c\u7269\u7406\u4f18\u5316\u4e24\u4e2a\u9636\u6bb5\u3002\u4e3a\u4e86\u628a\u5f00\u53d1\u8005\u7684\u67e5\u8be2\u4f18\u5316\u5230\u6781\u81f4\uff0c\u6574\u4e2a\u4f18\u5316\u8fc7\u7a0b\u7684\u8fd0\u4f5c\u673a\u5236\u8bbe\u8ba1\u5f97\u90fd\u5f88\u7cbe\u5bc6\uff0c\u56e0\u6b64\u6211\u4f1a\u7528\u4e09\u8bb2\u7684\u65f6\u95f4\u5e26\u4f60\u8be6\u7ec6\u63a2\u8ba8\u3002<\/p>\n\n\n\n<p>\u4e0b\u56fe\u5c31\u662f\u8fd9\u4e2a\u8fc7\u7a0b\u7684\u5b8c\u6574\u56fe\u793a\uff0c\u4f60\u53ef\u4ee5\u5148\u901a\u8fc7\u5b83\u5bf9\u4f18\u5316\u6d41\u7a0b\u6709\u4e00\u4e2a\u6574\u4f53\u7684\u8ba4\u77e5\u3002\u7136\u540e\u968f\u7740\u6211\u7684\u8bb2\u89e3\uff0c\u9010\u6e10\u53bb\u592f\u5b9e\u5176\u4e2d\u7684\u5173\u952e\u73af\u8282\u3001\u91cd\u8981\u6b65\u9aa4\u548c\u6838\u5fc3\u77e5\u8bc6\u70b9\uff0c\u5728\u6df1\u5165\u5c40\u90e8\u4f18\u5316\u7ec6\u8282\u7684\u540c\u65f6\uff0c\u628a\u63e1\u5168\u5c40\u4f18\u5316\u6d41\u7a0b\uff0c\u505a\u5230\u65e2\u89c1\u6811\u6728\u3001\u4e5f\u89c1\u68ee\u6797\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/static001.geekbang.org\/resource\/image\/f3\/72\/f3ffb5fc43ae3c9bca44c1f4f8b7e872.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/static001.geekbang.org\/resource\/image\/f3\/72\/f3ffb5fc43ae3c9bca44c1f4f8b7e872.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4f8b\u5b50\u6765\u81ea\u7535\u5b50\u5546\u52a1\u573a\u666f\uff0c\u4e1a\u52a1\u9700\u6c42\u5f88\u7b80\u5355\uff1a\u7ed9\u5b9a\u4ea4\u6613\u4e8b\u5b9e\u8868 transactions \u548c\u7528\u6237\u7ef4\u5ea6\u8868 users\uff0c\u7edf\u8ba1\u4e0d\u540c\u7528\u6237\u7684\u4ea4\u6613\u989d\uff0c\u6570\u636e\u6e90\u4ee5 Parquet \u7684\u683c\u5f0f\u5b58\u50a8\u5728\u5206\u5e03\u5f0f\u6587\u4ef6\u7cfb\u7edf\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8981\u5148\u7528 Parquet API \u8bfb\u53d6\u6e90\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val userFile: String = _\nval usersDf = spark.read.parquet(userFile)\nusersDf.printSchema\n\/**\nroot\n|-- userId: integer (nullable = true)\n|-- name: string (nullable = true)\n|-- age: integer (nullable = true)\n|-- gender: string (nullable = true)\n|-- email: string (nullable = true)\n*\/\nval users = usersDf\n.select(\"name\", \"age\", \"userId\")\n.filter($\"age\" < 30)\n.filter($\"gender\".isin(\"M\"))\n\nval txFile: String = _\nval txDf = spark.read.parquet(txFile)\ntxDf.printSchema\n\/**\nroot\n|-- itemId: integer (nullable = true)\n|-- userId: integer (nullable = true)\n|-- price: float (nullable = true)\n|-- quantity: integer (nullable = true)\n*\/\n\nval result = txDF.select(\"price\", \"volume\", \"userId\")\n.join(users, Seq(\"userId\"), \"inner\")\n.groupBy(col(\"name\"), col(\"age\")).agg(sum(col(\"price\") * col(\"volume\")).alias(\"revenue\"))\n\nresult.write.parquet(\"_\")<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spark SQL \u7aef\u5230\u7aef\u7684\u5b8c\u6574\u4f18\u5316\u6d41\u7a0b\u4e3b\u8981\u5305\u62ec\u4e24\u4e2a\u9636\u6bb5\uff1aCatalyst \u4f18\u5316\u5668\u548c Tungsten\u3002\u5176\u4e2d\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,19],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/507"}],"collection":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":4,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":835,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/507\/revisions\/835"}],"wp:attachment":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}