1. 程式人生 > >Hive Shell命令之一(資料庫和表的操作)

Hive Shell命令之一(資料庫和表的操作)

//資料庫的有關操作
1、如果資料庫不存在的話建立資料庫,預設資料庫default:
create database if not exists test;

2、檢視hive中所包含的資料庫:
show databases;

3、如果資料庫非常多,可以用正則表示式匹配篩選出需要的資料庫名。
show databases like 't.*';

4、建立資料庫並指定資料庫存放位置(預設存放在hive.metastore.warehouse.dir所指定的目錄):
create database test01 location '/data1';

5、建立資料庫時增加描述資訊:
create database test02 comment 'this is a database named test02';

6、檢視資料庫的描述資訊:
describe database test02;
結果:test02    this is a database named test02    hdfs://master.infobird.com:8020/user/hive/warehouse/test02.db    hdfs    USER    

7、為資料庫增加一些和其相關的鍵值對屬性資訊:
create database test05 with dbproperties('name' = 'abc', 'data'='2015-12-22');
注意:鍵值對根據需要自定義

8、顯示為資料庫定義的鍵值對的資訊:
describe database extended test05;

9、使用資料庫:
use test01;

10、設定屬性值使提示符裡顯示當前資料庫:
set hive.cli.print.current.db=true;

11、如果資料庫存在,刪除資料庫:
drop database if exists test;

12、預設情況下,hive是不允許使用者刪除一個包含有標的資料庫的。
    使用者要麼先刪除表再刪除資料庫,要麼在命令中加入關鍵字cascade(預設是restrict):
drop database if exists test01 cascade;

如果某個資料庫刪除了,其對應的目錄也同時會被刪除。

13、只有為資料庫設定的鍵值對資訊可以改,其他資訊如資料庫名,資料庫所在目錄都不可更改:
alter database test05 set dbproperties('name'='abcd','size'='10');

//表的有關操作
1、建立表
create table if not exists test05.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'
      tblproperties ('creator'='me','created_date'='2015-12-22')
      location '/user/hive/warehouse/test05.db/employees';
如果使用者當前所處的資料庫並非目標資料庫,那麼我們可以在表名錢增加一個數據庫名來進行指定,如test05就是我們之前建的資料庫。

2、拷貝已經存在的標的表模式,無需拷貝資料:
create table if not exists test05.employees2 like test05.employees;

3、列舉當前庫下的所有表:
show tables;

列舉指定庫下的所有表:
show tables in default;

根據正則過濾需要的表名:
show tables like 'empl.*';

4、查看錶結構資訊:
describe test05.employees;

查看錶的結構的詳細資訊:
describe extended test05.employees;

檢視某一列的資訊:
describe test05.employees salary;

5、建立一個外部表
create external table if not exists stocks(name STRING, age INT, address STRING)
      ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
      LOCATION '/data1/stocks';
這個表可以讀取所有位於/data1/stocks目錄下以逗號分隔的資料。
因為表是外部的,所以hive並非認為其完全擁有這份資料,因此刪除該表並不會刪除掉這份資料。

6、通過複製產生一張表
create external table if not exists test05.emplyees3 like test05.employees;

這裡語句中如果省略掉external關鍵字而且源表是外部表的話,那麼生成的新表也是外部表。
如果語句中省略掉external關鍵字而且源表是管理表的話,那麼生成的新表也將是管理表。
但是,如果語句中包含有external關鍵字而且源表是管理表的話,那麼生成的新表將是外部表。

7、建立分割槽表:
 create table employees(name string, age int, phone string) partitioned by (address string, city string);

如果表中資料及分割槽個數都非常大,執行一個所有分割槽的查詢可能會觸發一個巨大的MapReduce任務,因此建議設定
set hive.mapred.mode=strict;

8、檢視所有分割槽:

show partitions employees;


9、檢視指定分割槽:
show partitions employees partition(addresds='Beijing');

10、建立外部表分割槽:
create external table if not exists log_messages (hms int) partitioned by (year int, month int, day int) row format delimited fields terminated by '\t';

修改表

alter table log_messages add partition(year = 2015, month = 12, day = 23) location 'hdfs://data1/log_messages/2015/12/23';


11、自定義表的儲存格式:
Hive的預設儲存格式是文字檔案格式,這個格式也可以通過可選的子句STORED AS TEXTFILE顯示指定。
使用者可以在建立表的時候指定各種各樣的分隔符
create table employees(name string, salary float,
     subordinates array<string>,
     deductions map<string, float>,
     address struct<street:string, city:string, state:string, zip:int>)
     row format delimited
     fields terminated by '\001'
     collection items terminated by '\002'
     map keys terminated by '\003'
     lines terminated by '\n'
     stored as textfile;


12、刪除表:
drop table if exists employees;


13、表重新命名
alter table log_messages rename to logmessages;

14、增加、修改和刪除分割槽
alter table logmessages add if not exists partition(year = 2015, month = 12, day =23) location '/logs/2015/12/23' partition (year = 2015, month = 12, day = 22) location '/logs/2015/12/22';


通過高效地移動位置來修改某個分割槽的路徑(不會將資料從舊的路徑轉走,也不會刪除舊的資料):
alter table logmessages partition(year = 2015, month = 12, day = 22) set location 's3n://ourbucket/logs/2015/12/22';

刪除某個分割槽:
alter table logmessages drop if exists partition (year = 2015, month = 12, day = 22);


15、增加列:
alter table logmessages add columns (app_name string comment 'application name');

16、修改列,對某個欄位進行重新命名並修改其位置、型別或者註釋:
alter table logmessages change column hms hours_minutes int comment 'the hours and minutes' after app_name;

把列名hms修改為:hours_minutes, 並添加註釋,位置放在列app_name 後面,若是放在第一個位置則用關鍵字 first代替after app_name.

17、刪除或者替換列:
alter table logmessages replace columns (hms int);
這是把表中所有的列刪除掉,並新加入列hms,因為是alter語句,所以只有表的元資料資訊改變了,原來的分割槽還在。

18、修改表的屬性
 alter table logmessages set tblproperties('notes' = 'the process');

19、修改表的儲存屬性
alter table logmessages partition(year = 2015, minth = 12, day = 22) set fileformat sequencefile;