1. 程式人生 > >Hadoop---Hive的基本操作

Hadoop---Hive的基本操作

建立資料庫(使用SCHEMA方式)
CREATE SCHEMA userdb;

建立資料庫
CREATE DATABASE userdb;

建立資料庫(存在則不建立,不存在則建立)
CREATE DATABASE IF NOT EXISTS userdb;

列出資料庫列表
SHOW DATABASES;

刪除資料庫
DROP DATABASE IF EXISTS userdb;

刪除資料庫(全部刪除相應的表在刪除資料庫之前)
DROP DATABASE IF EXISTS userdb CASCADE;

刪除資料庫(使用SCHEMA方式)
DROP SCHEMA userdb;

建立表
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;

修改表名
ALTER TABLE employee RENAME TO emp;

修改列名和列資料型別
ALTER TABLE emp CHANGE name ename String;

增加列(列名為dept,型別為String)
ALTER TABLE emp ADD COLUMNS(
dept STRING COMMENT 'Department name');

刪除列
ALTER TABLE employee DROP COLUMN destination;

使用empid代替eid列,name代替ename列
ALTER TABLE emp REPLACE COLUMNS(
eid INT empid Int,
ename STRING name String);

刪除表
DROP TABLE IF EXISTS emp;

檢視所有表
SHOW TABELS;

smple.txt
1201    Gopal    45000    Technical    manager
1202    Manisha    45000    Proof    reader
1203    Masthanvali    40000    Technical    writer
1204    Kiran    40000    Hr    Admin
1205    Kranthi    30000    Op    Admin

插入資料(將文字插入表中)
LOAD DATA LOCAL INPATH '/home/hadoop/smple.txt'
OVERWRITE INTO TABLE employee;

插入資料
insert into employee values(102,'ss','ddd','dd');

刪除資料


查詢一條資料
select * from employee where eid=1204;

查詢所有資料
select * from employee;

建立檢視(為工資超過30000的建立一個檢視)
CREATE VIEW emp_30000 AS
SELECT * FROM employee 
WHERE salary>30000;

檢視檢視
select * from emp_30000;

刪除檢視
DROP VIEW emp_30000;

建立索引(對salary列建立一個索引)
CREATE INDEX index_salary ON TABLE employee(salary) 
AS 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild
IN TABLE index_salary_employee;

刪除索引(刪除index_salary索引)
DROP INDEX index_salary ON employee;

升序排列
SELECT eid, name FROM employee ORDER BY name ASC;

降序排列
SELECT eid, name FROM employee ORDER BY name DESC;

分組
SELECT name, count(*) FROM employee GROUP BY name;

連線查詢

JOIN(內連線)
SELECT c.id, c.name, c.age, o.amount
FROM CUSTOMERS c JOIN ORDERS o
ON (c.id = o.CUSTOMERS); 

LEFT OUTER JOIN(左外連線)
SELECT c.id, c.name, c.age, o.amount
FROM CUSTOMERS c 
LEFT OUTER JOIN ORDERS o
ON (c.id = o.CUSTOMERS); 

RIGHT OUTER JOIN(右外連線)
SELECT c.id, c.name, c.age, o.amount
FROM CUSTOMERS c 
RIGHT OUTER JOIN ORDERS o
ON (c.id = o.CUSTOMERS); 

FULL OUTER JOIN(全連線)
SELECT c.id, c.name, c.age, o.amount
FROM CUSTOMERS c 
FULL OUTER JOIN ORDERS o
ON (c.id = o.CUSTOMERS);