hive表基本操作
一、建表:
1、内部表:
即是普通表,保存在/user/hive/warehouse/
create table t_student(id int,name string,sex string,age int,deptno string)
row format delimited
fields terminated by ','
lines terminated by '\n';
fields terminated by 用于字段分隔符
lines terminated by 用于行分隔符
加载数据到内部表:
load data inpath '/tmp.txt' into table t_student
inpath:表示从hdfs文件系统中取数据
不加inpath表示从本地hdfs文件系统中取数据
2、外部表:
create external table t_external(id int,name string,age int)
row format delimited
fields terminated by ','
location "/hivedata";
加载数据:加载数据之后,原本目录数据删除,移动到/user/hive/warehouse/test.db/emp
load data local inpath '/home/hadoop1/student.txt' into table t_external;
指定了location:数据保存在/hivedata,在/user/hive/warehouse/db1.db中看不到数据;
若不指定location,默认保存在/user/hive/warehouse/下
内部表和外部表的区别:
删除外部表后,再次创建相同表:
不需要进行加载数据,直接执行查询,发现可以直接插上上次外部表的数据。所以上面执行的删除表只是删除表的元数据而已。
结论: 外部表的数据不由hive自身负责管理,就算不指定location,虽然数据会被加载到/user/hive/warehouse/,但是不由hive管理。删除数据库中的表并不能删除外部表,无论是保存在哪里都不会删除,保存在hdfs中,需要通过hdfs进行删除
3、分区表:
创建分区内部表
create table t_student(id int,name string,idcar int) partitioned by (country string) row format delimited fields terminated by