{"id":5362,"date":"2022-11-17T11:00:28","date_gmt":"2022-11-17T03:00:28","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5362"},"modified":"2022-11-17T11:00:28","modified_gmt":"2022-11-17T03:00:28","slug":"hive-sql%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e6%97%b6%e9%97%b4%e5%a4%84%e7%90%86%ef%bf%bc","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5362.html","title":{"rendered":"Hive SQL\u4e2d\u7684\u65e5\u671f\u65f6\u95f4\u5904\u7406\ufffc"},"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\u7528Hive SQL\u505a\u6570\u636e\u5206\u6790\u7684\u65f6\u5019\uff0c\u4e00\u822c\u5c11\u4e0d\u4e86\u548c\u65e5\u671f\u65f6\u95f4\u51fd\u6570\u6253\u4ea4\u9053\uff0c\u4f46\u662f\u6211\u53ef\u80fd\u662f\u7528\u7684\u8fd8\u4e0d\u591f\u591a\u3001\u4e0d\u591f\u719f\u7ec3\uff0c\u6240\u4ee5\u6211\u6bcf\u6b21\u8981\u5b9e\u73b0\u67d0\u4e2a\u529f\u80fd\u7684\u65f6\u5019\u8fd8\u9700\u8981\u53bb\u73b0\u67e5\uff0c\u4e0d\u662f\u592a\u65b9\u4fbf\uff0c\u6240\u4ee5\u60f3\u7740\u81ea\u5df1\u6309\u7167\u81ea\u5df1\u7684\u4e60\u60ef\u548c\u9700\u6c42\u505a\u4e00\u4e2a\u6574\u7406\uff0c\u65b9\u4fbf\u540e\u9762\u5fd8\u4e86\u7684\u65f6\u5019\u5feb\u901f\u53c2\u8003\u3002<\/p>\n\n\n\n<p>\u5728\u7ecf\u8fc7\u591a\u6b21\u641c\u7d22\u4e4b\u540e\u53d1\u73b0\u4e86\u4e0d\u5c11\u4e0d\u9519\u7684\u6587\u7ae0\uff0c\u8fd9\u91cc\u6309\u7167\u4e2a\u4eba\u9700\u6c42\u9009\u53d6\u90e8\u5206\u5185\u5bb9\u8fdb\u884c\u6458\u5f55\uff0c\u540e\u9762\u518d\u6709\u590d\u6742\u9700\u6c42\u7684\u76f4\u63a5\u53bb\u5b98\u65b9\u6587\u6863\u4e2d\u8fdb\u884c\u67e5\u8be2\u548c\u7ec4\u5408\u5b9e\u73b0\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>\u51e0\u4e2a\u524d\u7f6e\u8bf4\u660e\u9879\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Hive SQL\u4e2d\u7684 timestamp \u7c7b\u578b\u6570\u636e\u663e\u793a\u683c\u5f0f\u662f <code>2022-11-16 16:07:13.816<\/code> \u8fd9\u6837\u7684\uff1b<\/li><li>\u800c\u6211\u4eec\u5e38\u8bf4\u7684\u65f6\u95f4\u6233\u662f\u4e00\u4e2a bigint \u7c7b\u578b\u7684\u6570\u5b57\uff0c\u901a\u8fc7 unix_timestamp() \u53ef\u83b7\u53d6\u5f53\u524dUnix\u65f6\u95f4\u6233\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff0c10\u4f4d\u6570\uff09\uff1b<\/li><li>Hive SQL\u4e2d timestamp \u7c7b\u578b\u7684\u5217\u662f\u53ef\u4ee5\u76f4\u63a5\u6bd4\u8f83\u7684\uff0c\u52a0\u51cf\u64cd\u4f5c\u8fd4\u56de\u5185\u5bb9\u7684\u683c\u5f0f\u4e5f\u5f88\u53cb\u597d\uff0c\u662f\u65f6\u5206\u79d2\u7684\u683c\u5f0f\uff1b\u800c unixtime \u76f4\u63a5\u6bd4\u8f83\u662f\u76f8\u5dee\u7684\u79d2\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f46\u6570\u503c\u5927\u4e86\u4e4b\u540e\u4e0d\u76f4\u89c2\uff0c\u4f46\u53ef\u4ee5\u6309\u81ea\u5df1\u7684\u9700\u6c42\u505a\u8f6c\u6362\uff0c\u5176\u5b9e\u4e5f\u5f88\u7075\u6d3b\u3002<\/li><\/ul>\n\n\n\n<p>\u6211\u7684\u7b80\u5355\u7c97\u66b4\u7684\u8bb0\u5fc6\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>unixtime \u4e3a bigint \u7c7b\u578b\u7684Unix\u65f6\u95f4\u6233\uff0c\u6240\u4ee5 from_unixtime \u51fd\u6570\u7528\u4e8e<strong>\u5c06Unix\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/strong>\uff1b<\/li><li>timestamp \u4e3a yyyy-MM-dd HH:mm:ss \u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5 unix_timestamp \u51fd\u6570\u7528\u4e8e\u63a5\u6536\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aUnix\u65f6\u95f4\u6233(\u4ee5\u79d2\u4e3a\u5355\u4f4d)\uff0c\u5982\u679c\u5931\u8d25\u8fd4\u56denull\u3002<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u6210unix\u65f6\u95f4\u6233<\/h4>\n\n\n\n<p>\u51fd\u6570: unix_timestamp()<br>\u8fd4\u56de\u503c\u7c7b\u578b: bigint<br>(hive 2.0 &gt; version)\u901a\u8fc7 unix_timestamp() \u51fd\u6570\u83b7\u53d6\uff0c\u8fd4\u56de\u5f53\u524dUnix\u65f6\u95f4\u6233(\u4ee5\u79d2\u4e3a\u5355\u4f4d)\u3002<\/p>\n\n\n\n<p>\u51fd\u6570: unix_timestamp(string date)<br>\u8fd4\u56de\u503c\u7c7b\u578b: bigint<br>\u8fd4\u56de\u65e5\u671f\u5b57\u7b26\u4e32 date \uff08\u683c\u5f0f\u4e3a yyyy-MM-dd HH:mm:ss \uff09\u5bf9\u5e94\u7684Unix\u65f6\u95f4\u6233(\u4ee5\u79d2\u4e3a\u5355\u4f4d)\uff0c\u82e5\u5931\u8d25\u8fd4\u56denull\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\nunix_timestamp() as ts_bigint -- \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u6233 (1668586033)\n,unix_timestamp('2022-11-16') as ts_bigint2 -- null \u56e0\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u7b26\u5408\u8981\u6c42\u6240\u4ee5\u89e3\u6790\u5931\u8d25\u8fd4\u56denull\n,unix_timestamp('2022-11-16 00:00:00') as ts_bigint3 -- 1668528000\n,unix_timestamp('2022-11-16 18:31:36') as ts_bigint4 -- 1668594696<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5c06unix\u65f6\u95f4\u6233\u5bf9\u5e94\u7684\u79d2\u503c\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/h4>\n\n\n\n<p>\u51fd\u6570\uff1a from_unixtime(bigint unixtime[, string format])<br>\u8fd4\u56de\u503c\uff1astring<br>\u5c06unix\u65f6\u95f4\u6233\u5bf9\u5e94\u7684\u79d2\u6570\u8f6c\u6362\u4e3a\u8868\u793a\u5f53\u524d\u7cfb\u7edf\u65f6\u533a\u4e2d\u8be5\u65f6\u523b\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\uff0c\u9ed8\u8ba4\u683c\u5f0f\u4e3a\u201cyyyy-MM-dd HH:mm:ss\u201d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\nfrom_unixtime(1668594696) -- '2022-11-16 18:31:36'\n,from_unixtime(1668594696,'yyyy-MM-dd HH-mm-ss') -- '2022-11-16 18-31-36'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u65f6\u95f4<\/h4>\n\n\n\n<p>\u51fd\u6570: current_timestamp<br>\u8fd4\u56de\u503c\u7c7b\u578b: timestamp (\u6bd4\u5982 2022-11-16 16:07:13.816)<br>\u8fd4\u56de\u67e5\u8be2\u8ba1\u7b97\u5f00\u59cb\u65f6\u7684\u5f53\u524d\u65f6\u95f4\u6233(\u4eceHive 1.2.0\u5f00\u59cb)\u3002\u5728\u540c\u4e00\u4e2a\u67e5\u8be2\u4e2d\u5bf9current_timestamp\u7684\u6240\u6709\u8c03\u7528\u90fd\u8fd4\u56de\u76f8\u540c\u7684\u503c\u3002(hive 2.0 &lt;= version)\u5efa\u8bae\u4f7f\u7528 CURRENT_TIMESTAMP \u5e38\u91cf\u8fdb\u884c\u83b7\u53d6\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u51fd\u6570: current_date<br>\u8fd4\u56de\u503c\u7c7b\u578b: date<br>\u8fd4\u56de\u67e5\u8be2\u8ba1\u7b97\u5f00\u59cb\u65f6\u7684\u5f53\u524d\u65e5\u671f(\u4eceHive 1.2.0\u5f00\u59cb)\u3002\u5728\u540c\u4e00\u4e2a\u67e5\u8be2\u4e2dcurrent_date\u7684\u6240\u6709\u8c03\u7528\u90fd\u8fd4\u56de\u76f8\u540c\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\ncurrent_timestamp() as ts_str -- \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u6233 (2022-11-16 16:07:13.816)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570<\/h4>\n\n\n\n<p>\u51fd\u6570: datediff(string enddate, string startdate)<br>\u8fd4\u56de\u503c\u7c7b\u578b: int<br>\u8fd4\u56de\u4ece startdate \u5230 enddate \u7684\u5929\u6570\uff0c\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\u524d\u8005\u51cf\u53bb\u540e\u8005(enddate-startdate)\uff0c\u6216\u8005\u8bf4\u540e\u8005\u518d\u7ecf\u8fc7\u51e0\u5929\u4f1a\u5230\u524d\u8005\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\ndatediff('2022-11-16', '2022-11-13') as day_range -- 3<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u65e5\u671f\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h4>\n\n\n\n<p>\u51fd\u6570\uff1a date_format(date\/timestamp\/string ts, string fmt)<br>\u8fd4\u56de\u503c\uff1astring<br>\u5c06\u65e5\u671f\/\u65f6\u95f4\u6233\/\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7531\u7b2c2\u4e2a\u53c2\u6570 fmt \u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u503c\uff08\u4ece Hive 1.2.0 \u5f00\u59cb\u652f\u6301\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\ndate_format('2022-11-09','yyyy-MM-dd') -- '2022-11-09'\n,date_format('2022-11-09','yyyy-MM-dd HH:mm:ss') -- '2022-11-09 00:00:00'\n,date_format('2022-11-11 15:30:12.084','yyyy-MM-dd') -- '2022-11-11'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728\u7279\u5b9a\u65e5\u671f\u4e0a\u52a0\u51cf\u6307\u5b9a\u5929\u6570<\/h4>\n\n\n\n<p>date_add\/date_sub \u7528\u4e8e\u5bf9\u6307\u5b9a\u65e5\u671f\u589e\u52a0\/\u51cf\u53bb\u7279\u5b9a\u5929\u6570\u5e76\u83b7\u53d6\u7ed3\u679c\u65e5\u671f\u3002<\/p>\n\n\n\n<p>\u51fd\u6570\uff1a date_add(date\/timestamp\/string startdate, tinyint\/smallint\/int days)<br>\u8fd4\u56de\u503c\uff1a<br>pre 2.1.0: string<br>2.1.0 on: date<br>\u7ed9startdate\u6dfb\u52a0\u5929\u6570\uff0c\u5728Hive 2.1.0 (Hive -13248)\u4e4b\u524d\uff0c\u8fd4\u56de\u7c7b\u578b\u662fString\uff0c\u56e0\u4e3a\u5f53\u65f6\u521b\u5efa\u8fd9\u4e2a\u65b9\u6cd5\u65f6\u8fd8\u6ca1\u6709Date\u7c7b\u578b\u5b58\u5728\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\ndate_add('2008-12-31', 1) -- '2009-01-01'\n;\n\nwith t as (\n  select '2020-12-28' as dt_start, '2021-01-12' as dt_end\n)\nselect\nidx, date_add(to_date(dt_start), idx) as dt\nfrom t\nlateral view posexplode(split(space(datediff(t.dt_end,t.dt_start)),' ')) pi as idx, empty\n;\n\nidx dt  \n0 2020-12-28\n1 2020-12-29\n2 2020-12-30\n3 2020-12-31\n4 2021-01-01\n...\n13  2021-01-10\n14  2021-01-11\n15  2021-01-12<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5bf9 timestamp \u7c7b\u578b\u7684\u5217\u8fdb\u884c\u6bd4\u8f83<\/h4>\n\n\n\n<p>\u73b0\u5728\u60f3\u8ba1\u7b972\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u95f4\u9694\u6709\u591a\u5927\uff0c\u5728Hive SQL\u4e2d\u8be5\u600e\u4e48\u5904\u7406\uff1f\u89e3\u51b3\u529e\u6cd5\u2014\u2014\u76f4\u63a5\u6bd4\u8f83\u5c31\u884c\uff0c\u5e76\u4e14\u8fd4\u56de\u5185\u5bb9\u7684\u683c\u5f0f\u4e5f\u5f88\u53cb\u597d\uff0c\u65f6\u5206\u79d2\u7684\u683c\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>share_timestamp TIMESTAMP #\u65e5\u671f1\nopen_timestamp  TIMESTAMP #\u65e5\u671f2\n\nselect\n`share_timestamp`,\n`open_timestamp`,\n`open_timestamp`-`share_timestamp` as time_delay\n;\n\n2022-11-05 22:47:17.315 2022-11-05 22:47:28.712 11.397 seconds\n2022-11-05 09:52:51.805 2022-11-05 09:53:25.856 34.051 seconds\n2022-11-05 22:33:25.36  2022-11-05 23:04:50.45  31 minutes 25.09 seconds\n2022-11-05 02:16:17.241 2022-11-05 14:26:08.578 12 hours 9 minutes 51.337 seconds<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>Hive SQL\u65f6\u95f4\u51fd\u6570\u53ca\u7528\u6cd5<br><a href=\"https:\/\/juejin.cn\/post\/7137207230713036831\">https:\/\/juejin.cn\/post\/7137207230713036831<\/a><\/p>\n\n\n\n<p>Date Functions #\u5b98\u65b9\u6587\u6863\uff0c\u6700\u6743\u5a01\u4e5f\u6700\u53ca\u65f6<br><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-DateFunctions\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF#LanguageManualUDF-DateFunctions<\/a><\/p>\n\n\n\n<p>Hive Date and Timestamp Functions | Examples<br><a href=\"https:\/\/sparkbyexamples.com\/apache-hive\/hive-date-and-timestamp-functions-examples\/\">https:\/\/sparkbyexamples.com\/apache-hive\/hive-date-and-timestamp-functions-examples\/<\/a><\/p>\n\n\n\n<p>hive \u4e2d\u65f6\u95f4\u6233\u4e0e\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u76f8\u4e92\u8f6c\u6362<br><a href=\"https:\/\/blog.csdn.net\/HappyRocking\/article\/details\/80854778\">https:\/\/blog.csdn.net\/HappyRocking\/article\/details\/80854778<\/a><\/p>\n\n\n\n<p>Introduction to Date Functions in Hive<br><a href=\"https:\/\/www.educba.com\/date-functions-in-hive\/\">https:\/\/www.educba.com\/date-functions-in-hive\/<\/a><\/p>\n\n\n\n<p>Hive Date Functions<br><a href=\"http:\/\/hadooptutorial.info\/hive-date-functions\/\">http:\/\/hadooptutorial.info\/hive-date-functions\/<\/a><\/p>\n\n\n\n<p>Hive Functions Cheat-sheet, by Qubole<br><a href=\"https:\/\/www.qubole.com\/wp-content\/uploads\/2014\/01\/hive-function-cheat-sheet.pdf\">https:\/\/www.qubole.com\/wp-content\/uploads\/2014\/01\/hive-function-cheat-sheet.pdf<\/a><\/p>\n\n\n\n<p>How to Subtract TIMESTAMP-DATE-TIME in HIVE<br><a href=\"https:\/\/sqlandhadoop.com\/how-to-subtract-timestamp-date-time-in-hive\/\">https:\/\/sqlandhadoop.com\/how-to-subtract-timestamp-date-time-in-hive\/<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728\u7528Hive SQL\u505a\u6570\u636e\u5206\u6790\u7684\u65f6\u5019\uff0c\u4e00\u822c\u5c11\u4e0d\u4e86\u548c\u65e5\u671f\u65f6\u95f4\u51fd\u6570\u6253\u4ea4\u9053\uff0c\u4f46\u662f\u6211\u53ef\u80fd\u662f\u7528\u7684 [&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,7],"tags":[1724,1881,1261,1262,1879,1880],"class_list":["post-5362","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","tag-datediff","tag-from_unixtime","tag-hive","tag-sql","tag-timestamp","tag-unix_timestamp"],"views":2651,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5362","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=5362"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5362\/revisions"}],"predecessor-version":[{"id":5363,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5362\/revisions\/5363"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}