Hivejava
Author: Lijbmysql
Email: lijb1121@163.comweb
Hive介绍:sql
hive是基于Hadoop的一个数据仓库工具,能够用来进行数据踢群转换加载(ETL),这是一种能够存储、查询和分析存储在Hadoop中的大规模数据机制。能够将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,能够将sql语句转换为MapReduce任务进行运行。
ETL介绍:数据库
什么是etl Extract-Transform-Load):apache
一、用来描述将数据历来源端通过抽取(extract)、转换(transform)、加载(load)至目的端的过程。ETL一词较经常使用在数据仓库。是一个数据清洗工具 二、实现ETL,首先要实现ETL转换的过程。体现为如下几个方面: 一、空值处理:可捕获字段空值,进行加载或替换为其余含义数据,并可根据字段空值实现分流加载到不一样目标库。 二、规范化数据格式:可实现字段格式约束定义,对于数据源中时间、数值、字符等数据,可自定义加载格式。 三、拆分数据:依据业务需求对字段可进行分解。例,主叫号 861082585313-8148,可进行区域码和电话号码分解。 四、验证数据正确性:可利用Lookup及拆分功能进行数据验证。例如,主叫号861082585313-8148,进行区域码和电话号码分解后,可利用Lookup返回主叫网关或交换机记载的主叫地区,进行数据验证。 五、数据替换:对于因业务因素,可实现无效数据、缺失数据的替换。 六、Lookup:查获丢失数据 Lookup实现子查询,并返回用其余手段获取的缺失字段,保证字段完整性。 七、创建ETL过程的主外键约束:对无依赖性的非法数据,可替换或导出到错误数据文件中,保证主键惟一记录的加载。
Hive的架构图json
driver是hive中内置的核心驱动driver又包括三个组件,Compiler/Optimizer/Excutorcentos
Compiler:会把咱们写的hql编译成原生的mapreduce程序 Optimizer:优化器把hql编译成MR之后,MR会执行提交,Optimizer:会在MR执行提交以前对程序进行一些优化。优化无非就是在job里面添加不少参数或者配置等策略。 Excutor:执行器,把优化后的mapreduce程序交给hadoop的resourcemanager去处理 总结:编译--优化--提交到Hadoop的亚组件。 Hive操做方式CLI/JDBC(ODBC)/Web(GUI) CLI:经过客户端指令去操做 JDBC:sql管理起来 经过jdbc去操做 Web:经过访问web界面的方式去访问hive Thrift server:对象的序列化方式,可能jdbc和hive是写到不一样的数据库节点上,因此跨界点通讯的话要进行序列化. Metastore:用来存储hive中表的元数据的工具,也就是存元数据的指令 元数据:表的名字,表的列和分区及其属性,表的属性(是否为外部表等),以及表所在的数据存储目录等等 derby:数据库,hive内置了derby数据库。该数据库只支持单会话。数据无法共享 mysql:数据能够共享
Hive的安装数组
安装mysql并开启远程访问架构
安装Hadoop环境并启动
安装Hive
[root@centos ~]# tar -zxf apache-hive-1.2.1-bin.tar.gz -C /usr/ [root@centos ~]# vi /usr/apache-hive-1.2.1-bin/conf/hive-site.xml
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://CentOS:3306/hive</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>root</value> </property>
</configuration>
mysql> create database hive; Query OK, 1 row affected (0.00 sec) mysql> show create database hive; +---------- +----------------------------------------------------------------- + | Database | Create Database | +---------- +----------------------------------------------------------------- + | hive | CREATE DATABASE hive
/*!40100 DEFAULT CHARACTER SET latin1 */ | +---------- +----------------------------------------------------------------- + 1 row in set (0.00 sec) mysql> quit Bye [root@centos ~]# cp mysql-connector-java-5.1.46.jar /usr/apache-hive-1.2.1-bin/lib/ [root@centos ~]# cd /usr/apache-hive-1.2.1-bin/ [root@centos apache-hive-1.2.1-bin]# cp lib/jline-2.12.jar /usr/hadoop-2.6.0/share/hadoop/yarn/lib/ [root@centos apache-hive-1.2.1-bin]# rm -rf /usr/hadoop-2.6.0/share/hadoop/yarn/lib/jline-0.9.94.jar
启动Hive(单机|管理员)
[root@centos apache-hive-1.2.1-bin]# ./bin/hive Logging initialized using configuration in jar:file:/usr/apache-hive-1.2.1-bin/lib/hive-common- 1.2.1.jar!/hive-log4j.properties hive>
多用户访问
[root@centos apache-hive-1.2.1-bin]# ./bin/hiveserver2 (服务端) [root@centos apache-hive-1.2.1-bin]# ./bin/beeline -u jdbc:hive2://CentOS:10000 -n root Connecting to jdbc:hive2://CentOS:10000 Connected to: Apache Hive (version 1.2.1) Driver: Hive JDBC (version 1.2.1) Transaction isolation: TRANSACTION_REPEATABLE_READ Beeline version 1.2.1 by Apache Hive
Hive对表的操做
表分类
HIVE的表按类型 分为 管理表、外部表、分区表。
管理表:前面章节案例中所建立的表都称为管理表,有时候也称为内部表。由于这种表hive会控制数据的生命周期(在删除表的同时也会删除dhfs上的文本数据)所以管理表不方便和其余工做共享数据。
外部表: 和管理表比较外部表在建立的时候多一个中external关键字告诉Hive这是外部表,然后面的location则是告诉hive数据位于哪一个位置或者路径下。由于是外部表hive并不是彻底拥有这些数据,所以在删除外部表的时候并不会删除location参数指定路径的数据。
分区表: 不管外部表仍是管理表均可以在建立的时候指定分区,对于这种指定分区的表称为分区表。数据库分区的概念已经纯在好久,其能够有多种形式,可是一般是使用分区来水平分散压力,将数据从物理上转移和使用最频繁的用户更接近的地方,以及实现其余目的。
外部表
1,zhangsan,true,18,15000,TV|Game,001>建设|002>招商,china|bj 2,lisi,true,28,15000,TV|Game,001>建设|002>招商,china|bj 3,wangwu,false,38,5000,TV|Game,001>建设|002>招商,china|sh \------ create external table t_user_c( id int, name string, sex boolean, age int, salary double, hobbies array<string>, card map<string,string>, addressstruct<country:string,city:string> ) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n'; create external table t_user_a( id int, name string, sex boolean, age int, salary double, hobbies array<string>, card map<string,string>, addressstructcountry:string,city:string ) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n' location '/user/hive/warehouse/baizhi.db/t_user_c';
分区表
create external table t_user( id int, name string, sex boolean, age int, salary double, country string, city string ) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n' -------------------- create external table t_user_p( id int, name string, sex boolean, age int, salary double ) partitioned by(country string,city string) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n'
Hive On Hbase
CREATE EXTERNAL tablet_user_hbase( id string, name string, age int, salary int, company string ) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITHSERDEPROPERTIES('hbase.columns.mapping' = ':key,cf1:name,cf1:age,cf1:salary,cf1:company')TBLPROPERTIES('hbase.table.name' = 'zpark:t_user');
数据类型
①primitive(原始类型):
整数:TINYINT、SMALLINT、INT、BIGINT
布尔:BOOLEAN
小数:FLOAT、DOUBLE
字符:STRING、CHAR、VARCHAR
二进制:BINARY
时间类型:TIMESTAMP、DATE
②array(数组类型):ARRAY < data_type >
③map(key-value类型):MAP< primitive_type, data_type >
④struct(结构体类型):STRUCT <col_name:data_type, ...>
建表、创建映射关系
create table t_user01(id int,name varchar(40)) load data local inpath '/root/user1.log' into table t_user; load data local inpath '/root/user2.log' overwrite into table t_user2; create table t_order( id int,name varchar(32), num int, price double, tags array<string>, user_id int
默认分割符
分隔符 描述
\n 对于文原本说,每一行都是一条记录。所以\n能够分割记录。
^A(Ctrl+a) 用于分割字段(列),在create table中可使用\001表示。
^B(Ctrl+b) 用于分割array或者是struct中 的元素或者用于map结构中的k-v对的分隔符,在create table
中可使用\002表示。
^C(Ctrl+c) 用于Map中k-v的分隔符,在create table中可使用\003表示。
create table t_user( id int, name string, sex boolean, birthDay date, salary double, hobbies array<string>, card map<string,string>, address struct<country:string,city:string> ) 0: jdbc:hive2://CentOS:10000> desc formatted t_user;
准备数据
将数据导入到表中
#不经常使用 [root@centos ~]# hdfs dfs -put t_user /user/hive/warehouse/baizhi.db/t_user //使用hive加载数据 0: jdbc:hive2://CentOS:10000> load data local inpath '/root/t_user' overwrite into table t_user;
自定义分隔符(用的较多)
1,zhangsan,true,18,15000,TV|Game,001>建设|002>招商,china|bj 2,lisi,true,28,15000,TV|Game,001>建设|002>招商,china|bj 3,wangwu,false,38,5000,TV|Game,001>建设|002>招商,china|sh ------ create table t_user_c( id int, name string, sex boolean, age int, salary double, hobbies array<string>, card map<string,string>, address struct<country:string,city:string> ) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n';
正则匹配
192.168.0.1 qq com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.2.1 qq com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.0.1 xx com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.202.1 qq com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.2.1 qq com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.0.2 xx com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.0.2 qq com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.2.4 qq com.xx.xx.XxxService#xx 2018-10-10 10:10:00 192.168.0.4 xx com.xx.xx.XxxService#xx 2018-10-10 10:10:00 --------------------- create table t_access( ip string, app varchar(32), service string, last_time string ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe' WITH SERDEPROPERTIES ( "input.regex"="^(.*)\\s(.*)\\s(.*)\\s(.*\\s.*)" );
CSV格式文件
1,zhangsan,TRUE,20 2,zhangsan,TRUE,21 3,zhangsan,TRUE,22 4,zhangsan,TRUE,23 ------------------ CREATE TABLE my_table( id int, name string, sex boolean, age int ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' WITH SERDEPROPERTIES ( "separatorChar" = ",", "escapeChar" = "\\" );
JSON格式数据
{"id":1,"name":"zhangsan","sex":true,"birth":"1991-02-08"} {"id":2,"name":"lisi","sex":true,"birth":"1991-02-08"} --------------------- ADD|DELETE JAR /usr/apache-hive-1.2.1-bin/hcatalog/share/hcatalog/hive-hcatalog-core-1.2.1.jar ; create table t_user_json( id int, name varchar(32), sex boolean, birth date )ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe';
数据的导入/导出(管理员)
INSERT OVERWRITE DIRECTORY /logs/ ROW FORMAT delimited fields terminated by ',' SELECT ip , app,last_time from t_access; 0: jdbc:hive2://CentOS:10000> create table t(id string ,name string); 0: jdbc:hive2://CentOS:10000> INSERT into t(id,name) values(1,'zs'),(2,'ww'); 0: jdbc:hive2://CentOS:10000> insert into table t select ip,last_time from t_access; 0: jdbc:hive2://CentOS:10000> create table temp1 as select ip,last_time from t_access;
表分类
HIVE的表按类型 分为 管理表、外部表、分区表。
外部表
1,zhangsan,true,18,15000,TV|Game,001>建设|002>招商,china|bj 2,lisi,true,28,15000,TV|Game,001>建设|002>招商,china|bj 3,wangwu,false,38,5000,TV|Game,001>建设|002>招商,china|sh \------ create external table t_user_c( id int, name string, sex boolean, age int, salary double, hobbies array<string>, card map<string,string>, addressstruct<country:string,city:string> ) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n'; create external table t_user_a( id int, name string, sex boolean, age int, salary double, hobbies array<string>, card map<string,string>, addressstructcountry:string,city:string ) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n' location '/user/hive/warehouse/baizhi.db/t_user_c';
分区表
create external table t_user( id int, name string, sex boolean, age int, salary double, country string, city string ) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n' -------------------- create external table t_user_p( id int, name string, sex boolean, age int, salary double ) partitioned by(country string,city string) row format delimited fields terminated by ',' collection items terminated by '|' map keys terminated by '>' lines terminated by '\n'
Hive On Hbase
CREATE EXTERNAL tablet_user_hbase( id string, name string, age int, salary int, company string ) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITHSERDEPROPERTIES('hbase.columns.mapping' = ':key,cf1:name,cf1:age,cf1:salary,cf1:company')TBLPROPERTIES('hbase.table.name' = 'zpark:t_user');
Linux上刷新mysql权限:flush privileges;
启动mysql: service mysqld start e
进入MySQL:mysql -uroot -p
使用数据库: use test
查看表: show tables
...