{"id":5380,"date":"2023-01-12T20:00:09","date_gmt":"2023-01-12T12:00:09","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5380"},"modified":"2023-01-12T20:00:09","modified_gmt":"2023-01-12T12:00:09","slug":"hive-sql%e7%94%a8%e5%ae%8f%e5%ae%9e%e7%8e%b0%e5%b8%b8%e7%94%a8%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5380.html","title":{"rendered":"Hive SQL\u7528\u5b8f\u5b9e\u73b0\u5e38\u7528\u529f\u80fd"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7b80\u5355\u6574\u7406\u4e00\u4e0b\u8fd1\u671f\u60f3\u5230\u7684\u5e38\u7528SQL\u4ee3\u7801\u7247\u6bb5\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\uff0c\u4e0d\u65ad\u79ef\u7d2f\u4e0d\u65ad\u63d0\u6548\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<h5 class=\"wp-block-heading\">1. \u5e38\u7528\u5b8f&#8211;\u5c06\u6570\u5b57\u683c\u5f0f\u7684IPv4\u8f6c\u6362\u6210\u70b9\u5206\u5341\u8fdb\u5236\u683c\u5f0f\u7684IPv4\u5b57\u7b26\u4e32<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5c06\u6570\u5b57\u683c\u5f0f\u7684IPv4\u8f6c\u6362\u6210\u70b9\u5206\u5341\u8fdb\u5236\u683c\u5f0f\u7684IPv4\u5b57\u7b26\u4e32\ncreate temporary macro IPint2str (x bigint) concat_ws('.',CONV(substr(hex(x),1,2),16,10),CONV(substr(hex(x),3,2),16,10),CONV(substr(hex(x),5,2),16,10),CONV(substr(hex(x),7,2),16,10));\n\nselect\nconcat_ws('.',\nconv(substr(lpad(conv(3232267034, 10, 2), 32, 0), 1, 8), 2, 10),\nconv(substr(lpad(conv(3232267034, 10, 2), 32, 0), 9, 8), 2, 10),\nconv(substr(lpad(conv(3232267034, 10, 2), 32, 0), 17, 8), 2, 10),\nconv(substr(lpad(conv(3232267034, 10, 2), 32, 0), 25, 8), 2, 10)\n)\n, concat_ws('.',\nCONV(substr(hex(3232267034),1,2),16,10),\nCONV(substr(hex(3232267034),3,2),16,10),\nCONV(substr(hex(3232267034),5,2),16,10),\nCONV(substr(hex(3232267034),7,2),16,10)\n)\n, IPint2str(3232267034)\n;\n-- 192.168.123.26    192.168.123.26  192.168.123.26<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2. \u5e38\u7528\u5b8f&#8211;\u5224\u65adIPv4\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u5185\u7f51IP<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5185\u7f51IP\u6bb5\u7684\u5212\u5206\uff1a\n192.168.0.0 \u2013 192.168.255.255 (65,536 IP addresses)\n172.16.0.0 \u2013 172.31.255.255 (1,048,576 IP addresses)\n10.0.0.0 \u2013 10.255.255.255 (16,777,216 IP addresses)\n\n-- \u5224\u65ad\u5b57\u7b26\u4e32\u683c\u5f0f\u7684IPv4\u662f\u5426\u4e3a\u5185\u7f51IP\u7684\u5b8f\ncreate temporary macro is_private_ipv4(clientip string) if(clientip like '10.%'\nor clientip like '192.168.%'\nor clientip rlike '^172\\\\.(1&#91;6-9]|2&#91;0-9]|3&#91;0-1])\\\\.', 1, 0);\n\nselect\nis_private_ipv4('172.15.20.17')\n,is_private_ipv4('172.20.18.32')\n,is_private_ipv4('172.35.20.17')\n;\n-- 0    1   0<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">3. \u5e38\u7528\u5b8f&#8211;\u5c06\u70b9\u5206\u5341\u8fdb\u5236\u683c\u5f0f\u7684IPv4\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>create temporary macro IPstr2int (x string) cast(split(x, '\\\\.') &#91;0] * 256 * 256 * 256 + split(x, '\\\\.') &#91;1] * 256 * 256 + split(x, '\\\\.') &#91;2] * 256 + split(x, '\\\\.') &#91;3] AS bigint);\n\ncreate temporary macro IPstr2int2 (x string) (shiftleft(cast(split(x,'\\\\.')&#91;0] as bigint),24) \n+shiftleft(cast(split(x,'\\\\.')&#91;1] as bigint),16)\n+shiftleft(cast(split(x,'\\\\.')&#91;2] as bigint),8)\n+cast(split(x,'\\\\.')&#91;3] as bigint));\n\nSELECT\n  cast(\n    split('192.168.123.26', '\\\\.') &#91;0] * 256 * 256 * 256 + split('192.168.123.26', '\\\\.') &#91;1] * 256 * 256 + split('192.168.123.26', '\\\\.') &#91;2] * 256 + split('192.168.123.26', '\\\\.') &#91;3] AS bigint\n  ) AS ip_bigint\n, IPstr2int('192.168.123.26')\n, IPstr2int2('192.168.123.26')\n;\n-- 3232267034    3232267034  3232267034<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">4. \u4e00\u70b9\u60f3\u6cd5&#8211;\u7528SQL\u5982\u4f55\u5224\u65ad\u67d0\u4e2aIP\u662f\u5426\u5728\u67d0\u4e2a\u7f51\u6bb5\u5185\uff1f<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5728SQL\u91cc\u9762\u56e0\u4e3a\u4e0d\u50cfPython\/Java\u90a3\u6837\u5e26\u6709\u5f88\u591a\u73b0\u6210\u7684\u5e93\u6765\u505a\uff0c\u4e00\u822c\u601d\u8def\u5c31\u662f\uff1a\n-- 1. \u81ea\u5df1\u5199 UDF \u7136\u540e\u52a0\u8f7d\u4f7f\u7528\uff0c\u597d\u5904\u5728\u4e8e\u80fd\u5b9e\u73b0\u5f88\u591a\u81ea\u5b9a\u4e49\u7684\u529f\u80fd\uff0c\u4e0d\u8fc7\u9700\u8981\u5199\u4ee3\u7801\u800c\u4e14\u5c06jar\u5305\u4e0a\u4f20\u7b49\u989d\u5916\u6b65\u9aa4\n-- 2. \u7528 \u6b63\u5219\u8868\u8fbe\u5f0f \u6765\u5b9e\u73b0\uff0c\u597d\u5904\u5728\u4e8e\u4e0d\u7528\u5f15\u5165\u989d\u5916\u7684\u4f9d\u8d56\uff0c\u4f46\u9700\u8981\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u6bd4\u8f83\u719f\u6089\uff0c\u4e14\u5b9e\u73b0\u7cbe\u51c6\u4e86\u592a\u590d\u6742\uff0c\u5b9e\u73b0\u7b80\u5355\u4e86\u4e0d\u591f\u51c6\n-- 3. \u5c06 IP\u5b57\u7b26\u4e32\u5148\u8f6c\u6362\u6210\u6574\u6570\u683c\u5f0f\uff0c\u7136\u540e\u5224\u65ad\u8be5\u6574\u6570\u662f\u5426\u5728\u7f51\u6bb5\u9996\u5c3eIP\u7684\u6574\u6570\u8303\u56f4\u5185 \u8fd9\u4e5f\u662f\u4e2a\u529e\u6cd5<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">5. \u5e38\u7528\u5b8f&#8211;\u62bd\u53d6\u57df\u540d\u7684n\u7ea7\u5b50\u57df\u540d<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u62bd\u53d6\u5b8c\u6574\u57df\u540d\u7684\u51e0\u7ea7\u5b50\u57df\u540d\u7684\u5b8f\ncreate temporary macro subdomain_4(domain string) concat(\n  split(domain, '\\\\.') &#91;size(split(domain, '\\\\.'))-4]\n  ,'.', split(domain, '\\\\.') &#91;size(split(domain, '\\\\.'))-3]\n  ,'.', split(domain, '\\\\.') &#91;size(split(domain, '\\\\.'))-2]\n  ,'.', split(domain, '\\\\.') &#91;size(split(domain, '\\\\.'))-1]\n);\ncreate temporary macro subdomain_3(domain string) concat(\n  split(domain, '\\\\.') &#91;size(split(domain, '\\\\.'))-3]\n  ,'.', split(domain, '\\\\.') &#91;size(split(domain, '\\\\.'))-2]\n  ,'.', split(domain, '\\\\.') &#91;size(split(domain, '\\\\.'))-1]\n);\n\n-- \u63a8\u8350\u4e0b\u9762\u8fd9\u79cd reverse+substring_index \u7684\u65b9\u5f0f\uff0c\u6548\u7387\u7406\u8bba\u4e0a\u6765\u8bf4\u5e94\u8be5\u4e5f\u66f4\u9ad8\ncreate temporary macro subdomain_4x(domain string) reverse(substring_index(reverse(domain), '.', 4));\ncreate temporary macro subdomain_3x(domain string) reverse(substring_index(reverse(domain), '.', 3));\n\nwith t1 as (\nselect\n'a.b.c.d.com' as domain\n,'1.2.com' as domain2\n)\n\nselect\ndomain\n,subdomain_3(domain) as x3_1\n,subdomain_3x(domain) as x3_2\n,subdomain_4(domain) as x4_1\n,subdomain_4x(domain) as x4_2\n,domain2\n,subdomain_3(domain2) as y3_1\n,subdomain_3x(domain2) as y3_2\n,subdomain_4(domain2) as y4_1\n,subdomain_4x(domain2) as y4_2\nfrom t1\n;\n\n-- \u6570\u636e\u683c\u5f0f\u6b63\u5e38\u7684\u60c5\u51b5\u4e0b\u90fd\u6ca1\u6709\u95ee\u9898\n-- domain        x3_1    x3_2    x4_1        x4_2\n-- a.b.c.d.com    c.d.com c.d.com b.c.d.com   b.c.d.com\n\n-- \u6570\u636e\u683c\u5f0f\u4e0d\u90a3\u4e48\u89c4\u8303\u7684\u60c5\u51b5\u4e0b\uff0c4\u7ea7\u5b50\u57df\u540d\u4e0a\u9762\u5c31\u51fa\u73b0\u4e86\u95ee\u9898\uff0c\u7528 concat \u8fde\u63a5\u901a\u8fc7 split \u5207\u5206\u7684\u8fd9\u79cd\u65b9\u5f0f\u76f4\u63a5\u8fd4\u56de\u7a7a\uff0c\u4e0d\u6ee1\u8db3\u9700\u6c42\n-- domain2    y3_1    y3_2    y4_1    y4_2\n-- 1.2.com    1.2.com 1.2.com         1.2.com<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">6. \u5e38\u7528\u5b8f&#8211;\u8fdb\u884cURL\u7f16\u89e3\u7801<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Hive\u4e2d\u5982\u4f55\u8fdb\u884cURL\u7f16\u89e3\u7801\ncreate temporary macro url_decode(input string) reflect(\"java.net.URLDecoder\", \"decode\",trim(input),\"UTF-8\");\ncreate temporary macro url_encode(input string) reflect(\"java.net.URLEncoder\", \"encode\",input,\"UTF-8\");<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">7. \u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362&#8211;\u7279\u6b8a\u683c\u5f0f\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u89e3\u6790<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5728\u505a\u65e5\u5fd7\u5206\u6790\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u78b0\u5230\u5404\u79cd\u5404\u6837\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u662f\u5e38\u89c4\u90a3\u79cd yyyy-MM-dd HH:mm:ss \u683c\u5f0f\u7684\u80af\u5b9a\u662f\u6700\u597d\uff0c\u4f46\u662f\u4e5f\u4fdd\u4e0d\u9f50\u6709\u4e00\u4e9b\u7279\u6b8a\u683c\u5f0f\u7684\uff0c\u6bd4\u5982\u6708\u4efd\u662f\u7528\u82f1\u6587\u5b57\u6bcd\u7f29\u5199\u7684\u8fd9\u79cd\uff0c\u4e4b\u524d\u6ca1\u600e\u4e48\u9047\u5230\u8fc7\uff0c\u8fd9\u6b21\u9047\u5230\u89e3\u51b3\u4e86\u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\n\nselect\n'Jan 09 2023 15:30:21' as input\n,unix_timestamp('Jan 09 2023 15:30:21', 'MMM dd yyyy HH:mm:ss') as parsed\n;\n-- input    parsed  \n-- Jan 09 2023 15:30:21    1673249421\n\n\nunix_timestamp(string date, string pattern)\n\nConvert time string with given pattern to Unix time stamp (in seconds), return 0 if fail.\n\u5728\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e2d\u6307\u5b9a\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u7136\u540e\u5c06\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u65f6\u95f4\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4ee5\u79d2\u4e3a\u683c\u5f0f\u7684unix\u65f6\u95f4\u6233\u6570\u5b57\uff0c\u5f53\u8f6c\u6362\u5931\u8d25\u65f6\u8fd4\u56de0\u3002\n\nExample: unix_timestamp('2009-03-20', 'uuuu-MM-dd') = 1237532400.\n\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u4e3a\uff1a\nuuuu-MM-dd HH:mm:ss\n2023-01-09 10:34:46\n\u4e0a\u9762\u8fd9\u79cd\u683c\u5f0f\u7684\uff0c\u5e74\/\u65e5\/\u65f6\/\u5206\/\u79d2\u8fd9\u79cd\u7684\u5b57\u7b26\u5176\u5b9e\u8fd8\u6bd4\u8f83\u6e05\u695a\uff0c\u9ebb\u70e6\u5c31\u9ebb\u70e6\u5728\u6708\u4efd\u76843\u5b57\u7b80\u5199\u8be5\u600e\u4e48\u8868\u793a\u4e0a\u9762\uff0c\u8bd5\u4e86\u591a\u6b21\u624d\u786e\u8ba4\u662f MMM \u7528\u6765\u8868\u793a\u6708\u4efd\u76843\u4e2a\u5b57\u7b26\u7684\u82f1\u6587\u7f29\u5199\u3002\n\nMonth abbreviations consist of the first three characters of the month\u2019s name. Months with four-character names, such as June, are not abbreviated.\n\u53ea\u67093\u4e2a\u5b57\u7b26\u7684\u6708\u4efd\u5b57\u7b26\u4e32\u624d\u662f\u7f29\u5199\uff0c4\u4e2a\u5b57\u7b26\u7684\u4e0d\u662f\u3002\n\nYYMMMDD    #Last two digits of year, three-letter abbreviation of the month, two-digit day (example: 99JAN02)\nYYYYMMMDD    #Four-digit year, three-letter abbreviation of the month, two-digit day (example: 2003JUL04)<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/time\/format\/DateTimeFormatter.html\">https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/time\/format\/DateTimeFormatter.html<\/a> #pattern format<br><a href=\"https:\/\/docs.oracle.com\/cd\/E41183_01\/DR\/Date_Format_Types.html\">https:\/\/docs.oracle.com\/cd\/E41183_01\/DR\/Date_Format_Types.html<\/a><\/p>\n\n\n\n<p>Date\/Time Formats<br><a href=\"https:\/\/www.ibm.com\/docs\/en\/sgfmw\/5.3.1?topic=format-datetime-formats\">https:\/\/www.ibm.com\/docs\/en\/sgfmw\/5.3.1?topic=format-datetime-formats<\/a><\/p>\n\n\n\n<p>Hive SQL\u5b66\u4e60\u6574\u7406_6<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/5096.html\">https:\/\/ixyzero.com\/blog\/archives\/5096.html<\/a><\/p>\n\n\n\n<p>Hive SQL\u4e2d\u7684\u65e5\u671f\u65f6\u95f4\u5904\u7406\ufffc<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/5362.html\">https:\/\/ixyzero.com\/blog\/archives\/5362.html<\/a><\/p>\n\n\n\n<p>Convert IPv4 string to a BIGINT representation in spark sql<br><a href=\"https:\/\/stackoverflow.com\/questions\/40789834\/convert-ipv4-string-to-a-bigint-representation-in-spark-sql\">https:\/\/stackoverflow.com\/questions\/40789834\/convert-ipv4-string-to-a-bigint-representation-in-spark-sql<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u6574\u7406\u4e00\u4e0b\u8fd1\u671f\u60f3\u5230\u7684\u5e38\u7528SQL\u4ee3\u7801\u7247\u6bb5\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\uff0c\u4e0d\u65ad\u79ef\u7d2f\u4e0d\u65ad\u63d0\u6548\u3002 [&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,882,1262,1880,655,1191],"class_list":["post-5380","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","tag-hive","tag-macro","tag-sql","tag-unix_timestamp","tag-urldecode","tag-1191"],"views":1476,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5380","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=5380"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5380\/revisions"}],"predecessor-version":[{"id":5381,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5380\/revisions\/5381"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}