Hive结合MySQL的配置及操作

1、安装MySQL
(1)解压mysql安装包:$ unzip mysql-libs.zip
(2)切换到root用户,查询系统中是否已安装mysql:# rpm -qa | grep mysql
         如果已安装,删除已安装的mysql文件:# rpm -e --nodeps mysql-libs-5.1.66-2.el6_3.x86_64
(3)进入解压好的mysql-libs目录,对root用户赋予执行权限:mysql-libs]# chmod u+x ./*
(4)安装mysql server端:# rpm -ivh MySQL-server-5.6.24-1.el6.x86_64.rpm
         输出显示随机密码:2DcKdnr12JtazfAA
         该随机密码也可以在/root/.mysql_secret文件中查看:sudo cat /root/.mysql_secret
(5)启动mysql服务:# service mysql start
         查看mysql服务状态:# service mysql status
(6)安装mysql client端:# rpm -ivh MySQL-client-5.6.24-1.el6.x86_64.rpm
(7)root用户随机密码登陆mysql:# mysql -uroot -p2DcKdnr12JtazfAA
         修改root用户密码:mysql> set PASSWORD=PASSWORD('123456');
         root用户新密码登陆:# mysql -uroot -p123456
(8)mysql操作:
mysql> show databases;
mysql> use mysql;
mysql> show tables;
mysql> select User,Host,Password from user;
mysql> update user set Host='%' where User='root' and Host='localhost';
mysql> delete from user where User='root' and Host='hadoop-senior.ibeifeng.com';
mysql> delete from user where User='root' and Host='127.0.0.1';
mysql> delete from user where User='root' and Host='::1';
mysql> flush privileges; 刷新信息
(9)yum方式安装MySQL
yum安装命令:yum install mysql-server
判断mysql是否已经安装好:chkconfig --list | grep mysql
启动mysql服务:service mysqld start或者/etc/init.d/mysqld start
检查是否启动mysql服务:/etc/init.d/mysqld status
设置mysql开机启动:chkconfig mysqld on
检查设置mysql开机启动是否配置成功:显示2 3 4 5为on:chkconfig --list | grep mysql
创建root管理员:mysqladmin -uroot password root
登录mysql:mysql -uroot -proot
2、Hive关联MySQL
(1)拷贝mysql驱动jar包,到Hive安装目录的lib下。
$ cp mysql-connector-java-5.1.27-bin.jar /opt/modules/hive-0.13.1/lib/
(2)进入目录/opt/modules/hive-0.13.1/conf,创建配置文件hive-site.xml。
(3)在配置文件hive-site.xml中添加如下配置。

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>

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

</configuration>

注意:配置的hive metastore、Mysql与我们的hive安装在同一台机器上。
(3)克隆会话。
会话1:
mysql> show databases;
显示有四张表:
+——————–+
| Database |
+——————–+
| information_schema |
| metastore |
| performance_schema |
| test |
+——————–+
会话2:
hive-0.13.1]$ bin/hive
会话1:
mysql> show databases;
出现数据库metastore,该数据库即是用来存放hive的元数据的数据库。
+——————–+
| Database |
+——————–+
| information_schema |
| metastore |
| mysql |
| performance_schema |
| test |
+——————–+
mysql> use metastore;
3、Hive基本操作
$ bin/hive
hive> show databases;
hive> create database db_hive;
hive> use db_hive;
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
hive> show tables;
hive> desc student;
hive> desc extended student;
hive> desc formatted student;
hive> load data local inpath '/opt/datas/student.txt' into table db_hive.student;
hive> show functions;
hive> desc function upper;
hive> desc function extended upper;
hive> select id,upper(name) uname from db_hive.student;
hive> exit;
注意:MySQL的varchar主键只支持不超过768个字节,或者768/2=384个双字节,或768/3=256个三字节的字段,而GBK是双字节的,UTF-8是三字节的。
4、Hive属性配置
(1)删除数据库。
配置完MySQL,数据库存储的位置变成MySQL,而不是默认的Derby,可以将文件系统HDFS上Derby的数据库删掉,Hive可以直接操作文件系统。Hive在HDFS上的路径为/user/hive/warehouse。
hive (default)> dfs -rm -R /user/hive/warehouse/student;
(2)Hive数据仓库位置配置
默认位置:/user/hive/warehouse
注意:在仓库目录下,没有对默认的数据库default创建文件夹。如果某张表属于default数据库,直接在数据仓库目录下创建一个以该表名为名字的文件夹。
修改数据仓库位置,配置文件:hive-site.xml。

    <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

(3)Hive运行日志信息
<1>进入目录/opt/modules/hive-0.13.1/conf,执行如下命令。
cp hive-log4j.properties.template hive-log4j.properties
<2>修改配置文件hive-log4j.properties。

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

<3>重新进入hive-0.13.1]$ bin/hive,/opt/modules/hive-0.13.1/logs目录下便出现了hive.log。
(4)指定hive运行时显示的log日志的级别,配置文件:$HIVE_HOME/conf/hive-log4j.properties。

        hive.root.logger=INFO,DRFA

(5)在cli命令行上显示当前数据库,以及查询表的行头信息,配置文件:$HIVE_HOME/conf/hive-site.xml。

    <property>
        <name>hive.cli.print.header</name>
        <value>true</value>
    </property>
    <property>
        <name>hive.cli.print.current.db</name>
        <value>true</value>
    </property>

即可显示正在使用的数据库名和表的字段信息,如下。
$ bin/hive
hive (default)> show databases;
hive (default)> use db_hive;
hive (db_hive)> select * from student;
OK
student.id student.name
1001 zhangsan
1002 lisi
1003 wangwu
Time taken: 0.852 seconds, Fetched: 3 row(s)
(6)在启动hive时设置配置属性信息。
$ bin/hive --hiveconf <property=value>
查看当前所有的配置信息
hive > set ;
hive (db_hive)> set system:user.name ;
system:user.name=beifeng
hive (db_hive)> set system:user.name=beifeng ;

此种方式,设置属性的值,仅仅在当前会话session中生效。
5、Hive Shell常用操作
[beifeng@hadoop-senior hive-0.13.1]$ bin/hive -help

usage: hive
 -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                                  console)

bin/hive -e <quoted-query-string>
例:bin/hive -e "select * from db_hive.student ;"
bin/hive -f <filename>
例:$ touch hivef.sql
select * from db_hive.student ;
$ bin/hive -f /opt/datas/hivef.sql
$ bin/hive -f /opt/datas/hivef.sql > /opt/datas/hivef-res.txt

bin/hive -i <filename>用于初始化,与用户自定义文件udf相互使用
在hive cli命令窗口中查看hdfs文件系统:hive (default)> dfs -ls / ;
在hive cli命令窗口中查看本地文件系统:hive (default)> !ls /opt/datas ;
查看操作历史命令:$HOME/.hivehistory

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_72431373

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值