第二节:大数据仓库HIVE《01》

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hekaihaw/article/details/80962082

1、通过实际项目分析引出HIVE及HIVE是什么、HIVE前景如何

Hive
    HDFS
	存储数据
    YARN
	资源管理
    MapReduce
	数据处理
日志
日志内容,统一的规范
    * 每一行数据就是一条数据(RDBMS)
    * 很多列,统一的标识符,进行分割
schema
    模式
    约束
-------------------------------------------------------------------------
Hive
    * 处理的数据存储在HDFS
    * 分析数据底层的实现MapReduce
    * 执行程序运行的YARN
RDBMS
    表的概念
    create table test_log(
	ip string,
	user string,
	date string,
	......
    )
分析
HQL
    HiveQL
    select * from test_log limit 10;
    select substring( ip, 0, 4) ip_prex from test_log;
SQL On hadoop

2、Hive架构、优点及使用场景

* 操作接口采用类SQL语法,提供快速开发的能力(简单、容易上手)
* 避免了去写MapReduce,减少开发人员的学习成本
* 统一的数据管理,可与impala/spark等共享元数据
* 易扩展(HDFS+MapReduce:可以扩展集群规模;支持自定义函数)
* 数据的离线处理:比如:日志分析,海量结构化数据离线分析。。。
* Hive的执行延迟比较高,因此hive常用于数据分析的,对实时性要求不高的场合
* Hive优势在于处理大数据,对于处理小数据没有优势,因为Hive的执行延迟比较高

3、hive安装使用准备工作

* 下载地址:https://archive.apache.org/dist/hive/
* 参考文档:https://cwiki.apache.org/confluence/display/Hive/GettingStarted
* java 1.7,hadoop2.x,hive-0.13.1
安装步骤:
	* 解压apache-hive-0.13.1-bin.tar.gz至 /opt/modules
	* 修改/opt/modules/hive-0.13.1/conf/hive-env.sh中HADOOP_HOME=/opt/modules/hadoop-2.5.0
	* 修改/opt/modules/hive-0.13.1/conf/hive-env.sh中export HIVE_CONF_DIR=/opt/modules/hive-0.13.1/conf
		bin/hdfs dfs -mkdir /tmp
		bin/hdfs dfs -mkdir -p /user/hive/warehouse
		bin/hdfs dfs -chmod g+w /tmp
		bin/hdfs dfs -chmod g+w /user/hive/warehouse
	* cd /opt/modules/hive-0.13.1/
		bin/hive
	进入hive命令行
		show databases;
		show tables;
		....

4、Hive初步使用和安装Mysql

show databases;
use default;
show tables;
create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
load data local inpath '/opt/datas/student.txt' into table student;
mysql安装(百度查看),安装后
	mysqld_sate --user=mysql --skip-grant-tables --skip-networking &
	update user set password=password('123456') where user='root'

	use mysql;
	select * from user;
	update user set host='%' where user='root' and host='localhost';
	delete from user where user='root' and host in('::1', '127.0.0.1');

5、Hive配置metastore

配置的hive metastore
Mysql
	与我们hive安装在同一台机器上

修改/opt/modules/hive-0.13.1/conf/hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>

	<!-- WARNING!!! This file is provided for documentation purposes ONLY!     -->
	<!-- WARNING!!! Any changes you make to this file will be ignored by Hive. -->
	<!-- WARNING!!! You must make your changes in hive-site.xml instead.       -->


	<property>
	  <name>javax.jdo.option.ConnectionURL</name>
	  <value>jdbc:mysql://hadoop-senior:3306/metastore?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>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>

</configuration>

cp /opt/softwares/mysql-libs/mysql-connector-java-5.1.27/mysql-connector-java-5.1.27-bin.jar /opt/modules/hive-0.13.1/lib

bin/hive
show databases;

6、Hive基本操作使用

show databases;
create database db_hive;
create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
show tables;
desc student;
desc extended student;
desc formatted student;
use db_hive;
load data local inpath '/opt/datas/student.txt' into table student;
select count(1)/sum(xx)/avg(xx)..
show functions;
desc function upper;

7、hive运行日志配置和查看

位于:/opt/modules/hive-0.13.1/conf/hive-log4j.propetties
自定义配置日志输出路径:hive.log.dir=/opt/modules/hive-0.13.1/logs

8、hive常见属性配置

* Hive数据仓库位置配置
	default
		/user/hive/warehouse
	注意事项
		* 在仓库目录下,没有对默认的数据库default创建文件夹
		* 如果某张表属于default数据库,直接在数据仓库目录下创建一个文件夹
		hive-default.xml.template
		<property>
			<name>hive.metastore.warehouse.dir</name>
			<value>/user/hive/warehouse</value>
		</property>
		$ $HADOOP_HOME/bin/hadoop fs -mkdir       /tmp
		$ $HADOOP_HOME/bin/hadoop fs -mkdir       /user/hive/warehouse
		$ $HADOOP_HOME/bin/hadoop fs -chmod g+w   /tmp
		$ $HADOOP_HOME/bin/hadoop fs -chmod g+w   /user/hive/warehouse
* Hive运行日志信息位置
	$HIVE_HOME/conf/hive-log4j.property
		hive.log.dir=/opt/modules/hive-0.13.1/logs
		hive.log.file=hive.log
* 指定hive运行时显示的log日志的级别
	$HIVE_HOME/conf/hive-log4j.property
		hive.root.logger=INFO,DRFA
* 在cli命令行上显示当前数据库,以及查询表的行头信息
	$HIVE_HOME/conf/hive-site.xml
	<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 --hiveconf <property=value>
* 查看当前所有的配置信息
	hive > set;
	hive (db_hive)> set system:user.name;
		system:user.name=hadoop
	hive (db_hive)> set system:user.name=hadoop;
	
	此种方式,设置属性的值,仅仅在当前会话session生效。

9、hive常用集中交互操作

bin/hive -help
* bin/hive -e
eg:
	bin/hive -e "select * from db_hive.student;"
* bin/hive -f <filename>
eg:
	$touch hivef.sql
		select * from db_hive.studeng;
	
	$ bin/hive -f /opt/datas/hivef.sql
	$ bin/hive -f /opt/datas/hivef.sql > /opt/datas/hive-res.txt
* bin/hive -i <filename>
	与用户udf相互使用
在hive cli命令窗口中如何查看hdfs文件系统
	hive (default)> dfs -ls /
在hive cli命令窗口中如何查看本地文件系统
	hive (default)> !ls /opt/datas;
查看操作历史命令
	$HOME/.hivehistory

猜你喜欢

转载自blog.csdn.net/hekaihaw/article/details/80962082
今日推荐