{"id":1519,"date":"2019-11-25T11:35:24","date_gmt":"2019-11-25T03:35:24","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=1519"},"modified":"2021-06-22T18:38:08","modified_gmt":"2021-06-22T10:38:08","slug":"%e6%95%b0%e4%bb%93%e5%ae%9e%e8%b7%b5%e4%b9%8b%e6%8b%89%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=1519","title":{"rendered":"\u7f13\u6162\u53d8\u5316\u7ef4\u89e3\u51b3\u65b9\u6848\u4e4b\u62c9\u94fe\u8868"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u62c9\u94fe\u8868\u662f\u4e00\u79cd\u6570\u636e\u6a21\u578b\uff0c\u4e3b\u8981\u662f\u9488\u5bf9\u6570\u636e\u4ed3\u5e93\u8bbe\u8ba1\u4e2d\u8868\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u800c\u5b9a\u4e49\u7684\uff1b\u987e\u540d\u601d\u4e49\uff0c\u6240\u8c13\u62c9\u94fe\u8868\uff0c\u5c31\u662f\u8bb0\u5f55\u5386\u53f2\uff0c\u8bb0\u5f55\u4e00\u4e2a\u4e8b\u52a1\u4ece\u5f00\u59cb\u4e00\u76f4\u5230\u5f53\u524d\u72b6\u6001\u7684\u6240\u6709\u53d8\u5316\u7684\u4fe1\u606f\u3002<br \/>\n\u62c9\u94fe\u8868\u53ef\u4ee5\u907f\u514d\u6309\u6bcf\u4e00\u5929\u5b58\u50a8\u6240\u6709\u8bb0\u5f55\u9020\u6210\u7684\u6d77\u91cf\u5b58\u50a8\u95ee\u9898\uff0c\u540c\u65f6\u4e5f\u662f\u5904\u7406\u7f13\u6162\u53d8\u5316\u6570\u636e\uff08SCD2\uff09\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u5f0f\u3002<br \/>\n\u5ba2\u6237\u65e0\u9700\u6bcf\u5929\u5168\u91cf\u5bfc\u5165\u6240\u6709\u6570\u636e\uff0c\u53ea\u9700\u8981\u5bf9\u53d8\u5316\u7684\u6570\u636e\u505a\u589e\u91cf\u5bfc\u5165\uff0c\u975e\u5e38\u9002\u7528\u4e8eORC\u4e8b\u52a1\u8868\u3002<br \/>\n\u76f8\u6bd4\u4e8e\u4f20\u7edf\u5207\u7247\u8868\uff0c\u8ba1\u7b97\u65f6\u5927\u5927\u51cf\u5c11\u4e86\u626b\u63cf\u8bb0\u5f55\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<p>\u9002\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li>\u6709\u4e00\u4e9b\u8868\u7684<strong>\u6570\u636e\u91cf\u5f88\u5927<\/strong>\uff0c\u6bd4\u5982\u4e00\u5f20\u7528\u6237\u8868\uff0c\u5927\u7ea610\u4ebf\u6761\u8bb0\u5f55\uff0c50\u4e2a\u5b57\u6bb5\uff0c\u8fd9\u79cd\u8868\uff0c\u5373\u4f7f\u4f7f\u7528ORC\u538b\u7f29\uff0c\u5355\u5f20\u8868\u7684\u5b58\u50a8\u4e5f\u4f1a\u8d85\u8fc7100G\uff0c\u5728HDFS\u4f7f\u7528\u53cc\u5907\u4efd\u6216\u8005\u4e09\u5907\u4efd\u7684\u8bdd\u5c31\u66f4\u5927\u4e00\u4e9b\u3002<\/li>\n<li>\u8868\u4e2d\u7684<strong>\u90e8\u5206\u5b57\u6bb5\u4f1a\u88abupdate\u66f4\u65b0<\/strong>\u64cd\u4f5c\uff0c\u5982\u7528\u6237\u8054\u7cfb\u65b9\u5f0f\uff0c\u4ea7\u54c1\u7684\u63cf\u8ff0\u4fe1\u606f\uff0c\u8ba2\u5355\u7684\u72b6\u6001\u7b49\u7b49\u3002<\/li>\n<li>\u9700\u8981\u67e5\u770b\u67d0\u4e00\u4e2a\u65f6\u95f4\u70b9\u6216\u8005\u65f6\u95f4\u6bb5\u7684<strong>\u5386\u53f2\u5feb\u7167<\/strong>\u4fe1\u606f\uff0c\u6bd4\u5982\uff0c\u67e5\u770b\u67d0\u4e00\u4e2a\u8ba2\u5355\u5728\u5386\u53f2\u67d0\u4e00\u4e2a\u65f6\u95f4\u70b9\u7684\u72b6\u6001\u3002<\/li>\n<li>\u8868\u4e2d\u7684\u8bb0\u5f55<strong>\u53d8\u5316\u7684\u6bd4\u4f8b\u548c\u9891\u7387\u4e0d\u662f\u5f88\u5927<\/strong>\uff0c\u6bd4\u5982\uff0c\u603b\u5171\u670910\u4ebf\u7684\u7528\u6237\uff0c\u6bcf\u5929\u65b0\u589e\u548c\u53d1\u751f\u53d8\u5316\u7684\u6709200\u4e07\u5de6\u53f3\uff0c\u53d8\u5316\u7684\u6bd4\u4f8b\u5360\u7684\u5f88\u5c0f\u3002<\/li>\n<\/ul>\n<p>\u82e5\u4e0d\u4f7f\u7528\u62c9\u94fe\u8868\uff0c\u89e3\u51b3\u65b9\u6848\u6b64\u5904\u7ed9\u51fa\u4e24\u79cd\uff1a<\/p>\n<ul>\n<li>\u65b9\u6848\u4e00\uff1a\u6bcf\u5929\u53ea\u7559\u6700\u65b0\u7684\u4e00\u4efd\uff0c\u6bd4\u5982\u6211\u4eec\u6bcf\u5929\u62bd\u53d6\u6700\u65b0\u7684\u4e00\u4efd\u5168\u91cf\u6570\u636e\u5230Inceptor\u4e2d\u3002\uff08\u4f46\u65e0\u6cd5\u67e5\u770b\u5386\u53f2\u6570\u636e\uff09<\/li>\n<li>\u65b9\u6848\u4e8c\uff1a\u6bcf\u5929\u4fdd\u7559\u4e00\u4efd\u5168\u91cf\u7684\u5207\u7247\u6570\u636e\u3002\uff08\u5b58\u50a8\u7a7a\u95f4\u5360\u7528\u91cf\u5927\uff0c\u65e0\u6cd5\u786e\u5b9a\u6570\u636e\u751f\u547d\u5468\u671f\uff0c\u4fdd\u7559\u591a\u5c11\u4efd\u5168\u91cf\u65e0\u6cd5\u63a7\u5236\uff09<\/li>\n<\/ul>\n<p>\u6b64\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u62c9\u94fe\u8868<\/p>\n<ul>\n<li>\u9996\u5148\u5b83\u5728\u7a7a\u95f4\u4e0a\u505a\u4e86\u4e00\u4e2a\u53d6\u820d\uff0c\u4e0d\u50cf\u65b9\u6848\u4e00\u90a3\u6837\u5360\u7528\u91cf\u90a3\u4e48\u5c0f\uff0c\u4f46\u662f\u5b83\u6bcf\u65e5\u7684\u589e\u91cf\u53ef\u80fd\u53ea\u6709\u65b9\u6848\u4e8c\u7684\u5343\u5206\u4e4b\u4e00\u751a\u81f3\u662f\u4e07\u5206\u4e4b\u4e00\u3002<\/li>\n<li>\u5176\u6b21\u5b83\u80fd\u6ee1\u8db3\u65b9\u6848\u4e8c\u6240\u80fd\u6ee1\u8db3\u7684\u9700\u6c42\uff0c\u65e2\u80fd\u83b7\u53d6\u6700\u65b0\u7684\u6570\u636e\uff0c\u4e5f\u80fd\u6dfb\u52a0\u7b5b\u9009\u6761\u4ef6\u4e5f\u83b7\u53d6\u5386\u53f2\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<hr \/>\n<h4>1. \u6784\u9020\u5386\u53f2\u62c9\u94fe\u8868<\/h4>\n<p>\u62c9\u94fe\u8868\u76f8\u8f83\u4e8e\u622a\u9762\u8868\uff0c\u591a\u4e86\u4e24\u5217\uff0cbegindt\u548cenddt\uff0c\u5206\u522b\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4\u548c\u7ed3\u675f\u65f6\u95f4<\/p>\n<pre><code class=\"language-sql\">--\u5386\u53f2\u62c9\u94fe\u8868(2016-11-01\u65e5\u6570\u636e)\n> DROP TABLE account_hs;\n> create table account_hs(\nacid string,balance int,begindt date,enddt date\n) \nclustered by(balance) into 1 buckets \nstored as ORC \ntblproperties(\"transactional\"=\"true\");\n> insert into account_hs values('B',1287,'2016-11-01','2999-12-31');\n> insert into account_hs values('C',8888,'2016-11-01','2999-12-31');\n> insert into account_hs values('A',4439,'2016-11-01','2999-12-31');<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2019\/11\/image-1574057448363.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-sql\">--2016-11-02\u622a\u9762\u8868\n> DROP TABLE account_20161102;\n> create table account_20161102(\nacid string,balance int,curdt date\n) \nclustered by(balance) \ninto 1 buckets \nstored as ORC \ntblproperties(\"transactional\"=\"true\");\n> insert into account_20161102 values('B',1287,'2016-11-02');\n> insert into account_20161102 values('C',8888,'2016-11-02');\n> insert into account_20161102 values('A',8234,'2016-11-02');<\/code><\/pre>\n<pre><code class=\"language-sql\">--2016-11-03\u622a\u9762\u8868\n> DROP TABLE account_20161103;\n> create table account_20161103(\nacid string,balance int,curdt date\n) \nclustered by(balance) \ninto 1 buckets \nstored as ORC \ntblproperties(\"transactional\"=\"true\");\n> insert into account_20161103 values('B',1287,'2016-11-03');\n> insert into account_20161103 values('A',8234,'2016-11-03');<\/code><\/pre>\n<p><strong>\u9996\u5148\u6211\u4eec\u5206\u6790\u4e0b2016-11-02\u548c2016-11-03\u8fd9\u4e24\u5929\u5bf9\u6570\u636e\u505a\u7684\u64cd\u4f5c\uff1a<\/strong><br \/>\n<strong>2016-11-02\u65e5\uff1a\u5bf9acid=A\u7684\u6570\u636e\u505a\u66f4\u6539\uff0c\u6240\u4ee5\u9700\u8981\u505a\u65b0\u589e\u6570\u636e\uff08step2\uff09\u548c\u5c01\u94fe\uff08step3\uff09\uff1b<\/strong><br \/>\n<strong>2016-11-03\u65e5\uff1a\u5bf9acid=C\u7684\u6570\u636e\u505a\u5220\u9664\uff0c\u6240\u4ee5\u9700\u8981\u505a\u5c01\u94fe\u5220\u9664\uff08step4\uff09\u3002<\/strong><\/p>\n<h4>2. \u65b0\u589e\u6570\u636e<\/h4>\n<pre><code class=\"language-sql\">--\u63d2\u51652016-11-02\u53d1\u751f\u53d8\u52a8\u7684\u6570\u636e\n> insert into table account_hs\nselect\n    acid, balance, begindt, enddt\nFROM\n    (select\n         acid, balance, curdt as begindt, cast('2999-12-31' as date) as enddt\n     from\n         account_20161102\n      except\n      SELECT\n          acid, balance, cast('2016-11-02' as date) as begindt, enddt\n      FROM\n          account_hs\n      WHERE\n           begindt <= '2016-11-01' AND enddt > '2016-11-01');<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2019\/11\/image-1574057539383.png\" alt=\"file\" \/><\/p>\n<h4>3. \u5c01\u94fe<\/h4>\n<pre><code class=\"language-sql\">--\u5c06acid=A\uff0cbalance=4439 \u8fd9\u6761\u6570\u636e\u7684enddt\u505a\u4fee\u6539\uff0c\u6709\u6548\u65e5\u671f\u622a\u6b622016-11-02\n> MERGE INTO account_hs m\n    USING (SELECT\n                a.begindt as begindt, a.acid as acid\n            FROM\n                account_hs a\n                INNER JOIN account_hs b\n                ON b.begindt = cast('2016-11-02' as date)\n                    AND a.acid = b.acid\n            WHERE\n                 a.begindt <= cast('2016-11-01' as date)\n                 AND a.enddt > cast('2016-11-01' as date)) c\n    ON(m.begindt = c.begindt and m.acid = c.acid)\nWHEN MATCHED THEN\n    UPDATE SET enddt = cast('2016-11-02' as date);<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2019\/11\/image-1574057577917.png\" alt=\"file\" \/><\/p>\n<h4>4. \u5220\u9664\u6570\u636e\u7684\u5c01\u94fe<\/h4>\n<pre><code class=\"language-sql\">--2016-11-03\u65e5acid=C\u7684\u6570\u636e\u88ab\u5220\u9664\uff0cenddt\u65e5\u671f\u4fee\u6539\u4e3a2016-11-03\u5f53\u5929\n> UPDATE account_hs a SET\n    a.enddt = cast('2016-11-03' as date)\nWHERE\n     NOT EXISTS(SELECT\n                    1\n                FROM\n                    account_20161103 b\n                WHERE\n                     a.acid = b.acid)\n     AND a.begindt <= cast('2016-11-03' as date)\n     AND a.enddt > cast('2016-11-03' as date);<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2019\/11\/image-1574057603679.png\" alt=\"file\" \/><\/p>\n<h4>5. \u67e5\u8be2\u6d4b\u8bd5<\/h4>\n<p><strong>1. \u6bd4\u5982\u6211\u60f3\u67e5\u8be22016-11-02\u65e5\u7684\u5386\u53f2\u6570\u636e<\/strong><\/p>\n<p><strong><code>&gt;  SELECT * FROM account_hs WHERE begindt &lt;= cast(&#039;2016-11-02&#039; as date) AND enddt &gt; cast(&#039;2016-11-02&#039; as date);<\/code><\/strong><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2019\/11\/image-1574057781158.png\" alt=\"file\" \/><\/p>\n<p><strong>2. \u6bd4\u5982\u6211\u60f3\u67e5\u8be2\u6700\u65b0\u7684\u6570\u636e<\/strong><\/p>\n<p><strong><code>&gt;  SELECT * FROM account_hs WHERE enddt = cast(&#039;2999-12-31&#039; as date);<\/code><\/strong><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2019\/11\/image-1574057887338.png\" alt=\"file\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 \u62c9\u94fe\u8868\u662f\u4e00\u79cd\u6570\u636e\u6a21\u578b\uff0c\u4e3b\u8981\u662f\u9488\u5bf9\u6570\u636e\u4ed3\u5e93\u8bbe\u8ba1\u4e2d\u8868\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u800c\u5b9a\u4e49\u7684\uff1b\u987e\u540d\u601d\u4e49\uff0c\u6240\u8c13\u62c9\u94fe\u8868\uff0c\u5c31\u662f\u8bb0 ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=1519\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[],"class_list":["post-1519","post","type-post","status-publish","format-standard","hentry","category-configuration"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/1519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1519"}],"version-history":[{"count":1,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/1519\/revisions"}],"predecessor-version":[{"id":1526,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/1519\/revisions\/1526"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}