HiveQL详解HiveQL是一种类似SQL的语言, 它与大部分的SQL语法兼容, 但是并不完全支持SQL标准, 如HiveQL不支持更新操作, 也不支持索引和事务, 它的子查询和join操作也很局限, 这是因其底层依赖于Hadoop云平台这一特性决定的, 但其有些特点是SQL所无法企及的。
例如多表查询、支持create table as select和集成MapReduce脚本等, 本节主要介绍Hive的数据类型和常用的HiveQL操作。
1.hive client命令a.hive命令参数-e: 命令行sql语句-f: SQL文件-h, --help: 帮助--hiveconf: 指定配置文件-i: 初始化文件-S, --silent: 静态模式(不将错误输出)-v,--verbose: 详细模式b.交互模式hive> show tables; #查看所有表名hive> show tables 'ad*' #查看以'ad'开头的表名hive>set命令 #设置变量与查看变量;hive>set-v #查看所有的变量hive>set hive.stats.atomic #查看hive.stats.atomic变量hive>set hive.stats.atomic=false #设置hive.stats.atomic变量hive> dfs -ls #查看hadoop所有文件路径hive> dfs -ls /user/hive/warehouse/ #查看hive所有文件hive> dfs -ls /user/hive/warehouse/ptest #查看ptest文件hive> source file<filepath> #在client里执行一个hive脚本文件hive> quit #退出交互式shellhive>exit #退出交互式shellhive> reset #重置配置为默认值hive> !ls #从Hive shell执行一个shell命令2.操作及函数查看函数:hive> show functions;正则查看函数名:show functions 'xpath.*';查看具体函数内容:describe function xpath; |desc function xpath;3.字段类型Hive支持基本数据类型和复杂类型, 基本数据类型主要有数值类型(INT、FLOAT、DOUBLE)、布尔型和字符串, 复杂类型有三种:ARRAY、MAP 和STRUCT。
a.基本数据类型TINYINT: 1个字节SMALLINT: 2个字节INT: 4个字节BIGINT: 8个字节BOOLEAN: TRUE/FALSEFLOAT: 4个字节,单精度浮点型DOUBLE: 8个字节,双精度浮点型STRING 字符串b.复杂数据类型ARRAY: 有序字段MAP: 无序字段STRUCT: 一组命名的字段4.表类型hive表大致分为普通表、外部表、分区表三种。
a.普通表创建表hive>create table tb_person(id int, name string);创建表并创建分区字段dshive>create table tb_stu(id int, name string) partitioned by(ds string);查看分区hive> show partitions tb_stu;显示所有表hive> show tables;按正则表达式显示表,hive> show tables 'tb_*';表添加一列hive>alter table tb_person add columns (new_col int);添加一列并增加列字段注释hive>alter table tb_stu add columns (new_col2 int comment 'a comment');更改表名hive>alter table tb_stu rename to tb_stu;删除表(hive只能删分区,不能删记录或列 )hive>drop table tb_stu;对于托管表, drop操作会把元数据和数据文件删除掉, 对于外部表, 只是删除元数据。
如果只要删除表中的数据, 保留表名可以在 HDFS 上删除数据文件: hive> dfs –rmr /user/hive/warehouse/mutill1/*将本地/home/hadoop/ziliao/stu.txt文件中的数据加载到表中, stu.txt文件数据如下:1 zhangsan2 lisi3 wangwu将文件中的数据加载到表中hive>load data local inpath '/home/hadoop/ziliao/stu.txt' overwrite into table tb_person;加载本地数据,同时给定分区信息hive>load data local inpath '/home/hadoop/ziliao/stu.txt' overwrite into table tb_stu partition (ds='2008-08-15');备注:如果导入的数据在HDFS 上,则不需要local 关键字。
托管表导入的数据文件可在数据仓库目录“user/hive/warehouse/<tablename>”中看到。
查看数据hive> dfs -ls /user/hive/warehouse/tb_stuhive> dfs -ls /user/hive/warehouse/tb_personb.外部表external关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(location),hive创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。
在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
eg. 创建外部表:create external table tb_record(col1 string, col2 string) row format delimited fields terminated by'\t' location '/user/hadoop/input';这样表tb_record的数据就是hdfs://user/hadoop/input/* 的数据了。
c.分区表分区是表的部分列的集合, 可以为频繁使用的数据建立分区, 这样查找分区中的数据时就不需要扫描全表, 这对于提高查找效率很有帮助。
创建分区:create table log(ts bigint,line string) partitioned by(name string);插入分区:insert overwrite table log partition(name='xiapi') select id from userinfo where name='xiapi';查看分区:show partitions log;删除分区: alter table ptest drop partition (name='xiapi')备注:通常情况下需要先预先创建好分区,然后才能使用该分区。
还有分区列的值要转化为文件夹的存储路径,所以如果分区列的值中包含特殊值,如'%', ':', '/', '#',它将会被使用%加上 2 字节的ASCII 码进行转义。
5. sql操作及桶1). 创建表首先建立三张测试表:userinfo表中有两列,以tab键分割,分别存储用户的id和名字name;classinfo表中有两列,以tab键分割,分别存储课程老师teacher和课程名classname; choice表中有两列,以tab键分割,分别存储用户的userid和选课名称classname(类似中间表)。
创建测试表:hive>create table userinfo(id int,name string) row format delimited fields terminated by'\t';hive>create table classinfo(teacher string,classname string) row format delimited fields terminated by'\t';hive>create table choice(userid int,classname string) row format delimited fields terminated by'\t';注意:'\t'相当于一个tab键盘。
显示刚才创建的数据表:hive> show tables;2). 导入数据建表后,可以从本地文件系统或HDFS 中导入数据文件,导入数据样例如下:userinfo.txt内容如下(数据之间用tab键隔开):1 xiapi2 xiaoxue3 qingqingclassinfo.txt内容如下(数据之间用tab键隔开):jack mathsam chinalucy englishchoice.txt内容如下(数据之间用tab键隔开):1 math1 china1 english2 china2 english3 english首先在本地“/home/hadoop/ziliao”下按照上面建立三个文件, 并添加如上的内容信息。
3. 按照下面导入数据。
hive>load data local inpath '/home/hadoop/ziliao/userinfo.txt' overwrite into table userinfo;hive>load data local inpath '/home/hadoop/ziliao/classinfo.txt' overwrite into table classinfo;hive>load data local inpath '/home/hadoop/ziliao/choice.txt'overwrite into table choice;查询表数据hive>select*from userinfo;hive>select*from classinfo;hive>select*from choice;4. 分区a.创建分区hive>create table ptest(userid int) partitioned by (name string) row format delimited fields terminated by'\t';b.准备导入数据xiapi.txt内容如下(数据之间用tab键隔开):1c.导入数据hive>load data local inpath '/home/hadoop/ziliao/xiapi.txt' overwrite into table ptest partition (name='xiapi');d.查看分区hive> dfs -ls /user/hive/warehouse/ptest/name=xiapi;e.查询分区hive>select*from ptest where name='xiapi';f.显示分区hive> show partitions ptest;g.对分区插入数据(每次都会覆盖掉原来的数据):hive>insert overwrite table ptest partition(name='xiapi') select id from userinfo where name='xiapi';h.删除分区hive>alter table ptest drop partition (name='xiapi')5.桶可以把表或分区组织成桶, 桶是按行分开组织特定字段, 每个桶对应一个reduce 操作。