Hive

Hive

1.概念

Hive是基于Hadoop的数据仓库,SQL ON HADOOP,将结构化的文件映射成一张表,并提供类sql查询的功能。

数据库和数据仓库

数据库:mysql,oracle,sqlsever,db2,sqlite,mdb

数据仓库:Hive,是MR的客户端,不必每台机器都安装部署Hive。

2.基本组成

用户接口:包括 CLI、JDBC/ODBC、WebGUI。

元数据存储:通常是存储在关系型数据库中,如MySQL中。

解释器、编译器、优化器、执行器。

用户接口主要由三个:CLI、JDBC/ODBC和WebGUI。其中,CLI为shell命令行;JDBC/ODBC是Hive的JAVA实现,与传统数据库JDBC类似;WebGUI是通过浏览器访问Hive。

元数据存储:Hive 将元数据存储在数据库中。Hive 中的元数据包括表的名字,表的列和分区及其属性,表的属性(是否为外部表等),表的数据所在目录等。

解释器、编译器、优化器完成 HQL 查询语句从词法分析、语法分析、编译、优化以及查询计划的生成。生成的查询计划存储在 HDFS 中,并在随后有 MapReduce 调用执行。

3.特点

1.操作接口是采用SQL语法,HQL

2.避免了写MapReduce的繁琐过程

4.数据存储

1、Hive中所有的数据都存储在 HDFS 中,没有专门的数据存储格式(可支持Text,SequenceFile,ParquetFile,RCFILE等)

2、只需要在创建表的时候告诉 Hive 数据中的列分隔符和行分隔符,Hive 就可以解析数据。

3、Hive 中包含以下数据模型:DB、Table,External Table,Partition,Bucket。

²  db:在hdfs中表现为${hive.metastore.warehouse.dir}目录下一个文件夹

²  table:在hdfs中表现所属db目录下一个文件夹

²  external table:与table类似,不过其数据存放位置可以在任意指定路径

²  partition:在hdfs中表现为table目录下的子目录

²  bucket:在hdfs中表现为同一个表目录下根据hash散列之后的多个文件

5.部署配置

hive-site.xml

<property>
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://hadoop102.com:3306/metastore?createDatabaseIfNotExist=true</value>
</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>123456</value>
  <description>password to use against metastore database</description>
</property>
<!-- 是否在当前客户端中显示查询出来的数据的字段名称 -->
<property>
  <name>hive.cli.print.header</name>
  <value>true</value>
  <description>Whether to print the names of the columns in query output.</description>
</property>

<!-- 是否在当前客户端中显示当前所在数据库名称 -->
<property>
  <name>hive.cli.print.current.db</name>
  <value>true</value>
  <description>Whether to include the current database in the Hive prompt.</description>
</property>

 


  启动Hive

$ bin/hive

** 创建数据库
hive> create database staff;

** 创建表操作
hive> create table t1(eid int, name string, sex string) row format delimited fields terminated by '\t';

** 导入数据

*** 从本地导入
load data local inpath '文件路径' into table;
*** 从HDFS系统导入

load data inpath '文件路径' into table;

Hive操作HQL语句

hive -e ""
hive -f 文件.hql

Hive历史命令存放地

cat ~/.hivehistory
主要用于排查逻辑错误或者查看常用命令

Hive临时生效设置

固定语法:set 属性名=属性值
例如:set hive.cli.print.header=false;

Hive的内部表与外部表

伪代码:
hive> CREATE TABLE custom_table(id int, name string) location '/custom/z/hive/somedatabase'
默认情况:inner
hive> CREATE INNER TABLE(报错)
显示指定:external
hive> CREATE EXTERNAL TABLE

内部表:
删除表数据时,连同数据源以及元数据信息同时删除
外部表:
1、只会删除元数据信息。
2、共享数据,外部表相对而言也更加方便和安全。

相同之处:
如果你导入数据时,操作于HDFS上,则会将数据进行迁移,并在metastore留下记录,而不是copy数据源。

 

Hive分区表

创建分区表:create database if not exists db_web_data ;

create table if not exists db_web_data.track_log(

id string,

url string,

referer string

)
partitioned by (date string,hour string) -- 分区表的分区字段以逗号分隔
row format delimited fields terminated by '\t';

 

导入数据到分区表:
hive> load data local inpath '/home/admin/Desktop/2015082818' into table db_web_data.track_log partition(date='20150828', hour='18');
查询分区表中的数据:
hive> select url from track_log where date='20150828';查询28整天的数据
hive> select url from track_log where date='20150828' and hour='18'; 查询28号18时那一刻的数据
select url from track_log where date='20150828' and hour='18' limit 1;显示第一条

 

HiveServer2 

beeline(Hive命令行客户端工具)

配置:hive-site.xml
hive.server2.thrift.port --> 10000
hive.server2.thrift.bind.host --> hadoop-senior01.itguigu.com
hive.server2.long.polling.timeout -- > 5000(去掉L)
检查端口:
$ sudo netstat -antp | grep 10000
启动服务:
$ bin/hive --service hiveserver2
连接服务:
$ bin/beeline
beeline> !connect jdbc:hive2://hadoop102:10000

 

自定义UDF函数

编写代码,打包jar文件

添加打包后的jar
hive (db_web_data)> add jar xxx.jar;

添加临时函数
hive (db_web_data)> create temporary function dateformat as 'com.z.demo.udf.DataTransformUDF';

查看所有函数

show functions;

 

posted @ 2019-07-25 15:27  孤身!  阅读(172)  评论(0编辑  收藏  举报