Hadoop生态圈-Hive快速入门篇之HQL的基础语法

                                     作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

  本篇博客的重点是介绍Hive中常见的数据类型,DDL数据定义,DML数据操作以及常用的查询操作。如果你没有hive的安装环境的话,可以参考我之前分析搭建hive的笔记:https://www.cnblogs.com/yinzhengjie/p/9154324.html

 

一.Hive常见的属性配置

1>.Hive数据仓库位置配置

1>.Default数据仓库的最原始位置在“hdfs:/user/hive/warehouse/ ”路径下
2>.在仓库目录下,没有对默认的数据库default的创建文件夹(也就是说,如果有表属于default数据库,那么默认会存放在根路径下)。如果某张表属于default数据库,直接在数据仓库目录下创建一个文件夹
3>.修改default数据仓库原始位置(将默认配置文件“hive-defalut.xml.template”如下配置信息拷贝到hive-site.xml文件中
      <property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive/warehouse</value>
        <description>location of default database for the warehouse</description>
      </property>

2>.配置当前数据库,以及查询表的头信息配置

在hive-site.xml文件中添加如下配置信息,即可以实现显示当前数据库,以及查询表的头信息配置。配置之后需要重启hive客户端
  <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客户端,你会发现多了两个功能,可以查看表头以及当前所在的数据库:

Hadoop生态圈-Hive快速入门篇之HQL的基础语法

3>.Hive运行日志信息配置

1>.Hive的log默认存放在"/tmp/atguigu/hive.log"目录下(当前用户名下)。

2>.修改hive的log存放日志到"/home/yinzhengjie/hive/logs",我们可以修改hive-log4j2.properties进行配置,具体操作如下:
[yinzhengjie@s101 ~]$ cd /soft/hive/conf/
[yinzhengjie@s101 conf]$ 
[yinzhengjie@s101 conf]$ cp hive-log4j2.properties.template hive-log4j2.properties                        #拷贝模板文件生成配置文件
[yinzhengjie@s101 conf]$ grep property.hive.log.dir  hive-log4j2.properties  | grep -v ^#                
property.hive.log.dir = /home/yinzhengjie/hive/logs                                                        #指定log的存放位置
[yinzhengjie@s101 conf]$ 
[yinzhengjie@s101 conf]$ ll /home/yinzhengjie/hive/logs/hive.log 
-rw-rw-r-- 1 yinzhengjie yinzhengjie 4265 Aug  5 21:20 /home/yinzhengjie/hive/logs/hive.log                #重启hive,查看日志文件中的内容
[yinzhengjie@s101 conf]$ 

4>.查看参数配置方式

1>.查看当前的所有配置信息(hive (yinzhengjie)> set;)
    配置文件方式:
        默认配置文件:            hive-default.xml
        用户自定义配置文件:    hive-site.xml
        注意:用户自定义配置会覆盖默认配置。另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启用的,Hive的配置会覆盖Hadoop的配置。配置文件的设定对本机启动的所有Hive进程都有效。

        
2>.参数的配置三种方式以及优先级介绍
    启动命令行时声明参数方式:
        [yinzhengjie@s101 ~]$ hive -hiveconf mapred.reduce.tasks=10
        SLF4J: Class path contains multiple SLF4J bindings.
        SLF4J: Found binding in [jar:file:/soft/apache-hive-2.1.1-bin/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: Found binding in [jar:file:/soft/hbase-1.2.6/lib/phoenix-4.10.0-HBase-1.2-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: Found binding in [jar:file:/soft/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

        Logging initialized using configuration in file:/soft/apache-hive-2.1.1-bin/conf/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 (default)> set mapred.reduce.tasks;
        mapred.reduce.tasks=10
        hive (default)> quit;
        [yinzhengjie@s101 ~]$ 
        [yinzhengjie@s101 ~]$ hive
        SLF4J: Class path contains multiple SLF4J bindings.
        SLF4J: Found binding in [jar:file:/soft/apache-hive-2.1.1-bin/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: Found binding in [jar:file:/soft/hbase-1.2.6/lib/phoenix-4.10.0-HBase-1.2-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: Found binding in [jar:file:/soft/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

        Logging initialized using configuration in file:/soft/apache-hive-2.1.1-bin/conf/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 (default)> set mapred.reduce.tasks;
        mapred.reduce.tasks=-1
        hive (default)> exit;
        [yinzhengjie@s101 ~]$ 
    
    启动命令行后参数声明方式:
        [yinzhengjie@s101 ~]$ hive
        SLF4J: Class path contains multiple SLF4J bindings.
        SLF4J: Found binding in [jar:file:/soft/apache-hive-2.1.1-bin/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: Found binding in [jar:file:/soft/hbase-1.2.6/lib/phoenix-4.10.0-HBase-1.2-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: Found binding in [jar:file:/soft/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
        SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

        Logging initialized using configuration in file:/soft/apache-hive-2.1.1-bin/conf/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 (default)> set mapred.reduce.tasks;
        mapred.reduce.tasks=-1
        hive (default)> set mapred.reduce.tasks=100;
        hive (default)> set mapred.reduce.tasks;
        mapred.reduce.tasks=100
        hive (default)> quit;
        [yinzhengjie@s101 ~]$ 
    
    三种方式优先级温馨提示:
        以上三种设定方式的优先级依次递增。即"配置文件"<"启动命令行时"<"启动命令行后"。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。

 

二.Hive数据类型

1>.基本数据类型

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

Hive数据类型

Java数据类型

长度

例子

TINYINT

byte

1byte有符号整数

20

SMALINT

short

2byte有符号整数

20

INT

int

4byte有符号整数

20

BIGINT

long

8byte有符号整数

20

BOOLEAN

boolean

布尔类型,true或者false

TRUE  FALSE

FLOAT

float

单精度浮点数

3.14159

DOUBLE

double

双精度浮点数

3.14159

STRING

string

字符系列。可以指定字符集。可以使用单引号或者双引号。

‘now is the time’ “for all good men”

TIMESTAMP

 

时间类型

 

BINARY

 

字节数组

 

2>.集合数据类型

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

数据类型

描述

语法示例

STRUCT

和c语言中的struct类似,都可以通过“点”符号访问元素内容。例如,如果某个列的数据类型是STRUCT{first STRING, last STRING},那么第1个元素可以通过字段.first来引用。

struct()

MAP

MAP是一组键-值对元组集合,使用数组表示法可以访问数据。例如,如果某个列的数据类型是MAP,其中键->值对是’first’->’John’和’last’->’Doe’,那么可以通过字段名[‘last’]获取最后一个元素

map()

ARRAY

数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。例如,数组值为[‘John’, ‘Doe’],那么第2个元素可以通过数组名[1]进行引用。

Array()

3>类型转化

   Hive的原子数据类型是可以进行隐式转换的,类似于Java的类型转换,例如某表达式使用INT类型,TINYINT会自动转换为INT类型,但是Hive不会进行反向转化,例如,某表达式使用TINYINT类型,INT不会自动转换为TINYINT类型,它会返回错误,除非使用CAST操作。隐式类型转换规则如下。

    第一:任何整数类型都可以隐式地转换为一个范围更广的类型,如TINYINT可以转换成INT,INT可以转换成BIGINT。

    第二:所有整数类型、FLOAT和STRING类型都可以隐式地转换成DOUBLE。

    第三:TINYINT、SMALLINT、INT都可以转换为FLOAT。

    第四:BOOLEAN类型不可以转换为任何其它的类型。

  温馨提示:可以使用CAST操作显示进行数据类型转换,例如CAST('1' AS INT)将把字符串'1' 转换成整数1;如果强制类型转换失败,如执行CAST('X' AS INT),表达式返回空值 NULL。

4>.小试牛刀

  假设某表有如下一行,我们用JSON格式来表示其数据结构。在Hive下访问的格式为:

Hadoop生态圈-Hive快速入门篇之HQL的基础语法

  基于上述数据结构,我们在Hive里创建对应的表,并导入数据。创建本地测试文件test.txt内容如下:(注意,MAP,STRUCT和ARRAY里的元素间关系都可以用同一个字符表示,这里用“_”。

[yinzhengjie@s101 download]$ cat /home/yinzhengjie/download/test.txt 
漩涡鸣人,我爱罗_佐助,漩涡博人:18_漩涡向日葵:16,一乐拉面附近_木业忍者村
宇智波富岳,宇智波美琴_志村团藏,宇智波鼬:28_宇智波佐助:19,木叶警务部_木业忍者村
[yinzhengjie@s101 download]$ 

  Hive上创建测试表test,如下:

create table test(
    name string,
    friends array<string>,
    children map<string, int>,
    address struct<street:string, city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';

Hadoop生态圈-Hive快速入门篇之HQL的基础语法

  导入文本数据到测试表:

hive (yinzhengjie)>  load data local inpath '/home/yinzhengjie/download/test.txt' into table test;
Loading data to table yinzhengjie.test
OK
Time taken: 0.335 seconds
hive (yinzhengjie)> select * from test;
OK
test.name    test.friends    test.children    test.address
漩涡鸣人    ["我爱罗","佐助"]    {"漩涡博人":18,"漩涡向日葵":16}    {"street":"一乐拉面附近","city":"木业忍者村"}
宇智波富岳    ["宇智波美琴","志村团藏"]    {"宇智波鼬":28,"宇智波佐助":19}    {"street":"木叶警务部","city":"木业忍者村"}
Time taken: 0.099 seconds, Fetched: 2 row(s)
hive (yinzhengjie)> 

  访问三种集合列里的数据,以下分别是ARRAY,MAP,STRUCT的访问方式:

hive (yinzhengjie)> select * from test;
OK
test.name    test.friends    test.children    test.address
漩涡鸣人    ["我爱罗","佐助"]    {"漩涡博人":18,"漩涡向日葵":16}    {"street":"一乐拉面附近","city":"木业忍者村"}
宇智波富岳    ["宇智波美琴","志村团藏"]    {"宇智波鼬":28,"宇智波佐助":19}    {"street":"木叶警务部","city":"木业忍者村"}
Time taken: 0.085 seconds, Fetched: 2 row(s)
hive (yinzhengjie)> select friends[0],children['漩涡博人'],address.city from test where name="漩涡鸣人";
OK
_c0    _c1    city
我爱罗    18    木业忍者村
Time taken: 0.096 seconds, Fetched: 1 row(s)
hive (yinzhengjie)> select friends[1],children['漩涡向日葵'],address.city from test where name="漩涡鸣人";
OK
_c0    _c1    city
佐助    16    木业忍者村
Time taken: 0.1 seconds, Fetched: 1 row(s)
hive (yinzhengjie)> 

 

三.Hive的常用命令(HQL)用法展示

  温馨提示:在使用Hive交互命令或是执行HQL语句时都会启动Hive,而hive依赖于Hadoop的hdfs提供存储和MapReduce提供计算,因此在启动Hive之前,需要启动Hadoop集群哟。

[yinzhengjie@s101 ~]$ more `which xcall.sh`
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com


#判断用户是否传参
if [ $# -lt 1 ];then
        echo "请输入参数"
        exit
fi

#获取用户输入的命令
cmd=$@

for (( i=101;i<=105;i++ ))
do
        #使终端变绿色 
        tput setaf 2
        echo ============= s$i $cmd ============
        #使终端变回原来的颜色,即白灰色
        tput setaf 7
        #远程执行命令
        ssh s$i $cmd
        #判断命令是否执行成功
        if [ $? == 0 ];then
                echo "命令执行成功"
        fi
done
[yinzhengjie@s101 ~]$ 
查看集群的命令脚本([yinzhengjie@s101 ~]$ more `which xcall.sh`)

相关文章:

  • 2021-07-06
  • 2021-08-22
  • 2021-11-19
  • 2022-02-10
  • 2021-12-10
  • 2022-01-24
  • 2022-02-22
  • 2021-09-28
猜你喜欢
  • 2021-10-13
  • 2022-03-14
  • 2021-06-27
  • 2021-12-27
  • 2021-12-06
  • 2021-12-11
  • 2022-12-23
相关资源
相似解决方案