{"id":5353,"date":"2022-11-03T20:01:00","date_gmt":"2022-11-03T12:01:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5353"},"modified":"2022-11-03T16:45:59","modified_gmt":"2022-11-03T08:45:59","slug":"hive-sql%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e5%88%97%e4%b9%8b%e9%97%b4%e7%9a%84%e6%a8%a1%e7%b3%8a%e6%af%94%e8%be%83","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5353.html","title":{"rendered":"Hive SQL\u4e2d\u5982\u4f55\u8fdb\u884c\u5217\u4e4b\u95f4\u7684\u6a21\u7cca\u6bd4\u8f83"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u5728\u5904\u7406SSO\u8d26\u53f7\u5b89\u5168\u7684\u95ee\u9898\u65f6\uff0c\u5355\u6b65\u6838\u5fc3\u5728\u4e8e\u5bf9SSO\u8ba4\u8bc1\u65e5\u5fd7\u7684\u5206\u6790\uff08\u6df1\u5c42\u6b21\u7684\u8fd8\u9700\u8981\u7ed3\u5408\u7528\u6237\u65e5\u5e38\u884c\u4e3a\u5206\u6790\uff0c\u5c5e\u4e8e\u5b89\u5168UEBA\u7684\u8303\u7574\uff09\u3002\u8fd9\u91cc\u7684\u5927\u4f53\u903b\u8f91\u5728\u4e8e\u6bd4\u8f83SSO\u767b\u5f55\u8d26\u53f7\u4e0eIP\u5173\u8054\u8d26\u53f7\u548c\u8bbe\u5907\u5173\u8054\u8d26\u53f7\u662f\u5426\u76f8\u540c\uff1f\u662f\u5426\u540c\u7ec4\uff1f\u5982\u679c\u4e0d\u540c\u5c31\u9700\u8981\u5173\u6ce8\uff08\u5f53\u7136\u540c\u7ec4\u4e0d\u540c\u4eba\u4e5f\u9700\u8981\u5173\u6ce8\uff0c\u4e0d\u8fc7\u76f8\u5bf9\u800c\u8a00\u98ce\u9669\u4f4e\u4e00\u70b9\uff0c\u4e5f\u6709\u4e00\u5b9a\u7684\u4e1a\u52a1\u5b9e\u9645\u9700\u6c42\u5728\u91cc\u9762\uff0c\u867d\u7136\u4e5f\u4e0d\u592a\u5408\u7406\uff0c\u4f46\u603b\u4f53\u98ce\u9669\u53ef\u63a7\uff0c\u53ef\u4ee5\u653e\u5728\u540e\u9762\u5904\u7406\uff09\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4e3b\u8981\u8bf4\u4e00\u4e0b\u5728Hive SQL\u4e2d\u5982\u4f55\u8fdb\u884c\u591a\u5217\u4e4b\u95f4\u7684\u6bd4\u8f83\uff0c\u4ee5\u5b9e\u73b0\u4e0a\u9762\u7684\u903b\u8f91\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<p>\u73b0\u5728\u5047\u8bbe\uff1aSSO\u767b\u5f55\u8d26\u53f7\u4e3a\u8d26\u53f7a(\u90e8\u95e8a)\u3001IP\u5173\u8054\u8d26\u53f7\u4e3a\u8d26\u53f7b(\u90e8\u95e8b)\u3001\u8bbe\u5907\u5173\u8054\u8d26\u53f7\u4e3a\u8d26\u53f7c(\u90e8\u95e8c)\u3002\u8d26\u53f7\u662f\u5426\u76f8\u540c\uff08\u548c\u90e8\u95e8\u662f\u5426\u76f8\u540c\uff09\u76f4\u63a5\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u5c31\u884c\uff0c\u5373 a==b \u548c a==c \u3002\u4f46\u662f\u662f\u5426\u540c\u7ec4\u8fd9\u4e2a\u5c31\u4e0d\u80fd\u76f4\u63a5\u6bd4\u8f83\u4e86\uff0c\u9700\u8981\u7528\u5230\u7c7b\u4f3c\u4e8e\u5b57\u7b26\u4e32\u67e5\u627e\/\u5339\u914d\u6765\u5b9e\u73b0\uff0c\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5b9e\u73b0\u8d77\u6765\u6bd4\u8f83\u7b80\u5355\uff0c\u8fd9\u91cc\u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\u5728Hive SQL\u4e2d\u5982\u4f55\u5b9e\u73b0\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u8868\u91cc\u73b0\u5728\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>`timestr`\n,`action`\n,`ip`\n,`sso_user`\n,`sso_user_org`\n,`ip_user`\n,`ip_user_org`\n,`device_user`\n,`device_user_org`\n,`sso_user_base`\n,`ip_location`\n,`ip_attributes`\n,...<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d xxx_org \u7c7b\u7684\u6570\u636e\u662f l1,l2,l3,l4,l5 \u8fd9\u79cd\u9017\u53f7\u8fde\u63a5\u8d77\u6765\u7684\u5b57\u7b26\u4e32\uff0c\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u79cd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>`ip_user_org` = 'l1,l2,l3,l4,l5'\n\nsubstring_index(`ip_user_org`,',',1) as ip_user_1st_org,    -- l1\nsubstring_index(`ip_user_org`,',',2) as ip_user_2_org,    -- l1,l2\nsubstring_index(`ip_user_org`,',',3) as ip_user_3_org,    -- l1,l2,l3\nregexp_extract(`ip_user_org`, '(.+?),(&#91;^,]{1,})$', 1) as ip_user_x_org,    -- l1,l2,l3,l4\nsubstring_index(`ip_user_org`,',',-1) as ip_user_last_org,    -- l5<\/code><\/pre>\n\n\n\n<p>\u8865\u5145\u4e00\u4e0b\u4e4b\u524d\u8bb0\u5f55\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\u622a\u53d6\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p><strong>Hive SQL\u4e2d\u5982\u4f55\u622a\u53d6\u300c\u53d8\u957f\u300d\u300c\u7279\u5b9a\u5206\u9694\u7b26\u300d\u5b57\u7b26\u4e32\u7684\u9664\u6700\u540e\u4e00\u6bb5\u5185\u5bb9\u4e4b\u5916\u7684\u5185\u5bb9\uff1f<br>\u5982\u679c\u683c\u5f0f\u786e\u5b9a\u5bf9\u8bdd\uff0c\u5efa\u8bae\u4f7f\u7528 regexp_extract \u8fdb\u884c\u63d0\u53d6<\/strong>\uff1b\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u7528 substring_index \u6765\u64cd\u4f5c\uff0c\u4ece\u524d\u5f80\u540e\u6216\u4ece\u540e\u5f80\u524d\u53d6\u56fa\u5b9a\u6570\u91cf\u5206\u9694\u7b26\u7684\u5b50\u4e32\uff0c\u5b9e\u5728\u4e0d\u5acc\u9ebb\u70e6\u518d\u52a0\u4e00\u4e2a replace \u5c31\u884c\u3002<\/p>\n\n\n\n<p>\u6bd4\u8f83\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u90e8\u95e8\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u8fd9\u91cc\u4ee5 ip_user_org \u548c sso_user_org \u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>case\nwhen ip_user_org = sso_user_org then 'a=b'\nwhen ip_user_org like concat(sso_user_org,'%') then 'a_like_b'\nwhen sso_user_org like concat(ip_user_org,'%') then 'a_like_b'\nwhen substring_index(`ip_user_org`,',',3) = substring_index(`sso_user_org`,',',3) then 'a-b_common-3org'\nelse 'unknown'\nend as org_relation,\n\n# \u4e0a\u9762\u8fd9\u90e8\u5206\u7684\u6838\u5fc3\u5728\u4e8e\u5c06 column1\/column2 \u7684\u5185\u5bb9\u5148\u8f6c\u6210\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u7528 concat \u51fd\u6570\u7ed9\u6dfb\u52a0\u4e00\u4e2a % \u4ee5\u4fbf\u8fdb\u884clike\u6a21\u7cca\u5339\u914d\u3002\n# \u5982\u679c\u8981\u7528 rlike \u505a\u6b63\u5219\u5339\u914d\uff0c\u4e5f\u7c7b\u4f3c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u5c31\u662f\u8868\u793a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u90a3\u4e00\u5217\u5185\u5bb9\u9700\u8981\u63d0\u524d\u5904\u7406\u597d\u8f6c\u4e49\u5b57\u7b26\uff0c\u5426\u5219\u8fbe\u4e0d\u5230\u9884\u671f\u76ee\u6807\u3002<\/code><\/pre>\n\n\n\n<p>\u7528\u4e00\u4e9b\u7b80\u5355\u7684\u6848\u4f8b\u6765\u6a21\u62df\u9a8c\u8bc1\u4e0a\u9762\u5224\u65ad\u903b\u8f91\u662f\u5426\u80fd\u6ee1\u8db3\u76ee\u6807\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6709\u5173\u7cfb\u7684\u4f8b\u5b50\n\nexample1:\n\nip_user_org = 'l1,l2,l3,l4,l5'\nsso_user_org = 'l1,l2,l3,l4,l5'\n\na=b\n\nexample2:\n\nip_user_org = 'l1,l2,l3,l4'\nsso_user_org = 'l1,l2,l3,l4,l5'\n\nip_user_org like concat(sso_user_org,'%') -&gt; false\nsso_user_org like concat(ip_user_org,'%') -&gt; true\n\nexample3:\n\nsso_user_org = 'l1,l2,l3,l4'\nip_user_org = 'l1,l2,l3,l4,l5'\n\nip_user_org like concat(sso_user_org,'%') -&gt; true\nsso_user_org like concat(ip_user_org,'%') -&gt; false\n\nexample4:\n\nsso_user_org = 'l1,l2,l3,l4,lx'\nip_user_org = 'l1,l2,l3,l4,l5'\n\nip_user_org like concat(sso_user_org,'%') -&gt; false\nsso_user_org like concat(ip_user_org,'%') -&gt; false\nsubstring_index(`ip_user_org`,',',3) = substring_index(`sso_user_org`,',',3) -&gt; true\n\nexample5:\n\nsso_user_org = 'l1,l2,l3,l4,lx'\nip_user_org = 'l1,l2,l3,l4,l5'\n\nip_user_org like concat(sso_user_org,'%') -&gt; false\nsso_user_org like concat(ip_user_org,'%') -&gt; false\nsubstring_index(`ip_user_org`,',',3) = substring_index(`sso_user_org`,',',3) -&gt; true\n\n&#91;\u4e0d\u53ca\u9884\u671f]example6:\n\nsso_user_org = 'l1,l2,lx'\nip_user_org = 'l1,l2,l3'\n\nip_user_org like concat(sso_user_org,'%') -&gt; false\nsso_user_org like concat(ip_user_org,'%') -&gt; false\nsubstring_index(`ip_user_org`,',',3) = substring_index(`sso_user_org`,',',3) -&gt; false\n\n# \u6ca1\u6709\u5173\u7cfb\u7684\u4f8b\u5b50\n\nexample7:\n\nsso_user_org = 'l1,l2,l3,l4,lx'\nip_user_org = 'a1,a2,a3,a4'\n\nip_user_org = sso_user_org -&gt; false\nip_user_org like concat(sso_user_org,'%') -&gt; false\nsso_user_org like concat(ip_user_org,'%') -&gt; false\nsubstring_index(`ip_user_org`,',',3) = substring_index(`sso_user_org`,',',3) -&gt; false<\/code><\/pre>\n\n\n\n<p>\u597d\u4e86\uff0c\u6682\u65f6\u5148\u8bb0\u5f55\u5230\u8fd9\u91cc\uff0c\u540e\u9762\u518d\u89c6\u60c5\u51b5\u5199\u4e9b\u65b0\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u8865\u5145\u4e00\u4e0b\u7f51\u4e0a\u641c\u5230\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># method 1\nCASE\nWHEN CONCAT('%',\"B\",'%') LIKE CONCAT('%',\"A\",'%')  THEN 1 \nWHEN CONCAT('%',\"A\",'%') LIKE CONCAT('%',\"B\",'%') THEN 1\nELSE 0\nEND AS col_relation\n\n# method 2\ninstr(column1, column2) !=0<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>\u4ece SSO \u8d26\u53f7\u5b89\u5168\u76d1\u63a7\u8bf4\u8d77<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/5329.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/ixyzero.com\/blog\/archives\/5329.html<\/a><\/p>\n\n\n\n<p>How to compare two columns using partially match result to display<br><a href=\"https:\/\/community.yellowfinbi.com\/topic\/how-to-compare-two-columns-using-partially-match-result-to-display\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/community.yellowfinbi.com\/topic\/how-to-compare-two-columns-using-partially-match-result-to-display<\/a><\/p>\n\n\n\n<p>hive join tables and extracts value based on column values<br><a href=\"https:\/\/community.cloudera.com\/t5\/Support-Questions\/hive-join-tables-and-extracts-value-based-on-column-values\/td-p\/196804\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/community.cloudera.com\/t5\/Support-Questions\/hive-join-tables-and-extracts-value-based-on-column-values\/td-p\/196804<\/a><\/p>\n\n\n\n<p>How to compare two columns with different data type groups<br><a href=\"https:\/\/stackoverflow.com\/questions\/58240566\/how-to-compare-two-columns-with-different-data-type-groups\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/58240566\/how-to-compare-two-columns-with-different-data-type-groups<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728\u5904\u7406SSO\u8d26\u53f7\u5b89\u5168\u7684\u95ee\u9898\u65f6\uff0c\u5355\u6b65\u6838\u5fc3\u5728\u4e8e\u5bf9SSO\u8ba4\u8bc1\u65e5\u5fd7\u7684\u5206\u6790\uff08\u6df1\u5c42\u6b21\u7684\u8fd8\u9700\u8981\u7ed3\u5408\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1261,1870,483,1262,1853],"class_list":["post-5353","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-hive","tag-instr","tag-like","tag-sql","tag-sso"],"views":2432,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5353","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=5353"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5353\/revisions"}],"predecessor-version":[{"id":5354,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5353\/revisions\/5354"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}