hive 创建表

来源:互联网 发布:主播助手软件 编辑:程序博客网 时间:2024/05/29 14:40

HQL对sql语句的扩展,可以定义表的数据文件存储在什么位置,以及定义表文件存储格式。

create table if not exists hives.employees(name string comment 'employee name',salary float comment 'employee salary',subordinates array<string> comment 'names of subordinates',deducations map<string,float> comment 'Keys-value',address struct <street:string,city:string,state:string,zip:int> comment 'home address')comment 'Description of the table'tblproperties('creator'='me','create_at'='20170424')location '/user/hive/warehouse/hives.db/employees'

hive自动添加两个属性:last_modified_by 保存着最后一个修改这个表的用户的用户名,last_modified_time 最后一次修改这个表的时间。

用户可以只拷贝一个表的结构,使用like关键字:

hive (default)> create table copy_table like hives.employees;OKTime taken: 0.326 seconds

查看表结构:


这种方式可以指定location 属性,其他的属性模式是不可重新定义的。

hive (default)> create table copy_table_struct like employees location '/user/hive/warehouse/copy/';OKTime taken: 0.102 seconds
hdfs目录结构:


使用 describe extended table_name 可以查看表的详细描述信息


也可以使用formatted关键字代替extended,实际中formatted使用的比较多:

hive (default)> describe formatted copy_table_struct;

我们创建的这些表称为hive的内部表,如果删除表,hive也会把对应的数据删除掉,这样管理表不方便和其他工作共享数据。为了弥补这一缺点,可以创建外部表。

外部表

create external table if not exists bus(              CARDID string,LINEID string,BEGINTIME string,ENDTIME string,STATIONFROM string,STATIONTO string,BUSID string)row format delimited fields terminated by ','location '/user/hive/external/data/bus'
put数据:

[hadoop@localhost ~]$hadoop fs -put 201404_bus.csv /user/hive/external/data/bus/
查询外部表数据:

hive (default)> select * from bus limit 10;


0 0
原创粉丝点击