欢迎投稿

今日深度:

hive基本操作,

hive基本操作,


启动hive  

启动成功后, 配置的mysql数据库会创建一些表来保存元信息。(此时元信息表为空,因为还未创建表)

   4. 创建表 

create table test1(tid int, tname string);

此时表信息会被添加到mysql的元信息表中

(5)Hive的启动方式

CLI(命令行)方式

启动:hive

清屏:Ctrl+L 或者 !clear

查看数据仓库中的表:show tables;

查看数据仓库中内置的函数:show functions;

查看表结构:desc 表名

查看HDFS上的文件: dfs -ls 目录

执行操作系统的命令: !命令

执行HQL语句: select *** from ****

执行SQL的脚本:source SQL文件

进行hive的静默模式(不打印调试信息,只打印结果):hive -S

   Web界面方式

端口号:999

启动方式:#hive —service hwi 

通过浏览器访问:http://<IP地址>:9999/hwi

若报错没有war包,则需要下载源码并打包war包,并修改配置文件再启动。同时需要复制jdk的lib目录下的tools.jar 到hive的lib目录。

   远程服务启动方式

端口号:10000

启动方式:hive —service hiveserver

以JDBC或ODBC的程序登录到hive中操作数据时,必须选用远程服务启动方式


Hive的数据类型

(1)基本数据类型

-tinyint/smallint/int/bigint :整数类型

-float/double: 浮点数类型

-boolean:布尔类型

-string:字符串类型

-varchar(20):字符串,最长为20

-char(20):字符串,长度为20

  (2)复杂数据类型

-Array:数组类型,由一系列相同数据类型的元素组成

-Map:集合类型,包含key->value键值对

-Struct:结构类型 ,可以包含不同数据类型的元素,这些元素可以通过“点语法”的方式来得到所需要的元素

  (3)时间类型

-Date:从Hive0.12.0开始支持

-Timestamp:从Hive0.8.0开始支持

Hive的数据存储

(1)内部表

-与数据库中的Table在概念上是类似的

-每一个Table在Hive中都有一个相应的目录存储数据

-所有的Table数据(不包括External Table)都保存在这个目录中

-删除表时,元数据与数据都会被删除

  例:

1.create table t1(tid int,tname string, age int); --默认存储在 '/user/hive/warehouse'

2.指定存储目录:create table t2(tid int,tname string,age int) location'/mytable/hive/t2';

3.指定分隔符:create table t1(tid int,tname string, age int) rowformat delimited fields terminated by ',';

4.使用查询语句创建新表:create table t4 as select * from sample_data;

5.查看t4表的文件:hdfs dfs -cat /user/hive/warehouse/t4/000000_0

6.create table t5

row format delimited fields terminated by ',' as select * fromsample_data;

7.alter table t1 add columns(english int);

8.查看表结构:desc t1;

9.drop table t1

(2)分区表

-Partition对应数据库的Partition列的密集索引

-在Hive中,表中的一个Partition对应于表下的一个目录,所有的Partition的数据都存储在对应的目录中


例:
1.创建分区表:create table partition_table(sid int,sname string)

partitioned by (gender string)

row format delimited fields terminated by ',';

2.插入数据:insert into table partition_table partition(gender='M')select sid,sname from sample_data where gender=‘M’;


分区表可提高查询效率。

使用explain查询SQL语句的执行计划:

explain select * from sample_data where gender='M';

explain select * from partition_table where gender=‘M';

(3)外部表

-只想已经在HDFS中存在的数据,可以创建Partition

-它和内部表在元数据的组织上是相同的,而实际数据的存储则有较大的差异

-外部表只有一个过程,加载数据和创建表同时完成,并不会移动到数据仓库目录中,知识与外部数据建立一个链接。当删除一个外部表时,仅删除该链接。


例:

1.创建外部表:create external table external_student(sid int,snamestring,age int)

row format delimited fields terminated by ','

location '/input';

2.查询数据:select * from external_student;

(4)桶表

-桶表时对数据进行哈希取值,然后放到不同文件中

例:

创建桶表:create table bucket_table(sid int,sname string,age int)

clustered by(name) into 5 buckets;

(5)视图

-视图是一种虚表,是一个逻辑概念;可以跨越多张表

-视图建立在已有表的基础上,视图赖以建立的这些表称为基表

-视图可以简化复杂的查询

例:

create view empinfo as

select e.empno,e.ename, e.sal,e.sal*12 annlsal,d.dname from empe,dept d where e.deptno=d.deptno;


Hive的数据导入

1)使用load语句

load data [local] inpath 'filepath' [overwrite]

into table tablename [partition (partcol1=val1,partcol2=val2…)]

 例:将/root/data下的所有数据文件导入t3表中,并且覆盖原来的数据

load data local inpath '/root/data/' overwrite into table t3; 

将HDFS中 /input/student01.txt 导入到t3

load data inpath '/input/student01.txt' overwrite into table t3;

将数据导入分区表

load data local inpath '/root/data/data1.txt' into tablepartition_table partition(gender=‘M');

(2)使用Sqoop实现数据的导入

需要下载和安装Sqoop

 例子:

Hive的数据查询

(1)简单查询



Hive的Fetch Task功能开启后,简单查询将不会进行mapreduce操作。

Fetch Task配置方式有以下3种:

1.set hive.fetch.task.conversion=more;

2.hive --hiveconf hive.fetch.task.conversion=more

(2)过滤和排序




(3)Hive的函数

1.内置函数

-数学函数:round、ceil、floor

-字符函数:lower、upper、length、concat、substr、trim、lpad、rpad

-收集函数:size

-转换函数:cast

-日期函数:to_date、year、month、day、weekofyear、datediff、date_add、date_sub

-条件函数:coalesce、case...when...

  例:

select ename, job, sal,

case job when 'PRESIDENT' then sal+1000

when 'MANAGER' then sal+800

else sal+400

end

from emp;

-聚合函数:count、sum、min、max、avg

-表生成函数:explode

(4)Hive的表连接

-等值连接(若连接条件是=)

-不等值连接(不是=)

-外连接

-自连接

(5)Hive子查询

-hive只支持from和where字句中的子查询

Hive的命令分为以下几类:

-进入与退出Hive交互,比如:hive、quit、exit

-参数设置:set、reset

-资源文件管理:add、list、delete

-执行shell命令:!Cmd

-Hdfs文件操作: dfs-ls、dfs-cat

-HiveQL:<query string>

-执行外部文件:source FILE、compile’<groovy string>’ AS GROOVY NAMED <name>



显示地展示当前使用的数据库 

hive> set hive.cli.print.current.db=true;  

使Hive显示列头  set hive.cli.print.header=true;











本文转自 yntmdr 51CTO博客,原文链接:http://blog.51cto.com/yntmdr/1722999,如需转载请自行联系原作者

www.htsjk.Com true http://www.htsjk.com/hive/41486.html NewsArticle hive基本操作, 启动hive   启动成功后, 配置的mysql数据库会创建一些表来保存元信息。(此时元信息表为空,因为还未创建表)    4. 创建表  create table test1(tid int, tname string); 此时表...
相关文章
    暂无相关文章
评论暂时关闭