Hive基础知识了解

hive是什么

hive是hadoop的一个数据仓库工具,可以将结构化的数据文件映射成一张表,并且提供sql查询功能。本质是将HQL转化为MapReduce程序,适合离线计算处理。

Hive处理的数据存储在HDFS上的
分析数据底层的实现是MapReduce进行计算的 执行的程序运行在yarn上的

hive架构

  • client
    CLI(hive shell) jdbc/odbc webui(浏览器访问)
  • 元数据 包括表名,表所属的数据库,表的拥有者,表的类型 列分区字段,表的数据所在目录。
  • hadoop 数据存储在HDFS上的,并且使用MapReduce进行计算的。
  • driver 包括解析器,编译器,优化器,执行器
    • 解析器 将sql转化为抽象的语法树AST,即对sql字符串进行解析。
    • 编译器 将AST编译生成逻辑执行计划。
    • 优化器 对逻辑执行计划进行优化。
    • 执行器 把逻辑执行计划转化为可运行的物理计划。

hive的优点和使用场景

  • 数据的离线处理
  • hive的执行延迟比较高,因此适合用于数据分析,对实时性要求不高的场合。
  • hive的优势在于处理大数据,对于处理小数据没有优势,因为hive的执行延迟比较高。

安装hive

mv hive-env.sh.template hive-env.sh
vi hive-env.sh.template hive-env.sh
# Set HADOOP_HOME to point to a specific hadoop install directory
HADOOP_HOME=/opt/modules/hadoop-2.5.0

# Hive Configuration Directory can be controlled by:
export HIVE_CONF_DIR=/opt/modules/hive-0.13.1/conf

vi /etc/profile
export HIVE_HOME=/opt/modules/hive-0.13.1
export PATH=$PATH:$HIVE_HOME/bin
source /etc/profile

在Hadoop目录下进行操作

bin/hdfs dfs -mkdir /tmp
bin/hdfs dfs -mkdir /tmp
bin/hdfs dfs -mkdir -p  /user/hive/warehouse
bin/hdfs dfs -chmod g+x /tmp
bin/hdfs dfs -chmod g+W /user/hive/warehouse

Logging initialized using configuration in jar:file:/opt/modules/hive-0.13.1/lib/hive-common-0.13.1.jar!/hive-log4j.properties
hive>

到此hive的准备工作已经完成,出现上面表示成功。

hive结合mysql存储元数据

mysql安装

rpm -qa|grep mysql
rpm  -e --nodeps mysql-libs-5.1.66-2.el6_3.x86_64(卸载centos自带的版本)
rpm -ivh MySQL-server-5.6.24-1.el6.x86_64.rpm(安装mysql)
rpm -ivh MySQL-client-5.6.24-1.el6.x86_64.rpm(安装mysql客户端)
service mysql start(启动mysql)
cat /root/.mysql_secret(初始安装查看mysql设置的密码)
mysql -u root -p(登录mysql)


 set password=password("missandlove");(修改root用户密码)
 mysql> use mysql;
 select user ,host,password  from user;
 
 mysql> select user ,host,password  from user;
+------+--------------------+-------------------------------------------+
| user | host               | password                                  |
+------+--------------------+-------------------------------------------+
| root | localhost          | *3EF0681227C5B146C25FC6C7C8D0535C637C21DC |
| root | hadoop.jianxin.com | *2BD3CFCD8EAFBFC4CD49B7C7AE7A823791649975 |
| root | 127.0.0.1          | *2BD3CFCD8EAFBFC4CD49B7C7AE7A823791649975 |
| root | ::1                | *2BD3CFCD8EAFBFC4CD49B7C7AE7A823791649975 |
+------+--------------------+-------------------------------------------+
#设置任何一个用户可以连接
update user set host='%' where user='root' and host='localhost';
#把其他用户删除掉
delete from user where host='::1';
...

 flush privileges;(修改了用户权限以后在不重启mysql服务的情况下直接刷新,mysql权限将会起作用)

至此mysql的安装已经完成

hive配置mysql存储元数据

touch hive-site.xml(如果用了远程连接修改xml,一定要先随便打一些东西,防止notepad编辑的时候Windows和Linux编码出现不一致)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
	<property>
		<name>javax.jdo.option.ConnectionURL</name>
		<value>jdbc:mysql://hadoop.jianxin.com: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>missandlove</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>

记住一定要报mysql的连接驱动考入到hive的lib目录下
cp mysql-connector-java-5.1.27-bin.jar /opt/modules/hive-0.13.1/lib/

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| metastore          |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql 中多出了metastore 存储hive的元数据,因此hive可以支持多个客户端进行连接。

hive建表

导入大量数据最方便的方式

create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|';
load data local inpath '/opt/datas/student.txt'into table student 

hive显示函数功能实列

show functions ;
desc function upper ;
desc function extended upper ;


hive (default)> desc function extended upper ;
OK
tab_name
upper(str) - Returns str with all characters changed to uppercase
Synonyms: ucase
Example:
  > SELECT upper('Facebook') FROM src LIMIT 1;
  'FACEBOOK'
Time taken: 0.058 seconds, Fetched: 5 row(s)

hive数据仓库的配置

<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.properties

hive.log.dir=/opt/modules/hive-0.13.1/logs
hive.log.file=hive.log

在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=jianxin
hive (db_hive)> set system:user.name=jianxin ;

这种方式设置属性的值,仅仅在当前会话session生效

hive常用的交互式命令

我这里使用hive是因为设置了软连接

[root@hadoop hadoop-2.5.0]# hive -help
 -d,--define <key=value>          Variable subsitution to apply to hive
                                  commands. e.g. -d A=B or --define A=B
    --database <databasename>     Specify the database to use
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -H,--help                        Print help information
 -h <hostname>                    connecting to Hive Server on remote host
    --hiveconf <property=value>   Use value for given property
    --hivevar <key=value>         Variable subsitution to apply to hive
                                  commands. e.g. --hivevar A=B
 -i <filename>                    Initialization SQL file
 -p <port>                        connecting to Hive Server on port number
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the

交互式快捷查询

hive -e <quoted-query-string>
hive -e "select * from my_hive.student ;"
#my_hive表示数据库
#student表示数据库下面的表

Logging initialized using configuration in file:/opt/modules/hive-0.13.1/conf/hive-log4j.properties
OK
student.id      student.name
Time taken: 2.22 seconds

从客户端文件执行hive查询

bin/hive -f <filename>

[root@hadoop modules]# hive -f hello.sql

Logging initialized using configuration in file:/opt/modules/hive-0.13.1/conf/hive-log4j.properties
OK
student.id      student.name
Time taken: 1.794 seconds

结果保存到文件中

[root@hadoop modules]# hive -f hello.sql  > /opt/modules/hellores.txt 

[root@hadoop modules]# cat hellores.txt 
student.id      student.name

在hive cli命令窗口中如何查看hdfs文件系统

hive (my_hive)> dfs -ls /;
Found 2 items
drwxrwx---   - root supergroup          0 2018-04-09 13:05 /tmp
drwxr-xr-x   - root supergroup          0 2018-04-03 18:05 /user

在hive cli命令窗口中如何查看本地文件系统

hive (default)> !ls /opt/datas ;

猜你喜欢

转载自my.oschina.net/u/3798913/blog/1792004
今日推荐