Hive基础(三):使用基础(3)Hive 安装(二) (centos)

6 启动Hive

6.1 初始化元数据库

1)登陆MySQL

[atguigu@hadoop102 software]$ mysql -uroot -p000000

2)新建Hive元数据库

mysql> create database metastore;

mysql> quit;

3)初始化Hive元数据库

[atguigu@hadoop102 software]$ schematool -initSchema -dbType mysql -verbose

2.6.2 启动metastorehiveserver2

1Hive 2.x以上版本要先启动这两个服务否则会报错

 

FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient

 

2)编写hive服务启动脚本

[atguigu@hadoop102 software]$ vim $HIVE_HOME/bin/hiveservices.sh

内容如下

 

#!/bin/bash
HIVE_LOG_DIR=$HIVE_HOME/logs

mkdir -p $HIVE_LOG_DIR

#检查进程是否运行正常,参数1为进程名,参数2为进程端口
function check_process()
{
    pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
    ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
    echo $pid
    [[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}

function hive_start()
{
    metapid=$(check_process HiveMetastore 9083)
    cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
    cmd=$cmd" sleep4; hdfs dfsadmin -safemode wait >/dev/null 2>&1"
    [ -z "$metapid" ] && eval $cmd || echo "Metastroe服务已启动"
    server2pid=$(check_process HiveServer2 10000)
    cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
    [ -z "$server2pid" ] && eval $cmd || echo "HiveServer2服务已启动"
}

function hive_stop()
{
    metapid=$(check_process HiveMetastore 9083)
    [ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
    server2pid=$(check_process HiveServer2 10000)
    [ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}

case $1 in
"start")
    hive_start
    ;;
"stop")
    hive_stop
    ;;
"restart")
    hive_stop
    sleep 2
    hive_start
    ;;
"status")
    check_process HiveMetastore 9083 >/dev/null && echo "Metastore服务运行正常" || echo "Metastore服务运行异常"
    check_process HiveServer2 10000 >/dev/null && echo "HiveServer2服务运行正常" || echo "HiveServer2服务运行异常"
    ;;
*)
    echo Invalid Args!
    echo 'Usage: '$(basename $0)' start|stop|restart|status'
    ;;
esac

 

3)添加执行权限

[atguigu@hadoop102 software]$ chmod +x $HIVE_HOME/bin/hiveservices.sh

4)启动Hive后台服务

[atguigu@hadoop102 software]$ hiveservices.sh start

6.3 HiveJDBC访问

1)启动beeline客户端

 

[atguigu@hadoop102 software]$ beeline -u jdbc:hive2://hadoop102:10000 -n atguigu

 

2)看到如下界面

Connecting to jdbc:hive2://hadoop102:10000
Connected to: Apache Hive (version 3.1.2)
Driver: Hive JDBC (version 3.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 3.1.2 by Apache Hive
0: jdbc:hive2://hadoop102:10000>

7 Hive常用交互命令

 

[atguigu@hadoop102 hive]$ 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
    --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
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the console)

 

1)“-e”不进入hive的交互窗口执行sql语句

[atguigu@hadoop102 hive]$ bin/hive -e "select id from student;"

2)“-f”执行脚本中sql语句

1/opt/module/datas目录下创建hivef.sql文件

[atguigu@hadoop102 datas]$ touch hivef.sql

2文件中写入正确的sql语句

select *from student;

3执行文件中的sql语句

[atguigu@hadoop102 hive]$ bin/hive -f /opt/module/datas/hivef.sql

4执行文件中的sql语句并将结果写入文件中

[atguigu@hadoop102 hive]$ bin/hive -f /opt/module/datas/hivef.sql  > /opt/module/datas/hive_result.txt

8 Hive其他命令操作

1)退出hive窗口:

 

 

 

hive(default)>exit;
hive(default)>quit;

在新版的hive中没区别了,在以前的版本是有的:

exit:先隐性提交数据,再退出;

quit:不提交数据,退出;

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

hive(default)>dfs -ls /;

3)查看在hive中输入的所有历史命令

1进入到当前用户的根目录/root/home/atguigu

2查看. hivehistory文件

[atguigu@hadoop102 ~]$ cat .hivehistory

9 Hive常见属性配置

9.1 Hive运行日志信息配置

1Hivelog默认存放在/tmp/atguigu/hive.log目录下(当前用户名下)

2修改hivelog存放日志到/opt/module/hive/logs

1修改/opt/module/hive/conf/hive-log4j.properties.template文件名称为

hive-log4j.properties

[atguigu@hadoop102 conf]$ pwd
/opt/module/hive/conf
[atguigu@hadoop102 conf]$ mv hive-log4j.properties.template hive-log4j.properties

2hive-log4j.properties文件中修改log存放位置

hive.log.dir=/opt/module/hive/logs

9.2 参数配置方式

1查看当前所有的配置信息

hive>set;

2参数的配置三种方式

1配置文件方式

默认配置文件hive-default.xml

用户自定义配置文件hive-site.xml

注意:用户自定义配置会覆盖默认配置。另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的Hive的配置会覆盖Hadoop的配置。配置文件的设定对本机启动的所有Hive进程都有效。

2)命令行参数方式

启动Hive时,可以在命令行添加-hiveconf param=value来设定参数。

例如:

 

[atguigu@hadoop103 hive]$ bin/hive -hiveconf mapred.reduce.tasks=10;

 

注意:仅对本次hive启动有效

 

查看参数设置:

 

hive (default)> set mapred.reduce.tasks;

 

3)参数声明方式

 

可以在HQL中使用SET关键字设定参数

 

例如

 

hive (default)> set mapred.reduce.tasks=100;

 

注意:仅对本次hive启动有效。

 

查看参数设置

 

hive (default)> set mapred.reduce.tasks;

 

上述三种设定方式的优先级依次递增。即配置文件<命令行参数<参数声明。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在会话

 

建立以前已经完成了。

 

posted @ 2020-07-22 15:10  秋华  阅读(316)  评论(0编辑  收藏  举报