目录


这里写目录标题

  • 一、卸载linuxz自带的mysql
  • 二、安装mysql
  • 三、安装hive
  • 四、Hive元数据配置到MySql
  • 五、启动Hive
  • 六、hive交互命令
  • 七、hiveHive常见属性配置
  • 八、Hive常见数据类型


一、卸载linuxz自带的mysql

1.卸载linux 自带mysql

i. 查询系统自带的mysql

a. Centos6 -> mysql Centos6 ->mariadb )

b. 查找命令:rpm -qa | grep -i -E mysql|mariadb
c.卸载命令:
sudo rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

rpm -qa | grep -i -E mysql|mariadb | xargs -n1 sudo rpm -e --nodeps

二、安装mysql

二、安装mysql

  1. 上传mysql压缩包 至 /opt/module目录下
  2. 解压缩mysql压缩包:
[atdyh @hadoop102 software]$ tar -xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar -C ./mysql_rpms
[atdyh @hadoop102 software]$ sudo rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm

[atdyh @hadoop102 software]$ sudo rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm

[atdyh @hadoop102 software]$ sudo rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm

[atdyh @hadoop102 software]$ sudo rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm

[atdyh @hadoop102 software]$ sudo rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm

hive当前用户 hive当前用户密码_hive当前用户


3. 通过yum安装缺少的依赖,然后重新安装mysql-community-server-5.7.28-1.el7.x86_64 即可

[atdyh @hadoop102 software]$ yum install -y libaio

4.删除/etc/my.cnf文件中datadir指向的目录下的所有内容,如果有内容的情况下:

查看datadir的值:
[mysqld]
datadir=/var/lib/mysql

删除/var/lib/mysql目录下的所有内容:

[atdyh @hadoop102 mysql]# cd /var/lib/mysql
[atdyh @hadoop102 mysql]# sudo rm -rf ./*    //注意执行命令的位置

5.初始化数据库:

[atdyh @hadoop102 opt]$ sudo mysqld --initialize --user=mysql

6.查看临时生成的用户的密码

atdyh @hadoop102 opt]$ sudo cat /var/log/mysqld.log | grep password

hive当前用户 hive当前用户密码_数据库_02


7.启动mysql服务:

[atdyh@hadoop102 mysql_rpms]$ sudo systemctl start mysqld

8.查看你服务是否启动:

[atdyh@hadoop102 mysql_rpms]$ systemctl status mysqld

hive当前用户 hive当前用户密码_hadoop_03

9.登录mysql服务:

[atguigu @hadoop102 opt]$ mysql -uroot -p
Enter password:   输入临时生成的密码

hive当前用户 hive当前用户密码_mysql_04

10.必须先修改root用户的密码,否则执行其他的操作会报错(如上图)

hive当前用户 hive当前用户密码_数据库_05


11.修改mysql库下的user表中的root用户允许任意ip连接:

hive当前用户 hive当前用户密码_hadoop_06

12.查看原始用户和ip:

hive当前用户 hive当前用户密码_数据库_07


13.修改任意ip 去连接MySQL

mysql> update mysql.user set host='%' where user='root';
mysql> flush privileges;

hive当前用户 hive当前用户密码_mysql_08


14.可以使用终端去连接了

三、安装hive

三、安装hive

  1. 把apache-hive-3.1.2-bin.tar.gz上传到linux的/opt/software目录下
  2. hive当前用户 hive当前用户密码_数据库_09

  3. 解压apache-hive-3.1.2-bin.tar.gz到/opt/module/目录下面
[atdyh@hadoop102 software]$ tar -zxvf /opt/software/apache-hive-3.1.2-bin.tar.gz -C  /opt/module/
  1. 修改apache-hive-3.1.2-bin.tar.gz的名称为hive
[atdyh@hadoop102 software]$ mv /opt/module/apache-hive-3.1.2-bin/ /opt/module/hive

4.修改/etc/profile.d/my_env.sh,添加环境变量

[atdyh@hadoop102 software]$ sudo vim /etc/profile.d/my_env.sh

5.添加内容

#HIVE_HOME
HIVE_HOME=/opt/module/hive

PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$HIVE_HOME/bin
export PATH JAVA_HOME HADOOP_HOME HIVE_HOME
  1. 解决日志Jar包冲突
    hive 是基于hadoop,该日志jar会和hadoop有冲突,可以把hive的日志jar修改名字或者删除就可以了
    找到日志jar
[atdyh@hadoop102 lib]$ ll | grep log4j-slf4j-impl-2.10.0.jar 
-rw-rw-r--. 1 atdyh atdyh    24173 4月  15 2020 log4j-slf4j-impl-2.10.0.jar

以上hive安装完成

四、Hive元数据配置到MySql

  1. 配置Metastore到MySql
    在 /opt/module/hive-3.1.2/conf 路径下创建hive-site.xml
[atdyh@hadoop102 conf]$ touch hive-site.xml
[atdyh@hadoop102 conf]$ ll
总用量 332
-rwxr-xr-x. 1 atdyh atdyh   1596 8月  23 2019 beeline-log4j2.properties.template
-rwxr-xr-x. 1 atdyh atdyh 300482 4月  15 2020 hive-default.xml.template
-rwxr-xr-x. 1 atdyh atdyh   2365 8月  23 2019 hive-env.sh.template
-rwxr-xr-x. 1 atdyh atdyh   2274 8月  23 2019 hive-exec-log4j2.properties.template
-rwxr-xr-x. 1 atdyh atdyh   3086 8月  23 2019 hive-log4j2.properties.template
-rw-rw-r--. 1 atdyh atdyh      0 6月   4 14:57 hive-site.xml
-rwxr-xr-x. 1 atdyh atdyh   2060 8月  23 2019 ivysettings.xml
-rwxr-xr-x. 1 atdyh atdyh   3558 8月  23 2019 llap-cli-log4j2.properties.template
-rwxr-xr-x. 1 atdyh atdyh   7163 8月  23 2019 llap-daemon-log4j2.properties.template
-rwxr-xr-x. 1 atdyh atdyh   2662 8月  23 2019 parquet-logging.properties
  1. 在hive-site.xml 文件添加一下内容
[atdyh@hadoop102 conf]$ vim hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
    <!-- jdbc连接的URL -->
    <property>
        <name>javax.jdo.option.ConnectionURL</name>
        <value>jdbc:mysql://hadoop102:3306/metastore?useSSL=false</value>
</property>

    <!-- jdbc连接的Driver-->
    <property>
        <name>javax.jdo.option.ConnectionDriverName</name>
        <value>com.mysql.jdbc.Driver</value>
</property>

	<!-- jdbc连接的username-->
    <property>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>root</value>
    </property>

    <!-- jdbc连接的password -->
    <property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>123456</value>
</property>

    <!-- Hive默认在HDFS的工作目录 -->
    <property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive/warehouse</value>
    </property>
    
   <!-- Hive元数据存储的验证 -->
    <property>
        <name>hive.metastore.schema.verification</name>
        <value>false</value>
    </property>
   
    <!-- 元数据存储授权  -->
    <property>
        <name>hive.metastore.event.db.notification.api.auth</name>
        <value>false</value>
    </property>
</configuration>
  1. 拷贝驱动
    将MySQL的JDBC驱动拷贝到Hive的lib目录下,
    先把使用到的jar上传到/opt/software目录下,然后拷贝到hive 的lib目录下:
[atdyh@hadoop102 software]$ pwd
/opt/software
[atdyh@hadoop102 software]$ pwd
/opt/software
[atdyh@hadoop102 lib]$ cp mysql-connector-java-5.1.37.jar /opt/module/hive-3.1.2/lib/
  1. 初始化元数据库
    登陆MySQL
[atdyh@hadoop102 ~]$ mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

新建Hive元数据库

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

mysql> quit;

初始化Hive元数据库(hive的bin目录下)

[atdyh@hadoop102 bin]$ ./schematool -initSchema -dbType mysql -verbose
成功标识:
0: jdbc:mysql://hadoop102:3306/metastore> !closeall
Closing: 0: jdbc:mysql://hadoop102:3306/metastore?useSSL=false
beeline> 
beeline> Initialization script completed
schemaTool completed

五、启动Hive

1.启动hdfs yarn:

[atdyh@hadoop103 ~]$ start-dfs.sh
[atdyh@hadoop103 ~]$ start-yarn.sh

2.网页查看hdfs yarn 是否启动:

hive当前用户 hive当前用户密码_hadoop_10


hive当前用户 hive当前用户密码_数据库_11

3.启动hive:

[atdyh@hadoop102 bin]$ hive

4.使用hive

hive> show databases;
OK
default
Time taken: 0.223 seconds, Fetched: 1 row(s)
hive>

5.使用JDBC方式访问Hive
在hive-site.xml文件中添加如下配置信息

[atdyh@hadoop102 conf]$ pwd
/opt/module/hive-3.1.2/conf
[atdyh@hadoop102 conf]$ vim hive-site.xml 


    <!-- 指定hiveserver2连接的host -->
    <property>
        <name>hive.server2.thrift.bind.host</name>
        <value>hadoop102</value>
    </property>

    <!-- 指定hiveserver2连接的端口号 -->
    <property>
        <name>hive.server2.thrift.port</name>
        <value>10000</value>
    </property>

启动hiveserver2

[atdyh@hadoop102 hive-3.1.2]$ bin/hive --service hiveserver2
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive-3.1.2/bin:/home/atdyh/.local/bin:/home/atdyh/bin)
2022-06-05 00:27:34: Starting HiveServer2
Hive Session ID = a527b465-75fe-4c5d-917a-2258c95d62f0
Hive Session ID = 555138a5-836f-495c-abb5-3c0f9406d38c
Hive Session ID = 04e5e2d8-2e5a-4d54-8dcb-084888ed0a70
Hive Session ID = 52eeda8b-035f-44eb-a27f-884a3bf2e0fa

启动beeline客户端(需要多等待一会)

[atdyh@hadoop102 hive-3.1.2]$ bin/beeline -u jdbc:hive2://hadoop102:10000 -n atdyh
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>
  1. 使用元数据服务的方式访问Hive
    在hive-site.xml文件中添加如下配置信息
[atdyh@hadoop102 conf]$ pwd
/opt/module/hive-3.1.2/conf
[atdyh@hadoop102 conf]$ vim hive-site.xml 
    <!-- 指定存储元数据要连接的地址 -->
    <property>
        <name>hive.metastore.uris</name>
        <value>thrift://hadoop102:9083</value>
    </property>
  启动hive之前需要启动元数据服务,否则会报下面的问题
  [atdyh@hadoop102 conf]$ hive
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive-3.1.2/bin:/home/atdyh/.local/bin:/home/atdyh/bin)
Hive Session ID = e4ec4906-bde1-4df6-a351-dcd7b5fad0db

Logging initialized using configuration in jar:file:/opt/module/hive-3.1.2/lib/hive-common-3.1.2.jar!/hive-log4j2.properties Async: true
shoe^H^HHive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
hive> show databases;
FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
hive>

先启动元数据服务,在启动hive

[atdyh@hadoop102 conf]$ hive --service metastore
2022-06-05 15:24:47: Starting Hive Metastore Server

[atdyh@hadoop102 ~]$ hive
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive-3.1.2/bin:/home/atdyh/.local/bin:/home/atdyh/bin)
Hive Session ID = 032c9338-65e8-4cbe-adff-1ea0bcfee0ec

Logging initialized using configuration in jar:file:/opt/module/hive-3.1.2/lib/hive-common-3.1.2.jar!/hive-log4j2.properties Async: true
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Hive Session ID = 574218b8-e6f4-43a3-8779-32970a076bf4
hive> show databases;
OK
default
Time taken: 0.596 seconds, Fetched: 1 row(s)
hive>
  1. 启动metastore和hiveserver2脚本
[atdyh@hadoop202 hive]$ nohup hive --service metastore 2>&1 &
[atdyh@hadoop202 hive]$ nohup hive --service hiveserver2 2>&1 &

创建hiveservices.sh

#!/bin/bash
HIVE_LOG_DIR=$HIVE_HOME/logs
if [ ! -d $HIVE_LOG_DIR ]
then
	mkdir -p $HIVE_LOG_DIR
fi
#检查进程是否运行正常,参数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" sleep 4; 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

六、hive交互命令

1.hive交互命令

[atdyh@hadoop102 bin]$ hive -help
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive-3.1.2/bin:/home/atdyh/.local/bin:/home/atdyh/bin)
Hive Session ID = 6cc756f4-bdde-44f6-89e6-04631596ad8c
usage: hive
 -d,--define <key=value>          Variable substitution 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 substitution 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语句
[atdyh@hadoop102 bin]$ hive -e "select * from dyhtest.mytbl;"
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive-3.1.2/bin:/home/atdyh/.local/bin:/home/atdyh/bin)
Hive Session ID = 1dcb713a-de84-4e0e-a333-f5a2004aebc3

Logging initialized using configuration in jar:file:/opt/module/hive-3.1.2/lib/hive-common-3.1.2.jar!/hive-log4j2.properties Async: true
Hive Session ID = d4657922-9263-4c02-a38b-e375ad80a6f0
OK
1	zhangsan
Time taken: 2.882 seconds, Fetched: 1 row(s)
  1. “-f”执行脚本中sql语句
[atdyh@hadoop102 hive-3.1.2]$ mkdir datas
[atdyh@hadoop102 hive-3.1.2]$ ll
总用量 84
drwxrwxr-x. 3 atdyh atdyh  4096 6月   5 15:51 bin
drwxrwxr-x. 2 atdyh atdyh  4096 6月   5 15:18 conf
drwxrwxr-x. 2 atdyh atdyh  4096 6月   5 16:09 datas
drwxrwxr-x. 4 atdyh atdyh  4096 6月   4 14:29 examples
drwxrwxr-x. 7 atdyh atdyh  4096 6月   4 14:29 hcatalog
drwxrwxr-x. 2 atdyh atdyh  4096 6月   4 14:29 jdbc
drwxrwxr-x. 4 atdyh atdyh 20480 6月   4 15:29 lib
-rwxr-xr-x. 1 atdyh atdyh 20798 8月  23 2019 LICENSE
drwxrwxr-x. 2 atdyh atdyh  4096 6月   5 15:35 logs
-rwxr-xr-x. 1 atdyh atdyh   230 8月  23 2019 NOTICE
-rwxr-xr-x. 1 atdyh atdyh  2469 8月  23 2019 RELEASE_NOTES.txt
drwxrwxr-x. 4 atdyh atdyh  4096 6月   4 14:29 scripts
[atdyh@hadoop102 hive-3.1.2]$ cd datas/
[atdyh@hadoop102 datas]$ touch test.sql
[atdyh@hadoop102 datas]$ vim test.sql 
[atdyh@hadoop102 datas]$ hive -f test.sql 
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive-3.1.2/bin:/home/atdyh/.local/bin:/home/atdyh/bin)
Hive Session ID = be258fa7-1b3b-4f05-8923-f800ac01f2f2

Logging initialized using configuration in jar:file:/opt/module/hive-3.1.2/lib/hive-common-3.1.2.jar!/hive-log4j2.properties Async: true
Hive Session ID = 3728ab1e-0101-4d07-98c7-2008799a50e5
OK
1	zhangsan
Time taken: 2.619 seconds, Fetched: 1 row(s)

4.退出hive窗口:

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

注意:
在新版的hive中没区别了,在以前的版本是有的:
exit:先隐性提交数据,再退出;
quit:不提交数据,退出;

七、hiveHive常见属性配置

  1. hive窗口打印默认库和表头
[atdyh@hadoop102 conf]$ pwd
/opt/module/hive-3.1.2/conf
[atdyh@hadoop102 conf]$ vim 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>
  1. Hive运行日志信息配置
    未配置之前hive的日志目录:
[atdyh@hadoop102 atdyh]$ clear
[atdyh@hadoop102 atdyh]$ pwd
/tmp/atdyh
[atdyh@hadoop102 atdyh]$ ll
总用量 452
drwx------. 2 atdyh atdyh   4096 6月   5 16:20 308df92b-727b-4c53-bfcf-300140c1cf69
-rw-rw-r--. 1 atdyh atdyh      0 6月   5 16:20 308df92b-727b-4c53-bfcf-300140c1cf694373338308380121283.pipeout
-rw-rw-r--. 1 atdyh atdyh      0 6月   5 16:20 308df92b-727b-4c53-bfcf-300140c1cf698356273636680731957.pipeout
drwx------. 2 atdyh atdyh   4096 6月   5 16:20 ce3b5885-c604-45fd-8c5a-c2a04fe39bd6
-rw-rw-r--. 1 atdyh atdyh      0 6月   5 16:20 ce3b5885-c604-45fd-8c5a-c2a04fe39bd62216943123832906843.pipeout
-rw-rw-r--. 1 atdyh atdyh      0 6月   5 16:20 ce3b5885-c604-45fd-8c5a-c2a04fe39bd63482319897594389948.pipeout
-rw-rw-r--. 1 atdyh atdyh 429245 6月   5 16:21 hive.log
-rw-rw-r--. 1 atdyh atdyh  17153 6月   4 23:51 hive.log.2022-06-04

修改配置:

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

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

property.hive.log.dir=/opt/module/hive-3.1.2/logs
  1. 参数配置方式
    (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相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。

八、Hive常见数据类型

1.常见的数据类型

hive当前用户 hive当前用户密码_数据库_12


对于Hive的String类型相当于数据库的varchar类型,该类型是一个可变的字符串,不过它不能声明其中最多能存储多少个字符,理论上它可以存储2GB的字符数。

  1. 复杂数据类型

    Hive有三种复杂数据类型ARRAY、MAP 和 STRUCT。ARRAY和MAP与Java中的Array和Map类似,而STRUCT与C语言中的Struct类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。

例子:
{
“name”: “songsong”,
“friends”: [“bingbing” , “lili”] , //列表Array,
“children”: { //键值Map,
“xiao song”: 19 ,
“xiaoxiao song”: 18
}
“address”: { //结构Struct,
“street”: “hui long guan” ,
“city”: “beijing”
}
}
建表

hive (dyhtest)> create table test2(
              > name string,
              > friends array<string>,
              > children map<string, int>,
              > address struct<street:string, city:string>
              > );
OK
Time taken: 0.132 seconds

在hdfs上查看表

hive当前用户 hive当前用户密码_mysql_13


hive当前用户 hive当前用户密码_mysql_14

字段解释:
row format delimited fields terminated by ‘,’ – 列分隔符

collection items terminated by ‘_’ --MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)

map keys terminated by ‘:’ – MAP中的key与value的分隔符

lines terminated by ‘\n’; – 行分隔符

插入数据, 一般使用load方式加载数据
load data local inpath ‘数据文件存放的路径/文件’ into table 库名.表名 ;

hive (dyhtest)> load data local inpath '/opt/module/hive-3.1.2/datas/person.txt' into table test; 
Loading data to table dyhtest.test
OK
Time taken: 0.264 seconds
hive (dyhtest)>

查看数据是否插入成功,查询一下表核对一下数据:

hive (dyhtest)> select * from test;
OK
test.name	test.friends	test.children	test.address
songsong	["bingbing","lili"]	{"xiao song":18,"xiaoxiao song":19}	{"street":"hui long guan","city":"beijing"}
yangyang	["caicai","susu"]	{"xiao yang":18,"xiaoxiao yang":19}	{"street":"chao yang","city":"beijing"}
Time taken: 0.206 seconds, Fetched: 2 row(s)
hive (dyhtest)>

查看单个字段

hive (dyhtest)> select name, friends[0],address.city from test;
OK
name	_c1	city
songsong	bingbing	beijing
yangyang	caicai	beijing
Time taken: 0.511 seconds, Fetched: 2 row(s)

注意:
1.数据结构的数据要指定下标
2.map结构数据要给定一个key
3.结构体类型和java对象类似,对象.属性方式可获取

  1. 类型转换
    a.Hive的原子数据类型是可以进行隐式转换的,类似于Java的类型转换,
    例如某表达式使用INT类型,TINYINT会自动转换为INT类型
    b.Hive不会进行反向转化,例如,某表达式使用TINYINT类型,INT不会自动转换为TINYINT类型,它会返回错误,除非使用CAST操作。
    隐式类型转换规则如下:
    i.任何整数类型都可以隐式地转换为一个范围更广的类型,如TINYINT可以转换 成INT,INT可以转换成BIGINT。
    ii.所有整数类型、FLOAT和STRING类型都可以隐式地转换成DOUBLE。
    iii.TINYINT、SMALLINT、INT都可以转换为FLOAT。
    vi.BOOLEAN类型不可以转换为任何其它的类型。
    v.可以使用CAST操作显示进行数据类型转换
    例如CAST(‘1’ AS INT)将把字符串’1’ 转换成整数1;如果强制类型转换失败,如执行CAST(‘X’ AS INT),表达式返回空值 NULL。
hive (dyhtest)>  select '1' +1;
OK
_c0
2.0
Time taken: 0.146 seconds, Fetched: 1 row(s)
hive (dyhtest)>  select cast('1'as int) +1;
OK
_c0
2
Time taken: 0.171 seconds, Fetched: 1 row(s)
------转不了的返回null
hive (dyhtest)>  select cast('a'as int);
OK
_c0
NULL
Time taken: 0.142 seconds, Fetched: 1 row(s)