hive简介
- 基于hadoop的数据仓库工具
- 将结构化的数据文件映射为数据库表
- 提供类SQL的查询功能
hive特点
- 可扩展
hive可以自由扩展集群的规模,一般情况下不需要重启服务 - 延展性
hive支持用户自定义函数,用户可以根据自己的需要来实现自己的函数 - 容错
良好的容错性,节点出现问题SQL仍可完成执行
hive架构
- 可通过CLI、JDBC/ODBC(JAVA实现)、GUI来操作hive
- 元数据默认存储在derby数据库中,也可以存储在mysql、oracle中。包含表名、列名、分区及属性、表的属性等
- driver接收客户端请求,通过解释器、编译器、优化器生成mapreduce,提交集群运行,结果保存在HDFS中
hive与RDBMS的区别
类别 | hive | RDBMS |
---|---|---|
查询语言 | HQL | SQL |
数据存储 | HDFS | raw device or local FS |
执行 | mapreduce | excutor |
执行延迟 | 高 | 低 |
处理数据规模 | 大 | 小 |
索引 | 0.8版本后加入位图索引 | 有复杂的索引 |
hive的数据存储
- hive中所有的数据存储在hdfs中,没有专门的数据格式,可支持text、sequenceFile、parquetFile、rcFile等
- 只需要在创建表的时候,指定分隔符和换行符,hive即可解析数据
hive数据模型
- DB
在hdfs中表现为${hive.metastore.warehouse.dir}目录下的一个文件夹 - table(内部表)
在hdfs中表现为DB目录下的一个文件夹 - external table(外部表)
与table类似,不过其数据存放位置可以在任意指定路径 - partition(分区)
在hdfs中表现为table目录下的子目录 - bucket(桶)
在hdfs中表现为同一表目录下根据某字段hash散列之后的多个文件
hive部署
- hive只需要装在一台机器(有hadoop)
- 解压安装包
- 配置环境变量
export HIVE_HOME=/var/local/hive
export PATH=$PATH:$HIVE_HOME/bin - 修改配置文件
hive-site.xml
mysql地址
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>
驱动
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>
用户名
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>username to use against metastore database</description>
</property>
密码
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
<description>password to use against metastore database</description>
</property>
</configuration>
hive-env.sh
HADOOP_HOME=/var/local/hadoop
- 把mysql驱动放到hive/lib中
- 启动hive
hive
如有版本冲突,则把hadoop下的jline包替换hive中的jline
DDL
- 外部表和内部表的区别
- 删除了内部表,数据即删除
- 删除了外部表,数据不会被删除
- 建表语句
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]
example:
-
创建内部表
create table student(id int,name string,age int)
row format delimited(hive默认行分隔符)
fields terminated by ‘,’; -
加载本地数据到hive
load data local inpath ‘/root/students.txt’ into table student; -
加载HDFS数据到hive
load data inpath ‘/student1.txt’ into table student; -
创建外部表
create external table t_ext(id int,name string,age int)
row format delimited
fields terminated by ‘,’
location ‘/hivedata’;(HDFS上的数据路径) -
创建分区表(查询时利用where,可以提高效率)
create table t_partition(ip string,duration int)
partitioned by(country string)
row format delimited
fields terminated by ‘,’;load data local inpath ‘/root/hivedata/t_part’ into table t_partition partition(country=“China”);
-
创建多级分区表
create table t_partition(ip string,duration int)
partitioned by(country string,city string)
row format delimited
fields terminated by ‘,’; -
指定表的存储格式
textfile(默认)、sequencefile、rcfile
如果文件数据是纯文本,可以使用textfile
如果数据需要压缩,使用sequencefilecreate table t_2(id int,name string)
row format delimited
fields terminated by ‘,’
stored as textfile; -
当保存格式为sequencefile时,加载数据不能使用load,应使用:
insert overwrite table t_3 select * from t_2;