Hive ----DDL

1、創建數據庫
create database test_db comment 'test database';
create database if not exists test_db comment 'test database';
create database test_db location 'hdfs path';
create database test_db with dbproperties('creator'='bpf','date'='20171106');
2、查看數據庫
show databases;
show databases like 'test*';
3、查看具體數據庫結構
describe database test_db;
describe database extended test_db;
4、使用(切換)數據庫
use test_db
5、刪除數據庫
drop database if exists test_db;
drop database if exists test_db cascade; 
6、創建表
create table [if not exists] [test_db.]test_table(
  a string [comment 'string field'],
  b float [comment 'float field'],
  c array<string> [comment 'array field'],
  d map<string,int> [comment 'map field'],
  e struct<field1:string,field:int> [comment 'struct field']
)
[comment 'description for this table']
[tblproperties ('k1' = 'v1', 'k2' ='v2')]
[row format delimited
fields terminated by '\001'
collection items terminated by '\002'
map keys terminated by '\003']
[lines terminated by '\t']
[store as textfile]
[location 'hdfs path']
[as select_statement];   -- (這句不支持外部表) 
create table [if not exists] [test_db.]test_table like [test_db.]other_table [location 'hdfs path'] 
7、數據類型
data_type
  : primitive_type
  | array_type
  | map_type
  | struct_type
  | union_type  -- (Note: Available in Hive 0.7.0 and later)
primitive_type
  : TINYINT
  | SMALLINT
  | INT
  | BIGINT
  | BOOLEAN
  | FLOAT
  | DOUBLE
  | DOUBLE PRECISION -- (Note: Available in Hive 2.2.0 and later)
  | STRING
  | BINARY      -- (Note: Available in Hive 0.8.0 and later)
  | TIMESTAMP   -- (Note: Available in Hive 0.8.0 and later)
  | DECIMAL     -- (Note: Available in Hive 0.11.0 and later)
  | DECIMAL(precision, scale)  -- (Note: Available in Hive 0.13.0 and later)
  | DATE        -- (Note: Available in Hive 0.12.0 and later)
  | VARCHAR     -- (Note: Available in Hive 0.12.0 and later)
  | CHAR        -- (Note: Available in Hive 0.13.0 and later)

array_type
  : ARRAY < data_type >

map_type
  : MAP < primitive_type, data_type >

struct_type
  : STRUCT < col_name : data_type [COMMENT col_comment], ...>

union_type
   : UNIONTYPE < data_type, data_type, ... >  -- (Note: Available in Hive 0.7.0 and later)
8、文件格式
file_format:
  : SEQUENCEFILE
  | TEXTFILE    -- (Default, depending on hive.default.fileformat configuration)
  | RCFILE      -- (Note: Available in Hive 0.6.0 and later)
  | ORC         -- (Note: Available in Hive 0.11.0 and later)
  | PARQUET     -- (Note: Available in Hive 0.13.0 and later)
  | AVRO        -- (Note: Available in Hive 0.14.0 and later)
  | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
9、查看可用表
show tables;
show tables in test_db;
show tables like 'test*';
10、查看錶tblproperties
show tblproperties test_db;
11、查看錶詳細信息
desc test_table;
desc extended test_table;
desc formatted test_table;
12、查看錶中某一列的信息
desc test_table.column;
13、創建外部表
create external table if not exists test_table(
  field string
)
[as select_statement]不可用
14、創建分區表
create table test)table(
  field string
)
partitioned by (k1 string, k2 int);
//分區後會在hdfs中的表對應的目錄下生成子目錄 k1/k2,便於管理分類,針對性查詢時也可以提高性能
15、查看錶中的分區
show partitions test_db;
show partitions test_table partition(k1='value');
16、加載數據到分區表
load data [local] inpath 'path/file' into table test_table partition(k1='v1',k2='v2');
//有Local代表本地路徑,沒有代表HDFS路徑
17、增加分區
alter table test_table add partition(...) location 'hdfs path'
18、修改分區對應的目錄地址
alter table test_table partition(k1=v1, k2=v2) set location 'hdfs path';
19、刪除分區
alter table test_table drop if exists partition(...);
20、修改表
ALTER TABLE name RENAME TO new_name
ALTER TABLE name ADD COLUMNS (字段1及其屬性,字段2及其屬性)
ALTER TABLE name DROP [COLUMN] column_name
ALTER TABLE name CHANGE column_name new_name new_type
21、刪除表
DROP TABLE [IF EXISTS] table_name;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章