{"id":6039,"date":"2025-06-25T20:03:00","date_gmt":"2025-06-25T12:03:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=6039"},"modified":"2025-06-25T17:20:06","modified_gmt":"2025-06-25T09:20:06","slug":"hive-sql-%e7%9a%84%e7%9f%a5%e8%af%86%e7%82%b9%e5%b7%a9%e5%9b%ba","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/6039.html","title":{"rendered":"Hive SQL \u7684\u77e5\u8bc6\u70b9\u5de9\u56fa"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u8d81\u7740\u4e0d\u592a\u5fd9\u7684\u65f6\u5019\u6574\u7406\u603b\u7ed3\u4e00\u4e0b Hive SQL \u7684\u57fa\u672c\u7528\u6cd5\uff0c<strong>\u907f\u514d\u9057\u5fd8<\/strong>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<h4 class=\"wp-block-heading\">1. Hive SQL\u4e2d\u7684 inner join \u7684\u5199\u6cd5<\/h4>\n\n\n\n<p>\u5927\u90e8\u5206\u65f6\u5019\uff0c\u6211\u8fd8\u662f left outer join \u5199\u7684\u591a\uff08\u7528\u4e8e\u8865\u5145\u5b57\u6bb5\uff09\uff0cinner join\u5199\u7684\u5c11\uff0c\u5bf9\u4e8e\u5b83\u5230\u5e95\u8be5\u5982\u4f55\u5199\u4e0d\u662f\u592a\u786e\u5b9a\uff08\u8868\u5173\u8054\u6761\u4ef6\u5982\u679c\u4f4d\u7f6e\u653e\u9519\u4e86\uff0c\u4f1a\u5148\u505a\u7b1b\u5361\u5c14\u79ef\uff0c\u518d\u8fc7\u6ee4\uff09\uff0c\u8fd9\u91cc\u501f\u52a9\u5927\u6a21\u578b\u518d\u7cfb\u7edf\u6027\u7684\u6574\u7406\u603b\u7ed3\u4e00\u4e0b\uff0c\u7ed9\u6211\u81ea\u5df1\u540e\u9762\u5f62\u6210\u4e00\u4e2a\u7edf\u4e00\u7684\u6a21\u5f0f\uff0c\u5c31\u4e0d\u5bb9\u6613\u51fa\u9519\u4e86\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-b6cd275146d3d6b791382f3233deb048\"><strong>\u5728\uff08from\u4e4b\u540e\uff0cwhere\u4e4b\u524d\uff09\u8fdb\u884cjoin\u5e76\u901a\u8fc7on\u6307\u5b9a\u5173\u8054\u6761\u4ef6\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\na.*,b.*\nFROM table_a a\nINNER JOIN table_b b ON a.join_key = b.join_key -- \u5728\uff08from\u4e4b\u540e\uff0cwhere\u4e4b\u524d\uff09\u8fdb\u884cjoin\u5e76\u901a\u8fc7on\u6307\u5b9a\u5173\u8054\u6761\u4ef6\n...\nWHERE\n...<\/code><\/pre>\n\n\n\n<p>question<br>Hive SQL\u91cc\u9762\u7684\u591a\u8868inner join\u5173\u8054\uff0c\u5982\u679c\u8981\u5173\u8054\u7684\u6bcf\u4e00\u5f20\u8868\u90fd\u662f\u4e00\u4e2a\u5206\u533a\u8868\uff0c\u4e14\u5206\u522b\u6709\u4e00\u4e9b\u9664\u4e86\u5206\u533a\u4e4b\u5916\u7684\u8fc7\u6ee4\u6761\u4ef6\uff0cSQL\u8be5\u5982\u4f55\u5199\u5462\uff1f<\/p>\n\n\n\n<p>answer<br>\u5f53\u4f60\u5728Hive SQL\u4e2d\u5bf9\u591a\u4e2a\u5206\u533a\u8868\u8fdb\u884c<code>INNER JOIN<\/code>\u5173\u8054\uff0c\u5e76\u4e14\u6bcf\u5f20\u8868\u4e0d\u4ec5\u6709\u57fa\u4e8e\u5206\u533a\u7684\u8fc7\u6ee4\u6761\u4ef6\uff0c\u8fd8\u6709\u5176\u4ed6\u975e\u5206\u533a\u5b57\u6bb5\u7684\u8fc7\u6ee4\u6761\u4ef6\u65f6\uff0c\u53ef\u4ee5\u5728<code>WHERE<\/code>\u5b50\u53e5\u4e2d\u540c\u65f6\u6307\u5b9a\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u8fc7\u6ee4\u6761\u4ef6\u3002\u8fd9\u6837\u505a\u4e0d\u4ec5\u53ef\u4ee5\u5229\u7528\u5206\u533a\u6765\u51cf\u5c11\u67e5\u8be2\u626b\u63cf\u7684\u6570\u636e\u91cf\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u8fc7\u6ee4\u6761\u4ef6\u8fdb\u4e00\u6b65\u7f29\u5c0f\u7ed3\u679c\u96c6\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u4f60\u6709\u4e09\u4e2a\u5206\u533a\u8868\uff1a<code>table_a<\/code>\u3001<code>table_b<\/code>\u548c<code>table_c<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u6839\u636e\u5b57\u6bb5<code>partition_field_a<\/code>\u3001<code>partition_field_b<\/code>\u548c<code>partition_field_c<\/code>\u8fdb\u884c\u5206\u533a\u3002\u6b64\u5916\uff0c\u6bcf\u4e2a\u8868\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u8fc7\u6ee4\u6761\u4ef6\uff0c\u6bd4\u5982<code>filter_field_a<\/code>\u3001<code>filter_field_b<\/code>\u548c<code>filter_field_c<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8bSQL\u8bed\u53e5\u5c55\u793a\u5982\u4f55\u7f16\u5199\u8fd9\u6837\u7684\u67e5\u8be2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT a.*, b.*, c.*\nFROM table_a a\nINNER JOIN table_b b ON a.join_key = b.join_key -- \u5728\uff08from\u4e4b\u540e\uff0cwhere\u4e4b\u524d\uff09\u8fdb\u884cjoin\u5e76\u901a\u8fc7on\u6307\u5b9a\u5173\u8054\u6761\u4ef6\nINNER JOIN table_c c ON b.join_key = c.join_key\nWHERE a.partition_field_a = 'desired_value_a'  -- \u5206\u533a\u8fc7\u6ee4\u6761\u4ef6\n  AND b.partition_field_b = 'desired_value_b'  -- \u5206\u533a\u8fc7\u6ee4\u6761\u4ef6\n  AND c.partition_field_c = 'desired_value_c'  -- \u5206\u533a\u8fc7\u6ee4\u6761\u4ef6\n  AND a.filter_field_a = 'some_value_a'        -- \u975e\u5206\u533a\u5b57\u6bb5\u8fc7\u6ee4\u6761\u4ef6\n  AND b.filter_field_b = 'some_value_b'        -- \u975e\u5206\u533a\u5b57\u6bb5\u8fc7\u6ee4\u6761\u4ef6\n  AND c.filter_field_c = 'some_value_c';       -- \u975e\u5206\u533a\u5b57\u6bb5\u8fc7\u6ee4\u6761\u4ef6<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>join_key<\/code>\u4ee3\u8868\u7528\u4e8e\u8fde\u63a5\u5404\u8868\u7684\u5173\u952e\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li><code>'desired_value_a'<\/code>, <code>'desired_value_b'<\/code>, \u548c <code>'desired_value_c'<\/code>\u662f\u4f60\u4e3a\u5404\u4e2a\u8868\u8bbe\u5b9a\u7684\u5206\u533a\u503c\uff0c\u8fd9\u786e\u4fdd\u4e86\u53ea\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5206\u533a\u4f1a\u88ab\u626b\u63cf\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u67e5\u8be2\u6548\u7387\u3002<\/li>\n\n\n\n<li><code>'some_value_a'<\/code>, <code>'some_value_b'<\/code>, \u548c <code>'some_value_c'<\/code>\u662f\u5e94\u7528\u5728\u975e\u5206\u533a\u5b57\u6bb5\u4e0a\u7684\u989d\u5916\u8fc7\u6ee4\u6761\u4ef6\uff0c\u5e2e\u52a9\u8fdb\u4e00\u6b65\u7cbe\u786e\u4f60\u7684\u67e5\u8be2\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u5145\u5206\u5229\u7528\u5206\u533a\u7684\u4f18\u52bf\u6765\u4f18\u5316\u67e5\u8be2\u6027\u80fd\uff0c\u540c\u65f6\u4e5f\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7684\u8fc7\u6ee4\u6761\u4ef6\u6765\u7ec6\u5316\u7ed3\u679c\u96c6\u3002\u8bb0\u5f97\u8c03\u6574\u8868\u540d\u3001\u5b57\u6bb5\u540d\u53ca\u5177\u4f53\u503c\u4ee5\u5339\u914d\u4f60\u7684\u5b9e\u9645\u60c5\u51b5\u3002\u8fd9\u79cd\u67e5\u8be2\u65b9\u5f0f\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u663e\u8457\u51cf\u5c11\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u91cf\uff0c\u63d0\u9ad8\u67e5\u8be2\u6267\u884c\u7684\u901f\u5ea6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Hive SQL\u591a\u8868\u8054\u5408\u67e5\u8be2\u7684\u5199\u6cd5<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u4e24\u8868\u5355\u6761\u4ef6\nSELECT a.* FROM a JOIN b ON (a.id = b.id)\n-- \u4e24\u8868\u591a\u6761\u4ef6\nSELECT a.* FROM a JOIN b ON (a.id = b.id AND a.department = b.department)\n\n#\u591a\u8868\u8054\u5408join\nSELECT a.val, b.val, c.val\nFROM a\nJOIN b ON (a.key = b.key1)\nJOIN c ON (c.key = b.key2)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u8f83\u957f\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5904\u7406\u65b9\u6cd5<\/h4>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-712981ff3a1d5045596482c14affa5d8\">\u4f7f\u7528 set \u8bed\u6cd5\u5148\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff0c\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u5185\u5bb9\u7684\u4fee\u6539\u76f4\u63a5\u5728\u4e00\u4e2a\u5730\u65b9\u8fdb\u884c\uff0c\u5176\u5b83\u5730\u65b9\u76f4\u63a5\u4f7f\u7528\u5373\u53ef\uff0c\u907f\u514d\u5185\u5bb9\u4fee\u6539\u4e0d\u540c\u6b65\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n\u4e00\u4e2a\u6570\u636e\u5206\u6790\u573a\u666f\uff0c\u627e\u51fa\u5185\u90e8\u7cfb\u7edf\u4e2d\u53ef\u80fd\u548c\u641c\u7d22\u3001\u5927\u6570\u636e\u5e73\u53f0\u3001\u6570\u636e\u5e93\u67e5\u8be2\u3001\u4ee3\u7406\u7b49\u529f\u80fd\u6709\u5173\u7684\u57df\u540d\n\n\u4e00\u822c\u60c5\u51b5\u4e0b\u5c31\u662f\u5148\u6839\u636e\u7ecf\u9a8c\uff0c\u7ed9\u51fa\u4e00\u4e9b\u5173\u952e\u5b57\u53bb\u505a\u5339\u914d\uff0c\u7136\u540e\u4eba\u5de5\u7b5b\u9009\u3001\u786e\u8ba4\u5339\u914d\u4e14\u6709\u6548\u7684\u57df\u540d\u3002\n\u6240\u4ee5\u8fd9\u91cc\u9762\u6d89\u53ca\u52302\u6b65\uff0c\u7b2c1\u6b65\uff0c\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d\uff1b\u7b2c2\u6b65\uff0c\u6839\u636e\u603b\u7ed3\u51fa\u6765\u7684\u767d\u540d\u5355\u6392\u9664\u4e00\u4e9b\u5df2\u77e5\u9519\u8bef\u7684\u5339\u914d\u7ed3\u679c\u3002\n*\/\nset match_list='search|data|db|proxy|bi'; -- \u540e\u9762\u53d8\u91cf\u7684\u4f7f\u7528\u65f6\u9700\u8981\u7528 ${val_name} \u5c06\u53d8\u91cf\u540dval_name\u62ec\u8d77\u6765\nset white_list='feedback|feedbcak|sandbox';\n\nwith t1 as (SELECT\n'data.corp.com,db.corp.com,bi.corp.com,feedback.corp.com,sandbox.corp.com,adb.corp.com' as domain_list\n)\n\nSELECT\ndomain\n,if(domain rlike ${match_list} and domain not rlike ${white_list},1,0) as ret\n,${match_list},${white_list}\n,domain_list\nFROM t1\nlateral view explode(split(domain_list,',')) domain as domain\n\nwhere\ndomain rlike ${match_list}\nand domain not rlike ${white_list}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>Hive SQL\u4e2d\u7684full outer join\u4f7f\u7528\u5b66\u4e60<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/5465.html\">https:\/\/ixyzero.com\/blog\/archives\/5465.html<\/a><\/p>\n\n\n\n<p>[\u4e00\u8d77\u5b66Hive]\u4e4b\u5341\u4e00-Hive\u4e2dJoin\u7684\u7c7b\u578b\u548c\u7528\u6cd5<br><a href=\"http:\/\/lxw1234.com\/archives\/2015\/06\/315.htm\">http:\/\/lxw1234.com\/archives\/2015\/06\/315.htm<\/a><\/p>\n\n\n\n<p>Hive SQL\u5b66\u4e60\u6574\u7406<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/4245.html\">https:\/\/ixyzero.com\/blog\/archives\/4245.html<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u8d81\u7740\u4e0d\u592a\u5fd9\u7684\u65f6\u5019\u6574\u7406\u603b\u7ed3\u4e00\u4e0b Hive SQL \u7684\u57fa\u672c\u7528\u6cd5\uff0c\u907f\u514d\u9057\u5fd8\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23],"tags":[1261,2714,421,1262,2717,2716,2715],"class_list":["post-6039","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","tag-hive","tag-inner-join","tag-join","tag-sql","tag-udv","tag-2716","tag-2715"],"views":1008,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/6039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=6039"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/6039\/revisions"}],"predecessor-version":[{"id":6040,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/6039\/revisions\/6040"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=6039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=6039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=6039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}