首页 > 解决方案 > Hive - 加载到外部表的默认位置

问题描述

我在配置单元中创建了一个具有默认位置的外部表

 CREATE EXTERNAL TABLE `testtable`(
      `id` int, 
      `name` string)
    ROW FORMAT DELIMITED 
     FIELDS TERMINATED BY ',' 
    STORED AS INPUTFORMAT 
     'org.apache.hadoop.mapred.TextInputFormat' 
   OUTPUTFORMAT 
    'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
    LOCATION
    '<hdfs-uri>/hive/warehouse/testtable'

想确认我是否可以将包含 ID/名称值的文本文件从本地移动到外部表 testtable 的 HDFS 位置 /hive/warehouse/testtable/test.txt?谢谢。

标签: hivehiveql

解决方案


是的。test.txt您可以从外部表( )local的位置上传。即使是默认的仓库目录,这也将起作用。HDFStesttable<hdfs-uri>/hive/warehouse/testtable<hdfs-uri>/hive/warehouse/Hive

请记住 - 对于非外部(称为Hive 管理的)表,删除表将HDFS自动删除其仓库目录。对于外部表,删除表不会删除HDFS备份它的目录,需要作为单独的操作删除。

插图:

Here the Hive warehouse directory is hdfs:///apps/hive/warehouse

创建表

hive> CREATE EXTERNAL TABLE `testtable`(
      `id` int, 
      `name` string)
       ROW FORMAT DELIMITED 
       FIELDS TERMINATED BY ',' 
      STORED AS INPUTFORMAT 
        'org.apache.hadoop.mapred.TextInputFormat' 
      OUTPUTFORMAT 
        'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
      LOCATION 'hdfs:///apps/hive/warehouse/testtable';

test.txt 中的数据

1,name-1
2,name-2
3,name-3

上传数据到 HDFS

hadoop fs -put test.txt hdfs:///apps/hive/warehouse/testtable

查询表

hive> select * from testtable;
1   name-1
2   name-2
3   name-3

推荐阅读