hive_02

hive


  • 数据仓库,OLAP,分析处理,存储和分析,延迟较高,不支持事务,不支持删除更新
  • 数据库,OLTP,在线事物处理,低延迟,事务支持
  • 运行在hadoop之上,类SQL方法方式运行,(HQL)
  • 操纵结构化数据
  • schema(模式,元信息存放到数据库中),HDFS文件,默认存储在derby,也支持外置存放mysql
  • 数据库和表都是路径


配置HIVE
1. conf/hive-env.sh
HADOOP_HOME= //不配也可以
2. conf/hive-site.xml
${system:java.io.tmpdir} //配置本地临时目录

$>schematool -initSchema -dbType derby //初始化模式

hive常用命令

$>!clear;         //hive中执行shell命令  
$>!dfs -lsr /;  //hive中执行hdfs命令  
$hive>create table hive1.t as select t2 ; //复制表

将hive中的schema存放到外部的mysql中

1. 编写hive-site.xml,添加链接信息  
[hive/conf/hive-site.xml]  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<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.ConnectionURL</name>
<value>jdbc:mysql://192.168.231.1:3306/myhive</value>
<description>
JDBC connect string for a JDBC metastore.
To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL.
For example, jdbc:postgresql://myhost/db?ssl=true for postgres database.
</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>
<property>
<name>hive.metastore.ds.connection.url.hook</name>
<value/>
<description>Name of the hook to use for retrieving the JDO connection URL. If empty, the value in javax.jdo.option.ConnectionURL is used</description>
</property>
<property>
<name>javax.jdo.option.Multithreaded</name>
<value>true</value>
<description>Set this to true if multiple threads access metastore through JDO concurrently.</description>
</property>
2. 在mysql中创建myhive数据库  
    $>create database myhive;
3. mysql驱动程序(jar)放置到hive classpath下  
    [/hive/lib]
4. 重新初始化hive的schema元数据库  
    $>schematool --initSchema -dbType mysql

启动hiveserver2服务,接受多个客户端连接请求,使得client通过jdbc连接操纵hive数据仓库

整体架构图
bp
$>hive/bin/hive –service hiverserver2 start //启动服务
$>ctrl+z //切换后台
$>jobs //查看任务
$>bg %1 //继续执行
$>netstat -ano | grep 10000 //查看端口

在eclipse中创建Maven项目

1.
2. 引入外部jar包
3. 修改hive-site.xml配置文件  
    以上过程需要修改配置文件  
    使用OS操作系统的认证方式  
    [hive-site.xml]  
    hive.server2.enable.doAs=false  
    hive.metastore.sasl.enabled=false  
    hive.server2.authentication=NONE  
4. 重启hiveserver2服务器  
    $>hive --service hiveserver2 stop  
    $>hive --service hiveserver2 start &  
    $>netstat -ano | grep 10000
5. 编写App程序  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) throws Exception{
Class.forName("org.apache.hive.jdbc.HiveDriver");

Connection connection = DriverManager.getConnection("jdbc:hive2://192.168.231.100:10000/hive1", "ubuntu", "123456");
PreparedStatement preparedStatement = connection.prepareStatement("select * from t");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {

int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(id + "," + name + "," + age);
}
rs.close();
preparedStatement.close();
connection.close();

}
6.常用聚集函数  
    count()
    sum()
    avg()
    max()
    min()

7.解决beeline命令行终端的上下键导航历史命令的bug  
    [bin/beeline]  
    修改行  
    if [[ ! $(ps -o stat= -p $$) =~ + ]]; then
    为  
    if [[ ! $(ps -o stat- -p $$) =~ "+" ]]; then  

hive命令

$hive>dfs -lsr /                                  //执行dfs命令  
$>hive>!clear;                                      //执行shell脚本  
$>hive -e "select * from test"                   //-e execute 一次性执行
$>hive -S -e "select * from test"                  //-S silent 静默,不输出OK,耗时  
$>hive -f /path/x.hql                              //-f 执行一个文件,通常用于批处理  
$hive>tab tab                                      //显示所有命令  
$hive>-- this is a comment                       //注释  
$hive>set hive.cli.print.header=true;           //现在字段名称(头)  
$hive>create database if not exists xxx         //
$hive>create database hive3 with dbproperties('author'='fth','createtime'='today')    //
$hive>alter database hive3 set dbproperties('author'='fth_enchancer')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

[创建表语法]
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[ROW FORMAT row_format]
[STORED AS file_format]

[例子]
CREATE TABLE IF NOT EXISTS employee ( eid int, name String, salary String, destination String)
COMMENT 'Employee details' //注释
ROW FORMAT DELIMITED //行格式定义
FIELDS TERMINATED BY '\t' //字段结束符
LINES TERMINATED BY '\n' //行结尾符
STORED AS TEXTFILE; //存储成何种文件
1
2
3
4
5
6
7
[加载数据	insert]
load data local '/home/ubuntu/employee.txt' overwrite
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename
[PARTITION (partcol1=val1, partcol2=val2 ...)]

[例子]
LOAD DATA LOCAL INPATH '/home/user/sample.txt' OVERWRITE INTO TABLE employee;
1
2
3
4
5
6
7
8
9
10
[创建分区表]  
create table test5(id int, name string, age int) partitioned by (province STRING,city STRING); //按照省份和城市分区

[例子]
create table hive1.test5(id int,name string,age int)
partitioned by(province string,city string)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;
1
2
[加载数据到指定分区]
load data local inpath '/home/ubuntu/employees.txt' into table hive1.test5 partition(province='hebei',city='baoding');

分区表操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[查看hdfs]
/user/hive/warehouse/hive1.db/test5/province=hebei/city=baoding/employees.txt
[查询分区表]
$hive>select * from hive1.test5 where province = 'hebei' and city = 'baoding';
[分区表的查询模式:strict/nostrict] [严格/非严格]
$hive>set hive.mapred.mode=strict || false?? //默认是非严格模式

[查看分区表有哪些分区]
$hive>show partitions hive1.test5
$hive>show partitions hive1.test5 partition(province='hebei'); //查看具体分区的细节信息
$hive>desc exteded hive1.test5 //查看扩展信息

[手动增加分区]
$hive>alter table hive1.test5 add partition(province='henan',city='pingdingshan')//
$hive>alter table hive1.test5 add partition(area='华北',province='henan',city='pingdingshan')//增加不存在分区列,是非法的

[修改表]
$hive>alter table hive1.test5 rename to hive1.test6; //重命名
$hive>alter table hive1.test5 add partition (province='hebei',city='zhangjiakou') location 'xxx' //添加多个分区
partition (province='hebei',city='zhangjiakou')
partition (province='hebei',city='zhangjiakou')
[移动分区]
$hive>alter table hive1.test5 partition(province=...,city=..) set location 'xxx'
[修改表字段???]
$hive>alter table hive1.test5 change column string
[增加列]
$hive>alter table hive1.test5 add columns(birth string,fire string)
[替换列]
$hive>alter table hive1.test5 replace columns(birth string,fire string)
[修改表属性]
$hive>alter table hive1.test5 set tblproperties ('birth'='number is needed');
[修改存储属性]

[启用归档]
$hive>set hive.archive.enabled=true //设置可以归档,默认false???

[复制数据到分区表]
$>insert into hive1.test5 partition (province='hebei',city='baoding') select * from hive1.test2;
insert into hive1.test6 partition ( province='hebei',city='baoding') select id,name,age,birth,fire from hive1.test6 where province='hebei' and city='shijiazhuang' and id > 5 ; //字段个数相同,查询时,分区通过where子句制定,插入时,分区用partition(...)指定

[动态分区]
$hive>insert overwrite table hive1.test6 partition (province,city) select id,name...,province,city
from table2;
$hive>create table hive1.test1(id int) tblproperties('author'='Tak');  //创建表,指定属性  
$hive>create table hive1.test1(id int) LOCATION '/user/ubuntu/test1';

$hive>create external table hive1.test3 like hive1,test1; //external创建外部表,只复制表结构,没有数据  

$>hive>create external table hive1.test4 as select * from hive1.test1;  //创建外部表,复制结构和数据

$hive>desc extended hive1.test1                 //显示扩展信息  
$hive>desc formatted hive1.test1                //显示格式化信息
$hive>create table hive2.test4 like hiv1.test1    //复制表  
$hive>show tables in hive1;                     //显示指定数据的表集合,默认是当前库  


$>hive>drop database if exists xxx                  //存在即删除
$>hive>drop database if exists xxx cascade        //级联删除  
$>hive>create database hive2 location '/user/ubuntu/hive2.db'  
$>hive>desc[rib] database hive3                        //显示DB信息,不包含扩展信息  
$>hive>desc[rib] database extended hive3            //显示DB信息,包含扩展信息  
$>hive>use hive3;                                //使用哪个库  

分区表

托管表

hive默认表都是托管表  (内部表)hive控制其数据的生命周期。删除托管表时,元数据和数据均被删除。  

外部表

hive控制元数据。删除托管表时,数据不被删除。  
create external table hive1.test3 like hive1.test1 ;

使用beeline客户端可以实现远程jdbc连接

1. 连接  
    $>beeline>!help                 //查看帮助  
    $>hive --service beeline -u jdbc:hive2://s100:10000/hive1  
    $>beeline -u jdbc:hive2://s100:10000/hive1
    $>beeline 连接主机
    $>beeline>!sh hdfs -lsr/ ;        //执行dfs命令  
    $>beeline>!sh clear ;             //执行shell脚本  
    $>beeline>show databases;         //查看库  
    $>beeline>!dbinfo                 //查看数据库信息  

2.
3.
4.
5.

配置hive的仓库位置

hive-site.xml  
hive.metastore.warehouse.dir=/user/hive/warehouse/  

hive数据类型

[数据类型] [java类型] [长度] [例子]
TINYINT byte 1byte 20
SMALINT short 2byte有符号整数 20
INT int 4byte有符号整数 20
BIGINT long 8byte有符号整数 20
BOOLEAN boolean 布尔类型,true或者false TRUE
FLOAT float 单精度浮点数 3.14159
DOUBLE double 双精度浮点数 3.14159
STRING String 字符序列。可以制定字符集。 ‘now is the time’,”for all good men”可以使用单引号或者双引号
TIMESTAMP(v0.8.0+) TimeStamp 整数,浮点数或者字符串 1327882394(Unix新纪元秒)
1267882394.123456789( Unix新纪元秒并跟随有纳秒数)和
‘2012-02-03 12:34:56:123456789’( JDBC所兼容 的java.sql.Timestamp时间格式)
BINARY(v0.8.0+) 字节数组 请看后面的讨论

[集合类型]
STRUCT struct(‘John’,’Doe’)
MAP map(‘first’,’John,’last’,’Doe’)
ARRAY array(‘John’,’Doe’)

hive所谓的读模式

hive在写操作是不校验的,读时校验

42:29