Hive 基础
Hive启动:
$ start-dfs.sh
$ start-yarn.sh
$ systemctl start mysqld
$ hive
插入数据:
创建表
hive> create table student(id int, name string);
插入数据
hive> insert into student values(1,"baozi");
查询
hive> select * from student;
插入数据文件:
创建表
hive> create table emp(id int,name string) row format delimited fields terminated by '\t';
插入数据
$ vim emp-data
1 baozi1
2 baozi2
3 baozi3
hive> load data local inpath '/home/user000/data/emp-data' into table emp;
查询
hive> select * from emp;
Hive交互命令:
hive -S 静默模式进入
hive -e '要执行的语句'; 直接执行
hive -f xxx.hql 执行hql文件
dfs ... hive命令窗口使用hdfs dfs ...命令
! 命令 执行操作系统命令
参数配置方式:
- hive> set ......
- hive -hiveconf ......
- 配置文件
查看数据库与表:
$ show databases;
$ show databases like 'hive*';
类型转换
默认:自动向上转型,BOOLEAN除外。
手动:CAST('1' AS INT),失败返回NULL。
Hive 数据类型
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 | 相当于varchar | 不过不能生命存储多少个 |
TIMESTAMP | 时间类型 | BINARY | 字节数组 |
复杂数据类型 | 描述 |
---|---|
ARRAY | 使用“array名[下标]”访问:friends[1] |
MAP | 使用“map名['属性名']”访问:children['xiao song'] |
STRUCT | 类似于C语言的struct使用“struct名.属性”访问:address.city与Map不同的就是,map是根据键找值,struct是访问属性 |
数据类型示例
$ vim test.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
操作
直接将数据文件放到/user/hive/warehouse/test/目录下,可以免去load data ......
$ hdfs dfs -mkdir /user/hive/warehouse/test/
$ hdfs dfs -put ~/data/test.txt /user/hive/warehouse/test/
hive> 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';
hive> select * from test;
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"}
hive> select friends[1], children['xiao song'],address.city from test;
_c0 _c1 city
lili 18 beijing
susu NULL beijing
Hive hiveserver2
A 终端
$ hiveserver2
B 终端
$ ~/tools/hive/bin/beeline
beeline> !connect jdbc:hive2://host000:10000
Enter username for jdbc:hive2://host000:10000: user000
Enter password for jdbc:hive2://host000:10000:
0: jdbc:hive2://host000:10000> show databases;
0: jdbc:hive2://host000:10000> show tables;
0: jdbc:hive2://host000:10000> select * from test;
默认thriftserver的端口是10000,可以通过启动thriftserver时设置hive.server2.thrift.port=<listening-port>参数更改
网友评论