创建数据库
hive> CREATE DATABASE test; hive> USE test;
创建表
hive> CREATE EXTERNAL TABLE t1( > id INT, > name STRING, > age INT, > country STRING COMMENT 'country of origination', > province STRING) > COMMENT 'This is the staging t1 table' > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' > STORED AS TEXTFILE > LOCATION '/tmp/t1'; # 这里的/tmp/t1是hadoop上的路径 OK Time taken: 0.138 seconds hive> desc t1; OK id int name string age int country string country of origination province string Time taken: 0.103 seconds, Fetched: 5 row(s) hive>
上传本地数据文件到对应的hadoop路径中
hive> !hadoop fs -put /tmp/t1.txt /tmp/t1; Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0 hive>
此时hive的t1表里已经可以查询到刚上传上去的数据
hive> select * from t1; OK 1001 name1 18 China Shenzhen 1002 name2 19 China Hubei 1003 name3 18 China Shenzhen 1004 name4 18 China Hubei Time taken: 0.387 seconds, Fetched: 4 row(s) hive>
此时我们删除t1表
hive> drop table t1; OK Time taken: 0.126 seconds hive>
t1表删除了,但是hadoop对应文件夹下的数据文件仍然存在
hive> !hadoop fs -ls /tmp/t1; Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0 Found 1 items -rw-r--r-- 3 root supergroup 110 2018-05-16 09:23 /tmp/t1/t1.txt hive>Hive的数据库和数据表本质上是hadoop上的路径,外部表建表时指定了表对应的hadoop上的路径,表创建后不用从hadoop数据文件导入数据,直接在hive表里可以查到数据,可以理解为表和数据文件路径之间是一个软链接;hive外部表删除时,仅仅只是删除了链接,不会删除hadoop数据文件。