背景

fackbook为解决海量数据分析,避免使用传统mr而开发出来类sql的操作大数据的工具。

定义

Apache Hive™数据仓库软件有助于使用SQL读取,编写和管理驻留在分布式存储中的大型数据集。可以将结构投影到已存储的数据中。提供了命令行工具和JDBC驱动程序以将用户连接到Hive。

特点

1、Tools to enable easy access to data via SQL, thus enabling data warehousing tasks such as extract/transform/load (ETL), reporting, and data analysis.
2、A mechanism to impose structure on a variety of data formats
3、Access to files stored either directly in Apache HDFS™ or in other data storage systems such as Apache HBase™
4、Query execution via Apache Tez™, Apache Spark™, or MapReduce
5、Procedural language with HPL-SQL
6、Sub-second query retrieval via Hive LLAP, Apache YARN and Apache Slider.
//google翻译
1,通过SQL轻松访问数据的工具,从而实现数据仓库任务,如提取/转换/加载(ETL),报告和数据分析。
2,一种在各种数据格式上施加结构的机制
3,访问直接存储在Apache HDFS™或其他数据存储系统(如Apache HBase™)中的文件
4,通过Apache Tez™,Apache Spark™或MapReduce执行查询
5,HPL-SQL的过程语言
6,通过Hive LLAP,Apache YARN和Apache Slider进行亚秒级查询检索。

架构简述


用户连接客户端:cli、jdbc/odbc 、web gui
thriftserver:第三方服务
metastore:存储hive的元数据(库名、表名、字段名、字段类型、分区、分桶、创建时间、创建人等)。
hive的元数据存储:
derby:hive内置数据库,存储量小,只支持单session;
mysql(支持jdbc的关系型数据库):量大,支持多session
解析器:将hql抽象成表达式树
编译器:对hql语句进行词法、语法、语义的编译(需要跟元数据关联),编译完成后会生成一个有向无环的执行计划。hive上就 是编译成mapreduce的job。
优化器:将执行计划进行优化,减少不必要的列、使用分区、使用索引等。优化job。
执行器:将优化后的执行计划提交给hadoop的yarn上执行。提交job。

安装

centos7+mysql+hive安装配置

基础

命名

hive不能使用关键字、数字开始的字符串来作库表名,不区分大小写。

创建库:

create database if not exists mydb;

创建库的本质:在hive的warehouse目录下创建一个目录(库名.db命名的目录)
查看结果

切换库

use mydb;

创建表

本质是创建目录,并映射到元数据。

create table mydb.t_user(id int,name string);

create table t_user(id int,name string);

hive的默认的列与列之间分隔符是:

默认:^A 、\u0001 、 \001

执行:

hive> use default;
OK
Time taken: 0.044 seconds
hive> create table t_user(id int, name string);
OK
Time taken: 0.24 seconds
hive> create table mydb.t_user(id int, name string);
OK
Time taken: 0.258 seconds

查看


创建表语法:

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] TABLENAME(

[COLUMNNAME COLUMNTYPE [COMMENT ‘COLUMN COMMENT’],…] )

[COMMENT ‘TABLE COMMENT’]

[PARTITIONED BY (COLUMNNAME COLUMNTYPE [COMMENT ‘COLUMN COMMENT’],…)]

[CLUSTERED BY (COLUMNNAME COLUMNTYPE [COMMENT ‘COLUMN COMMENT’],…) [SORTED BY (COLUMNNAME [ASC|DESC])…] INTO NUM_BUCKETS BUCKETS]

[ROW FORMAT ROW_FORMAT]

[STORED AS FILEFORMAT]

[LOCATION HDFS_PATH]

内外部表区别:
1、默认创建内部表,创建外部表需要external。
2、一般使用外部表(长期存在的表、数据量大的、不希望把数据块删除的数据),
   临时表或者确定使用即可清空全部数据(数据库和元数据)则可以使用内部表。
3、内部表删除时将会删除元数据和hdfs中表对应的目录,而外部表删除时只会删除元数据,hdfs中的数据目录保留。


create external table t_user2(id int,name string);

执行

hive> create table if not exists my_user(
    > id int comment "this is user id", 
    > name string
    > )
    > row format delimited fields terminated by ' '
    > -- 用空格作为分隔符
    > lines terminated by '\n'
    > -- 换行符分隔每行
    > stored as textfile 
    > -- 存储格式
    > location '/user/hive/warehouse/mydb.db/my_user'
    > -- 只能是hdfs中的目录,直接为表加载数据
    > ;
OK
Time taken: 0.54 seconds

加载数据

hive表的数据加载:

1、直接将hdfs中数据使用命令上传到表所对应的目录即可。
[root@hadoop01 hivedata]# hdfs dfs -put ./t1 /user/hive/warehouse/mydb.db/my_user/

2、创建表的时候,使用location指定表所对应的目录即可。
create table if not exists mydb.my_user(
id int comment "this is userid",
name string
)
row format delimited fields terminated by ' '
lines terminated by '\n'
stored as textfile --存储格式
location '/user/hive/warehouse/mydb.db/my_user/'  --只能是hdfs中的目录,直接为表加载数据
;

3、使用load方式加载数据
load data local inpath '/home/data.txt' into table mydb.my_user; --默认复制
load data local inpath '/home/data.txt' overwrite into table mydb.my_user;
load data inpath '/data.txt' overwrite into table mydb.my_user;   --移动

4、使用insert into方式
法一:
set hive.exec.mode.local.auto=true;
insert into table t_user2
select id,name from t_user;

法二:
from t_user
insert into table t_user2
insert into table t_user3
select
id,
name
;

from t_user
insert into table t_user2
select
id,
name
where id > 2
;

法三:
set hive.exec.mode.local.auto=true;
with tmp as(
select
id,
name
from t_user
)
insert into table t_user2
select * from tmp;

5、使用ctas方式来
create table t_user3
as
select
name
from t_user
;

6、使用like方式(克隆)
create table t_user4 like t_user2;
create table t_user4 like t_user2 location '/user/hive/warehouse/my.db/my_user2';

等等 。

执行

[root@hadoop01 test]# cat data.txt
1,zzy
2,yjx
3,wyg
4,sk
5,hwl
[root@hadoop01 test]# hdfs dfs -put ./data.txt /user/hive/warehouse/mydb.db/my_user/

查看

原来我刚才设置的分隔符是’ ‘,但是数据里面用的是’,’,所以第一段数据不是int型,会自动用null代替,第二段为空…
那就修改数据,再加载一次

显示当前数据库

hive> set hive.cli.print.current.db=true;
hive (mydb)> 

查看表描述

desc my_user;
desc extended my_user;
show create table my_user;  --和mysql一样
describe my_user;
desc formatted my_user;

修改表

表名:rename to
hive (mydb)> alter table t_user rename to test_user;

添加字段:add columns
alter table test_user add columns(age int,info string);

字段名\字段类型\字段顺序: change column
可以强制转换的类型可以成功,如int转为string;
alter table test_user change age age string;
改变顺序的尝试了多种情况,只有表里全是string类型的列可以改变顺序,再研究吧...


删除字段:replace columns
alter table log3 replace columns(
id string,
phonenumber bigint,
mac string,
ip string,
url string,
status1 string,
status2 string,
upflow int,
downflow int,
status3 string,
dt string
);


内外部表:
alter table log3 set tblproperties('EXTERNAL'='true');  ###内部表转外部表,true一定要大写;
alter table log3 set tblproperties('EXTERNAL'='false'); ##false大小写都没有关系

注:
没有before和last。
内外部表的false可以大小写,而TRUE必须大写。