{"id":6361,"date":"2021-07-23T11:20:13","date_gmt":"2021-07-23T03:20:13","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=6361"},"modified":"2025-03-17T15:53:45","modified_gmt":"2025-03-17T07:53:45","slug":"%e5%85%b3%e4%ba%8einsert-overwrite%e5%86%99%e5%88%b0hdfs%ef%bc%8cnull%e5%8f%98%e6%88%90n%e7%9a%84%e9%97%ae%e9%a2%98%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=6361","title":{"rendered":"\u5173\u4e8einsert overwrite\u5199\u5230hdfs\uff0cnull\u53d8\u6210\\N\u7684\u95ee\u9898\u5904\u7406"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>inceptor\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u9700\u8981\u5c06inceptor\u8868\u5378\u6570\u5230hdfs\uff0c\u4f9b\u4e0b\u6e38\u5e94\u7528\u8bfb\u6570\u7684\u60c5\u51b5\u3002\u800c<code>null<\/code>\u5728inceptor\u4e2d\u9ed8\u8ba4\u662f<code>\\N<\/code>\u7684\u5f62\u5f0f\u5b58\u5728\u7684\uff0c\u76f4\u63a5\u901a\u8fc7<code>insert overwrite directory<\/code>\u7684\u65b9\u5f0f\u5378\u6570\u5230hdfs\uff0c<code>null<\/code>\u4f1a\u53d8\u6210<code>\\N<\/code>\u3002\u672c\u6587\u4e3b\u8981\u9488\u5bf9\u8be5\u95ee\u9898\u63d0\u4f9b\u89e3\u51b3\u65b9\u6848\uff0c\u4ec5\u4f9b\u53c2\u8003\u3002<\/p>\n<p><strong>\u5148\u8bf4\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u65b9\u68481: <code>NULL DEFINED AS &#039;&#039;<\/code> \u65b9\u5f0f     <strong><font color=red>  =&gt;\u63a8\u8350<\/font><\/strong><br \/>\n\u65b9\u68482: <code>INSERT OVERWRITE DIRECTORY<\/code> + <code>WITH SERDEPROPERTIES<\/code>\u65b9\u5f0f<br \/>\n\u65b9\u68483: <code>INSERT OVERWRITE DIRECTORY<\/code> + <code>SED<\/code>\u66ff\u6362\u65b9\u5f0f<br \/>\n\u65b9\u68484: <code>CTAS<\/code> \u4e2d\u95f4\u8868\u8fc7\u6e21\u65b9\u5f0f<br \/>\n\u62d3\u5c551\uff1a\u5982\u4f55\u5bfc\u51fa\u591a\u5b57\u6bb5\u5206\u9694\u7b26\u7684\u6570\u636e\u6587\u4ef6<\/p>\n<h3>\u95ee\u9898\u73b0\u8c61<\/h3>\n<hr \/>\n<p><strong>\u6784\u5efa\u539f\u59cb\u6570\u636e<\/strong><\/p>\n<pre><code class=\"language-sql\">DROP TABLE IF EXISTS org_torc;\nCREATE TABLE org_torc (id int,name STRING,age int) CLUSTERED BY (id) INTO 3 BUCKETS STORED AS ORC_TRANSACTION;\nINSERT INTO org_torc VALUES (1,'zhangsan',15);\nINSERT INTO org_torc VALUES (2,null,16);<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/07\/image-1627008951354.png\" alt=\"file\" \/><\/p>\n<p><strong>\u5378\u6570\u5230hdfs\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">INSERT OVERWRITE DIRECTORY '\/tmp\/staging_text'\nROW FORMAT DELIMITED FIELDS TERMINATED BY '|'\nSELECT id,name,age FROM org_torc;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/07\/image-1627009737893.png\" alt=\"file\" \/><\/p>\n<p><strong>\u53ef\u4ee5\u770b\u5230\uff0c\u6e90\u6570\u636e\u4e2d\u4e3a<code>NULL<\/code>\u7684\u6570\u636e\uff0c\u5728hdfs\u4e2d\u53d8\u6210\u4e86<code>\\N<\/code>\u3002<\/strong><\/p>\n<h3>\u89e3\u51b3\u65b9\u6848<\/h3>\n<hr \/>\n<h4>\u65b9\u68481.\uff08\u63a8\u8350\uff09 NULL DEFINED AS &#8221;\u65b9\u5f0f<\/h4>\n<p>&#8211;\u8be5\u65b9\u6848\u4ec5\u9002\u7528\u4e8e\u5355\u5b57\u6bb5\u5206\u9694\u7b26<\/p>\n<pre><code class=\"language-sql\">INSERT OVERWRITE DIRECTORY '\/tmp\/staging_text'\nROW FORMAT DELIMITED FIELDS TERMINATED BY '|'\nNULL DEFINED AS ''\nSELECT id,name,age FROM org_torc;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/07\/image-1669360529863.png\" alt=\"file\" \/><\/p>\n<h4>\u65b9\u68482. INSERT OVERWRITE DIRECTORY + WITH SERDEPROPERTIES\u65b9\u5f0f<\/h4>\n<p>&#8211;\u8be5\u65b9\u6848\u9002\u7528\u4e8e\u591a\u5b57\u6bb5\u5206\u9694\u7b26<\/p>\n<pre><code class=\"language-sql\">--\u4e0d\u5728SERDEPROPERTIES\u4e2d\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u800c\u662f\u901a\u8fc7\u591a\u5b57\u6bb5\u62fc\u63a5\u7684\u65b9\u5f0f\uff08\u4ea6\u6216\u8005concat\uff09\ninsert overwrite directory '\/tmp\/targetDir'\nselect id||'|'||name||'|'||age FROM org_torc;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/07\/image-1627019046306.png\" alt=\"file\" \/><\/p>\n<h4>\u65b9\u68483. INSERT OVERWRITE DIRECTORY + SED\u66ff\u6362\u65b9\u5f0f<\/h4>\n<p>\u83b7\u53d6<code>insert overwrite directory<\/code>\u7684\u7ed3\u679c\u6570\u636e\uff0c\u901a\u8fc7<code>sed<\/code>\u547d\u4ee4\u628a<code>\\N<\/code>\u66ff\u6362\u6210\u7a7a\u7684\u65b9\u5f0f\u3002<\/p>\n<pre><code class=\"language-perl\">sed 's\/\\\\N\/\/g' orgFile > targetFile<\/code><\/pre>\n<p><strong>i.e.<\/strong><\/p>\n<p>\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>nvl<\/code>\u51fd\u6570\u5c06<code>null<\/code>\u66ff\u6362\u6210\u5176\u4ed6\u5b57\u7b26\uff0c\u518d<code>sed<\/code>\u66ff\u6362\u6210\u7a7a\u7684\u65b9\u5f0f\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"language-sql\">INSERT OVERWRITE DIRECTORY '\/tmp\/staging_text'\nROW FORMAT DELIMITED FIELDS TERMINATED BY '|'\nSELECT\n    nvl(id,'ThisFiledIsNull'),\n    nvl(name,'ThisFiledIsNull'),\n    nvl(age,'ThisFiledIsNull')\nFROM org_torc;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/07\/image-1627009406442.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-perl\">sed 's\/ThisFiledIsNull\/\/g' orgFile > targetFile<\/code><\/pre>\n<h4>\u65b9\u68484. CTAS TEXTFILE\u5185\u8868\u8fc7\u6e21\u65b9\u5f0f<\/h4>\n<pre><code class=\"language-sql\">DROP TABLE IF EXISTS staging_text;\nCREATE TABLE staging_text\nROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'\nWITH SERDEPROPERTIES\n('field.delim' = '|', 'serialization.null.format' = '')\nSTORED AS TEXTFILE\nLOCATION '\/tmp\/staging_text' \nAS SELECT * FROM org_torc;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/07\/image-1627009090454.png\" alt=\"file\" \/><\/p>\n<p>\u540e\u9762\u518d\u6bcf\u6b21\u505a\u540c\u6837\u7684\u5378\u6570\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u5148<code>DROP<\/code>\u6389\u8be5\u5185\u8868\uff0c\u518d\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n<p><strong>i.e.<\/strong><\/p>\n<p>\u540c\u6837\u7684\uff0c\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7<strong><code>textfile<\/code>\u5916\u8868<\/strong>\u7684\u65b9\u5f0f\uff0c\u591a\u4e86\u4e00\u4e2a<code>insert<\/code>\u7684\u6b65\u9aa4\uff1a<\/p>\n<pre><code class=\"language-sql\">--external\u5916\u8868\u4e0d\u652f\u6301CTAS\uff0c\u4e14\u5fc5\u987b\u58f0\u660e\u5217\u540d\nDROP TABLE IF EXISTS staging_external_text;\nCREATE EXTERNAL TABLE staging_external_text (id INT,name STRING,age INT )\nROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'\nWITH SERDEPROPERTIES\n('field.delim' = '|', 'serialization.null.format' = '')\nSTORED AS TEXTFILE\nLOCATION '\/tmp\/staging_external_text';<\/code><\/pre>\n<pre><code class=\"language-sql\">INSERT OVERWRITE TABLE staging_external_text SELECT * FROM org_torc;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/07\/image-1627011642730.png\" alt=\"file\" \/><\/p>\n<h4>\u62d3\u5c551\uff1a\u5982\u4f55\u5bfc\u51fa\u591a\u5b57\u6bb5\u5206\u9694\u7b26\u7684\u6570\u636e\u6587\u4ef6<\/h4>\n<p>\u53ef\u4ee5\u901a\u8fc7\u591a\u5b57\u6bb5\u5206\u9694\u7b26\u8868\u7684\u65b9\u5f0f\u5b9e\u73b0\uff08\u8fd9\u91cc\u53ef\u4ee5\u5728<code>SERDEPROPERTIES<\/code>\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7f16\u7801\u683c\u5f0f\u3001null\u7684format\u3001\u4efb\u610f\u591a\u5b57\u6bb5\u5206\u9694\u7b26\uff09\uff1a<\/p>\n<pre><code class=\"language-sql\">DROP TABLE IF EXISTS emp_external_text;\nCREATE EXTERNAL TABLE emp_external_text (\n<code>empno<\/code> int DEFAULT NULL,\n<code>ename<\/code> string DEFAULT NULL,\n<code>job<\/code> string DEFAULT NULL,\n<code>mgr<\/code> int DEFAULT NULL,\n<code>hiredate<\/code> date DEFAULT NULL,\n<code>sal<\/code> int DEFAULT NULL,\n<code>comm<\/code> int DEFAULT NULL,\n<code>deptno<\/code> int DEFAULT NULL\n)\nROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe'\nWITH SERDEPROPERTIES\n('input.delimited'='@_@',\n'serialization.encoding'='GBK',\n'serialization.null.format' = '')\nSTORED AS TEXTFILE\nLOCATION '\/tmp\/emp_external_text';<\/code><\/pre>\n<p>\u521b\u5efa\u597d\u4e2d\u95f4\u6587\u672c\u8868\u4e4b\u540e\uff0c\u6211\u4eec\u628a\u6e90\u8868\u7684\u6570\u636e\u5199\u5165\u8fd9\u4e2a\u4e2d\u95f4\u8868\u4e2d\u3002 \u5982\u679c\u4e0b\u6e38\u5bf9\u6570\u636e\u6587\u4ef6\u7684\u4e2a\u6570\u6709\u8981\u6c42\uff0c\u53ef\u4ee5\u8bbe\u7f6ereduce\u6570\u76ee + <code>distribute by<\/code> \u7684\u65b9\u5f0f\u5199\u51fa\u56fa\u5b9a\u4e2a\u6570\u7684\u6570\u636e\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-sql\">set mapred.reduce.tasks=10;\nINSERT OVERWRITE TABLE emp_external_text SELECT * FROM emp_holo distribute by empno;<\/code><\/pre>\n<p>\u6267\u884c\u5b8c\u5bfc\u51fa\u8bed\u53e5\u4e4b\u540e\uff0c\u5728\u4e2d\u95f4\u6587\u672c\u8868\u7684hdfs\u76ee\u5f55\u4e0b\u5c31\u4f1a\u751f\u621010\u4e2a\u6570\u636e\u6587\u4ef6\uff0c\u901a\u8fc7hdfs get\u5230\u672c\u5730\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-sql\">hadoop fs -get \/tmp\/emp_external_text\/* .\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 inceptor\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u9700\u8981\u5c06inceptor\u8868\u5378\u6570\u5230hdfs\uff0c\u4f9b\u4e0b\u6e38\u5e94\u7528\u8bfb\u6570\u7684\u60c5\u51b5\u3002\u800cnull ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=6361\" 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":[40],"tags":[],"class_list":["post-6361","post","type-post","status-publish","format-standard","hentry","category-sql_issue"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/6361","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=6361"}],"version-history":[{"count":4,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/6361\/revisions"}],"predecessor-version":[{"id":16190,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/6361\/revisions\/16190"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}