简介
Hive是建立在Hadoop上的数据仓库基础架构。
Hive是为超大数据集设计的计算/扩展能力,支持SQL like的查询语言,具有统一的元数据管理系统。
数据类型
数字类型:tinyint , smallint , int , bigint , float ,decimal ,double
字符类型:string , char , varchar
日期类型:date , timestap
其他类型:boolean , binary
复杂类型:Maps<DATATYPE,DATATYPE> ,Arrays<DATATYPE>
DDL
新建表
Hive的元数据并不存放在HDFS中,而是存放在传统的RDBMS中,典型的如MySQL等。
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
create table if not exists table_name (
id int,
dtDontQuery string,
name string
)
comment 'this is the first table'
partitioned by (date string)
stored as textfile
row format delimited fields terminated by '\t' lines terminated by '\n';
如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。
如果数据需要压缩,使用 STORED AS SEQUENCEFILE。
textfile和sequencefile都属于行存储,RCFile属于列存储。
创建与已知表相同结构的表 Like:只复制表的结构,而不复制表的内容。
create table test_like_table
like test_bucket。
删除
删除表
drop table tbname
删除分区
alter table tbname drop partition
DML
插入
load data local infile filepath overwrite into table tbname
其中local表示加载的文件的来源是本地文件;overwrite表示覆盖原表的数据,不写则不会覆盖。