Hive的安装和一些例子

Hive

hive 的表就是 hdfs 的目录,数据就是 hdfs 的文件

嵌入模式:hive + derby,demo

每次执行 hive 命令,都会在当前目录中生成数据 derby 文件

在 console 管理界面执行命令 dfs ls

desc tableName

! 系统命令

Hive -S 静默模式

create table (id INT, name STRING)

安装客户端工具

Hadoop+Hive环境搭建

远程管理

1
2
3
4
5
6
7
启动web管理服务 hive --service hwi
编译war包:src/hwi jar cvfM0 hive-hwi-0.13.0.war -C web/ .
拷贝到hive的lib目录下
修改hive-site.xml
cp jdk/lib/tools.jar hive/lib/

hive --service hiveserver &

数据类型的一些例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 // 学生成绩
create table s (id int, name String, grade array<float>); -- {1, tom [90, 80.00]}

create table s2 (id int, name String, grade map<string, float>); -- {1, tom <"语文", 80.00>}

create table s3 (id int, name String, grade array<map<string, float>>); -- {1, tom [<"语文", 80.00>, <"英语", 90.00>]}

//结构数据类型
create table s4 (id int, info struct<name:string, age:int>); -- {1, {"tom", "19"}}

// 创建表并指定存储位置,不指定的情况下会默认在 /usr/hive/warehouse
create table s5(id int) location '/pwd/s5';

// 指定列的分割符号为 逗号
create table s6 (id int, name String) row format delimited fields terminated by ',';

//创建表并从 s5导入数据,以逗号为列的分隔符
crate table t7 row format delimited fields terminated by ',' as select * form s5;

select unix_timestamp();

date 数据类型只有日期,没有具体到几点的时间

分区表

1
2
3
4
//基于 gender 的分区表
create table partition_table(id int, name String) partitioned by (gender String) row format delimited fields terminated by ',';

insert into table partition_table partition(gender='M') slect id, name from sample_data where gender = 'M';

外部表

1
2
3
4
create external table external_student (sid int, sname string, age int) row format delimited fields terminated by ',' location '/input';

// 把本地文件导入到 hive 数据库中
LOAD DATA LOCAL INPATH '/data/login/20130101.csv' OVERWRITE INTO TABLE login PARTITION (dt='20130101');

桶表

1
2
create table bucket_table (sid int, sname string, age int) clustered by (sname) into 5 buckets row format delimited fields terminated by ',';
//创建一个桶表,这个桶表是以sname作为哈希运算,运算后的结果放到5个桶中

视图

1
2
3
4
5
6
create view empinfo
as
select e.empno,e.ename,e.sal,e.sal*12 annlsal, d.dname
from emp e, dept d
where e.deptho=d.deptho;
//e.sal*12 annlsal :为e.sal*12起一个别名叫annlsal. 其他操作和普通的表一样。

数据导入

1
2
3
//导本机目录下的数据到t3表,并覆盖原来的数据。

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