Hadoop实战(8)_CDH添加Hive服务及Hive基础

CDH Hadoop系列目录:

Hadoop实战(3)_虚拟机搭建CDH的全分布模式

Hadoop实战(4)_Hadoop的集群管理和资源分配

Hadoop实战(5)_Hadoop的运维经验

Hive体系结构

Hive有2个服务端守护进程:Hiveserver2:支撑JDBC访问,Thrift服务。MetaStore Server:支撑访问元数据库的服务。

Hive内核结构

Complier:编译器,编译hql语法。

Optimizer:优化hql代码,产生最优执行计划。通过explain select …查看执行计划。

Executor:执行最终转化的类(MRjob)。

Hive用户接口

用户接口主要有三个:CLI, JDBC/ODBC和WebGUI。

CLI,即hive shell命令行,Command line。

JDBC/ODBC是Hive的JAVA,与使用传统数据库JDBC的方式类似。

WebGUI是通过浏览器访问Hive,废弃功能。

添加Hive服务

添加服务-Hive,Gateway空,Hive Metastore Server选择cdhmaster,HiveServer2选择cdhslave1。使用嵌入式数据库测试连接跳过。

安装MySQL

yum list | grep mysql
yum install -y mysql-server
# 启动mysql服务
chkconfig --list | grep mysql
service mysqld start
chkconfig mysqld on
chkconfig --list | grep mysql
# 创建root管理员
mysqladmin -u root password 123456
# 登录mysql
mysql -u root -p
# 设置字符集,否则会造成转码问题
create database hive;
alter database hive character set latin1;
# 设置访问权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

字符集不正确的话,可能报错。

FAILED: Error in metadata: MetaException(message:Got exception: org.apache.thrift.transport.TTransportException null)
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask

MySQL驱动,把mysql的驱动mysql-connector-java-5.1.18-bin.jar放在/opt/cloudera/parcels/CDH/lib/hive/lib/下。

(可选)复制mysql-connector-java-5.1.18-bin.jar/usr/share/cmf/lib/,供cm界面用,添加hive服务跳过元数据库配置即这个驱动包可能会找不到。

Hive元数据库设置

进入cm的hive服务-配置中,

先进行资源管理,Hive Metastore Server的Java堆栈大小,200M。Hive Server2的Java堆栈大小,200M。

Hive Metastore数据库,选择MySQL。Hive Metastore数据库名称,hive。Hive Metastore数据库主机,cdhmaster。Hive Metastore数据库端口,3306。Hive Metastore数据库用户,root。Hive Metastore数据库密码,123456。自动创建和升级Hive Metastore数据库架构,打勾。严格的Hive Metastore架构验证,不打勾。

然后启动Hive服务,观察Metastore Server是否能连上mysql(实例点进去查看角色的日志)。如果连不上,就检查grant访问mysql的权限。

[main]: Failed initialising database.
Unable to open a test connection to the given database. JDBC url = jdbc:mysql://cdhmaster:3306/hive?useUnicode=true&characterEncoding=UTF-8, username = root. Terminating connection pool (set lazyInit to true if you expect to start your database after your app). Original Exception: ------
java.sql.SQLException: Access denied for user 'root'@'cdhmaster' (using password: YES)

GRANT ALL PRIVILEGES ON *.* TO 'root'@'cdhmaster' IDENTIFIED BY '123456' WITH GRANT OPTION;

远程元数据库

元数据库可以安装在任何节点上,客户端通过MetaStoreServer服务访问元数据库。

(Meta Store Client/Hive CLI)-MetaStore Server(thrift)-MySQL Server

属性默认值
hive.metastore.localtruefalse
hive.metastore.uris如thrift://192.168.1.110:9083

Hive命令

show databases;
use default;
create table test(id int, name string);
desc test;


内部表,又称托管表,drop后数据丢失。

外部表:create external table tableName,drop表时数据不会删除。

alter table set location '';
alter table add partition(date='') location '';

默认分隔符,列为\001,行为\n。

create external table page_view_stg
(userid bigint,
 url string,
 ip string comment 'IP Address of the User')
row format delimited fields terminated by '\t'
partitioned by (ds string, type string)
lines terminated by '\n'
stored as textfile
location '/user/hive/external/city';

字段类型

  • int
  • bigint,长整型
  • double,金额类
  • string,字符串,日期,非数值型的一切可以用string

Cli

hive -e “select …”

hive -f aa.sql

hive -e -i -i的作用是加载初始化命令,比如UDF

create database dw location '/user/hive/dw';

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Got exception: org.apache.hadoop.security.AccessControlException Permission denied: user=root, access=WRITE, inode="/user/hive":hive:hive:drwxrwxr-t

解决办法,用hdfs帐户执行

su - hdfs
hadoop fs -chmod 777 /user/hive
hive
use dw;
create table aa(name string);

分区

关系DB的分区都是事先建好,一般都是通过某个字段的范围,比如date。

Hive的分区是写数据进去的时候自动建的,分区表insert时必须指定分区。

把一个文件入到Hive表有2中方式:

方式1:通过load命令

方式2:首先hadoop fs -put至HDFS,然后alter location。

Hive的insert有2种,insert overwrite(覆盖),insert into(追加)。

create table track_log (
id                         string ,
url                        string ,
referer                    string ,
keyword                    string ,
type                       string ,
guid                       string ,
pageId                     string ,
moduleId                   string ,
linkId                     string ,
attachedInfo               string ,
sessionId                  string ,
trackerU                   string ,
trackerType                string ,
ip                         string ,
trackerSrc                 string ,
cookie                     string ,
orderCode                  string ,
trackTime                  string ,
endUserId                  string ,
firstLink                  string ,
sessionViewNo              string ,
productId                  string ,
curMerchantId              string ,
provinceId                 string ,
cityId                     string )  
PARTITIONED BY (date string,hour string)  
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
hive -e "LOAD DATA LOCAL INPATH '/root/data/2015082818' OVERWRITE INTO TABLE track_log PARTITION (date='2015-08-28',hour='18');"

hive -e "LOAD DATA LOCAL INPATH '/root/data/2015082819' OVERWRITE INTO TABLE track_log PARTITION (date='2015-08-28',hour='19');"
select date,count(url) as pv, count(distinct guid) as uv from track_log where date='2015-08-28' group by date;

分区字段名不能和普通字段重复,分区字段用起来和普通字段没区别。

动态分区

表1是日期分区,需要把表1中数据写入表2(日期、小时分区)?

insert overwrite table table2 partition(date='', hour='00') 
select 
from table1 
 where hour(time)=0;
create table rpt_visit_daily_hour 
(
    pv bigint,
    uv bigint
) partitioned by (date string, hour string);

insert overwrite table rpt_visit_daily_hour partition (date='2015-08-28', hour) 
select count(url) as pv, 
count(distinct guid) as uv, 
hour 
from track_log 
where date='2015-08-28' group by date,hour;

Hive表数据的来源

  • 业务系统,sqoop用于关系db和hive/hdfs导入导出。
  • 数据文件,hive load命令,用于加载网站用户行为数据。
  • 其他数据表,insert … select
  • 消息中间件,比如kafka离线消费写HDFS。

Q:drop后的外部表在什么位置?

A:外部表数据没有删除,只是删除了表的元数据信息,手工把HDFS目录映射到hive表分区:
hive -e “alter table tt add partition (date=”,hour=”) location ‘/user/hive/warehouse/track_log/date=2015-08-28/hour=18′”

Hive官方文档:

https://cwiki.apache.org/confluence/display/Hive/Tutorial

您可能还想看

数据分析/数据挖掘/机器学习

Python数据挖掘与机器学习_通信信用风险评估实战(1)——读数据

Python数据挖掘与机器学习_通信信用风险评估实战(2)——数据预处理

Python数据挖掘与机器学习_通信信用风险评估实战(3)——特征工程

Python数据挖掘与机器学习_通信信用风险评估实战(4)——模型训练与调优

爬虫

Python爬虫实战之爬取链家广州房价_01简单的单页爬虫

Python爬虫实战之爬取链家广州房价_02把小爬虫变大

Python爬虫实战之爬取链家广州房价_03存储

Python爬虫实战之爬取链家广州房价_04链家的模拟登录(记录)

搜狗词库爬虫(1):基础爬虫架构和爬取词库分类

搜狗词库爬虫(2):基础爬虫框架的运行流程

微信公众号「数据分析」,分享数据科学家的自我修养,既然遇见,不如一起成长。

《Hadoop实战(8)_CDH添加Hive服务及Hive基础》 数据分析

转载请注明:转载自微信公众号「数据分析」

    原文作者:padluo
    原文地址: https://www.jianshu.com/p/7232b74e4a06
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞