createtable [ifnotexists] [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];-- (这句不支持外部表)
createtable [ifnotexists] [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.0and later)
| ORC -- (Note: Available in Hive 0.11.0and later)
| PARQUET -- (Note: Available in Hive 0.13.0and later)
| AVRO -- (Note: Available in Hive 0.14.0and later)
| INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
9、查看可用表
show tables;show tables in test_db;show tables like'test*';
createexternaltableifnotexists test_table(
field string
)
[as select_statement]不可用
14、创建分区表
createtable 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'intotable test_table partition(k1='v1',k2='v2');
//有Local代表本地路径,没有代表HDFS路径
17、增加分区
alter table test_table add partition(...) location 'hdfs path'
18、修改分区对应的目录地址
altertable test_table partition(k1=v1, k2=v2) set location 'hdfs path';
19、删除分区
alter table test_table drop if exists partition(...);
20、修改表
ALTERTABLE name RENAME TO new_name
ALTERTABLE name ADD COLUMNS (字段1及其属性,字段2及其属性)
ALTERTABLE name DROP [COLUMN] column_name
ALTERTABLE name CHANGE column_name new_name new_type