hive数据库操作

2016-07-19  本文已影响260人  zhangxu0636

创建表

如果不加if not exist,如果已经存在表,则会报错,但是,如果加了if not exist,则如果存在了表,就会忽略后面执行的语句。

#创建完表后,hive会自动添加两个字段,一个是last_modified_by->最后修改这个表的用户的用户名;另一个是last_modified_time->最后一次修改的事件。
CREATE TABLE IF NOT EXISTS mydb.employees(
name STRING COMMENT 'Employee name',
salary FLOAT COMMENT 'Employee salary',
subordinates ARRAY<STRING> COMMENT 'Names of subordinates',
deductions MAP<STRING,FLOAT> COMMENT 'Keys are deductions names,values are percentages',
address STRUCT<street:STRING,city:STRING,state:STRING,zip:INT> COMMENT 'Home address') COMMENT 'Description of the table'
LOCATION 'employees';

CREATE TABLE IF NOT EXISTS mydb.employees2 LIKE mydb.employees;

SHOW TABLES

列举出所有的表

USE mydb;
SHOW TABLES;
#如果表比较多,则可以用正则表达式进行过滤。
SHOW TABLES 'empl.*';

DESCRIBE

列举表的详细信息

外部表

在创建表的时候加上EXTERNAL关键字,只是在删除表的时候,不会删除数据,但是描述表的元信息会被删除。

分区

当我们进行查询的时候,hive会遍历所有的表,而分区则会减少hive的查询操作,在你指定的分区中查询数据,会提高一些效率。

CREATE TABLE employees(
name STRING,
salary FLOAT,
subbordinates ARRAY<STRING>,
deductions MAP<STRING,FLOAT>,
address STRUCT<street:STRING,city:STRING,state:STRING,zip:INT>
)
PARTITIONED BY (country STRING,state STRING);

当插入数据的时候,我们会看成以下结构。
.../employees/country=CA/state=AB
.../employees/country=CA/state=BC
.../employees/country=US/state=AL
.../employees/country=US/state=AK

删除表

DROP TABLE IF EXISTS employees;

修改表

ALTER TABLE log_messages RENAME TO logmsgs;

增加、修改和删除表分区

ALTER TABLE log_messages ADD IF NOT EXISTS
PARTITION (year = 2011, month = 1,day=1) LOCATION '/logs/2011/01/01'
PARTITION(year = 2011, month = 1, day = 2) LOCATION '/logs/2011/01/02'
PARTITION(year = 2011, month = 1, day = 3) LOCATION '/logs/2011/01/03'
...;
ALTER TABLE log_messages PARTITION(year = 2011, month = 12, day = 2)
SET LOCATION 's3n://ourbucket/logs/2011/01/02';
ALTER TABLE log_messages DROP IF EXISTS PARTITION(year = 2011,month = 12, day = 2);

修改列信息

 ALTER TABLE log_messages
CHANGE COLUMN hms hours_minutes_seconds INT
COMMENT 'The hours,minutes, and seconds part of the timestamp'
AFTER severity; 

增加列

ALTER TABLE log_messages ADD COLUMNS(
app_name STRING COMMENT 'Application name',
session_id LONG COMMENT 'The current session id');

删除或者替换列

ALTER TABLE log_messages REPLACE COLUMNS(
hours_mins_secs INT COMMENT 'hour, minute,seconds from timestamp',
severity STRING COMMENT 'The message severity'
message STRING COMMENT 'The rest of the message');

修改表属性

ALTER TABLE log_messages SET TBLPROPERTIES(
'notes' = 'The process id is no longer captured; this column is always NULL');

修改存储属性

ALTER TABLE log_messages
PARTITION(year = 2012, month = 1, day = 1)
SET FILEFORMAT SEQUENCEFILE;
上一篇下一篇

猜你喜欢

热点阅读