1、進入hive數據庫:hive
2、查看hive中的所有數據庫:show databases;
3、用default數據庫:use default;
4、查看所有的表:show tables;
5、查詢表結構:desc mytest(表名);
6、查詢表數據: select * from mytest(表名);
7、創建數據庫:hive> CREATE SCHEMA userdb;
8、驗證數據庫表:hive> SHOW DATABASES;
9、刪除數據庫:hive> DROP DATABASE IF EXISTS userdb;
hive> DROP SCHEMA userdb;
全部刪除相應的表在刪除數據庫之前:hive> DROP DATABASE IF EXISTS userdb CASCADE;
10、創建表employee
hive> create table if not exists employee (eid int,name String,salary String,destination String)
> comment 'employee details'
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '\t'
> LINES TERMINATED BY '\n'
> STORED AS TEXTFILE;
如果增加分區必須在創建表的時候就創建分區,不然就會報錯,創建分區的命令>partition by ‘根據哪個字段分區’,
hive> create table employee (id int, name String, dept String)
> PARTITIONED BY (year int)
> ROW FORMAT delimited
> fields terminated by '\t'
> lines terminated by '\n'
> stored as textfile;
stored as textfile文件格式,文件格式在hive中有三種: textfile、Sequencefile(序列化文件,學hadoop的都會知道啦)、Rcfile。
11、添加數據到表中
hive> LOAD DATA LOCAL INPATH '/usr/hadoop/hive/sample.txt'
> OVERWRITE INTO TABLE employee;
如果table是個分區表則必須在hql中指定分區
hive> LOAD DATA LOCAL INPATH '/usr/hadoop/hive/sample.txt'
> OVERWRITE INTO TABLE employee partition(year=2012);(雖然已經實現了分區,但還未明白為什么分區后所有的數據都根據分區條件發生變化)
LOAD DATA:加載數據; LOCAL:本地數據 INPATH:文件的地址 OVERWRITE:覆蓋表中的數據 加overwrite是重寫表的數據,不加是追加數據
插入表數據:insert into employee(eid,name) values (1208,'jack');hive只支持插入不支持修改和刪除
12、重命名表名: hive> ALTER TABLE employee RENAME TO emp;
13、修改emp表中字段name為ename: hive> ALTER TABLE emp CHANGE name ename String;
14、修改emp表中字段salary的數據類型從float改為double:hive> ALTER TABLE emp CHANGE salary salary Double;
15、刪除表;hive> drop table test;
16、創建視圖
hive> create view empview as
> select * from emp where salary>40000;
17、不同類型的連接 JOIN 、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN
18、創建外部表:用external關鍵字
hive> create external table outsidetable(name string comment 'name value',addr string comment 'addr value');
查詢表信息:desc formatted outsidetable;