

1

1 解压到相关目录
tar -zxvf apache-hive-1.2.1-bin.tar.gz -C opt/modules/
2 修改/opt/modules/hive-1.2.1/conf下的hive-env.sh.template 为hive-env.sh
mv hive-env.sh.template hive-env.sh
3 配置hive-env.sh文件
HADOOP_HOME=/opt/modules/hadoop-2.7.2 #修改为hadoop安装目录
export HIVE_CONF_DIR=/opt/modules/hive-1.2.1/conf
4 hadsoop集群配置
必须启动HDFS和yarn
创建目录并赋予同组可写权限
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
5 Hive常见属性配置
1)数据仓库位置配置
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
配置同组用户有可写权限:bin/hdfs dfs -chmod g+w user/hive/warehouse
2)查询后信息显示配置
显示当前数据库及查询表的头部信息配置参数:
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
3)hive运行日志信息配置
mv hive-log4j.properties.template hive-log4j.properties
修改hive-log4j.properties
hive.log.dir=/opt/modules/hive-1.2.1/logs
4)hive参数配置方式
查看当前参数配置:hive (test)> set;
参数配置的三种方式:
a:配置文件方式
hive-default.xml:默认配置文件
hive-site.xml:用户自定义配置文件
b:命令行参数配置,仅对当前hive启动有效
bin/hive -hiveconf mapred.reduce.tasks=10
查看参数配置:hive (test)> set mapred.reduce.tasks;
c:参数声明方式,仅对本次hive启动有效
hive (test)> set mapred.reduce.tasks=100;
查看参数配置:hive (test)> set mapred.reduce.tasks;
6、Hive常用交互命令
1)查看帮助命令
[root@hadoop.localhost131 hive-1.2.1]# bin/hive -help
2)-e:不进入hive窗口执行sql语句
[root@hadoop.localhost131 hive-1.2.1]# bin/hive -e "show databases;"
3)-f:执行脚本中的SQL语句(也可使用重定向工具重定向到文件>>)
[root@hadoop.localhost131 hive-1.2.1]# cat hql.sh
show databases;
[root@hadoop.localhost131 hive-1.2.1]# bin/hive -f hql.sh
7、Hive其它命令操作
1) 在hive窗口中查看HDFS文件系统文件
hive (test)> dfs -ls -R ;
2) 在hive窗口中查看本地文件系统文件
hive (test)> ! ls -R opt/modules
3)查看在hive中输入的所有命令历史
[root@hadoop.localhost131 ~]# cd root/
[root@hadoop.localhost131 ~]# cat .hivehistory
8、Hive基本操作
(1)启动
[root@hadoop.localhost131 hive-1.2.1]# bin/hive
(2)查看数据库
hive>show databases;
(3)打开默认数据库
hive>use default;
(4)显示default数据库中的表
hive>show tables;
(5)创建一张表
hive> create table student(id int, name string);
(6)显示数据库中几张表
hive>show tables;
(7)查看表的结构
hive>desc student;
(8)向表中插入数据
hive> insert into student values(1000,"yanjiaxi");
(9)查询表中数据
hive> select * from student;
(10)退出
hivehive> quit;

使用MySQL存储元数据

1、hive不支持多个客户端窗口启动,否则会产生java.sql.SQLException
2、MySQL安装(二进制,cmake编译安装,rpm安装,yum)。个人喜欢二进制和cmake
查看:rpm -qa | grep mysql
卸载:rpm -e --nodeps ...
解压:tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C opt/modules
重命名:mv mysql-5.7.26-linux-glibc2.12-x86_64/ mysql
建立MySQL用户账号:useradd -s sbin/nologin -M mysql
查看建立的用户:id mysql
创建数据目录,修改MySQL文件所属主及所属组
cd mysql
mkdir mysql_data
chown -R mysql.mysql opt/modules/mysql/
3、初始化数据库
bin/mysqld --initialize --user=mysql --basedir=/opt/modules/mysql --datadir=/opt/modules/mysql_data
4、编辑配置文件
[root@hadoop.localhost131 modules]# cat etc/my.cnf
[mysqld]
user=mysql
basedir=/opt/modules/mysql
datadir=/opt/modules/mysql_data
pid-file=/tmp/mysqld.pid
server_id=6
port=3306
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
prompt=3306 [\\d]>
5、配置环境变量 vim etc/profile,并刷新
#mysql
export PATH=/opt/modules/mysql/bin:$PATH
6、生成启动脚本并启动MySQL
[root@hadoop.localhost131 mysql]# cp support-files/mysql.server etc/init.d/mysqld
[root@hadoop.localhost131 mysql]# chmod +x etc/init.d/mysqld
[root@hadoop.localhost131 mysql]# vim etc/init.d/mysqld
basedir=/opt/modules/mysql //在第46~47行
datadir=/opt/modules/mysql_data
[root@hadoop.localhost131 mysql]# etc/init.d/mysqld start
Starting MySQL. SUCCESS!
7、启动MySQL并修改密码
[root@hadoop.localhost131 mysql]# mysql -uroot -p'初始化时产生的密码'
mysql> set password for root@localhost=password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show variables like 'validate_password%'; //可以看到没有密码复杂度插件
Empty set (0.01 sec)
mysql> exit
Bye
[root@hadoop.localhost131 mysql]# mysql -uroot -proot
mysql>
8、MySQL中user表中主机配置
mysql>show databases;
mysql>use mysql;
mysql>show tables;
mysql>update user set host="%" where host="localhost"; 修改user表,将host表内容修改为%
删除root的其他host
刷新
退出
9、hive元数据配置到MySQL,配置完毕后重启hive和hadoop
1)驱动拷贝
cp mysql-connector-java-5.1.27-bin.jar opt/modules/hive-1.2.1/lib/
2)配置hive-site.xml
[root@hadoop.localhost131 conf]# cat hive-site.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop.localhost131: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>****</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>
至此,hive可实现多客户端窗口启动
至/opt/modules/hive-1.2.1/
bin/hive----启动,在启动之前需先启动HDFS和yarn

◆ 再谈重定向
文章转载自Linux与数据库自学之道,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




