Hive 基操

Hive启动方式

1、Xshell直接输入:hive

[root@guo146 ~]# hive

2、beeline

//启动hiveserver2服务
[root@guo146 ~]# hive --service hiveserver2


//效果同上,错误输出将会和标准输出  输出到黑洞
[root@guo146 ~]# nohup hiveserver2 1>/dev/null 2>&1 &



[root@guo146 ~]# nohup hive --service metastore &

[root@guo146 ~]# nohup hive --service hiveserver2 &

//输入beeline进入beeline交互模式
[root@guo146 ~]# beeline -u jdbc:hive2://guo146:10000

 在hdfs上给文件执行,读写权限

[root@guo146 ~]# hdfs dfs -chmod -R 777 /tmp

 常用命令

0: jdbc:hive2://guo146:10000> show databases;

//查看当前所在的数据库
0: jdbc:hive2://guo146:10000> select current_database();

//查看数据库详细信息
0: jdbc:hive2://guo146:10000> describe database kb21;

//创建数据库
0: jdbc:hive2://guo146:10000> create database if not exists guo;

//查看当前数据库所有表
0: jdbc:hive2://guo146:10000> show tables;

//创建表
0: jdbc:hive2://guo146:10000> create table if not exists nj(id int,name string);

//查看表信息
0: jdbc:hive2://guo146:10000> describe nj;

//查看建表语句
0: jdbc:hive2://guo146:10000> show create table nj;

//删除表
0: jdbc:hive2://guo146:10000> drop table if exists nj;

//删除数据库
0: jdbc:hive2://guo146:10000> drop database if exists guo;

//强制删除数据库
0: jdbc:hive2://guo146:10000> drop database if exists guo cascade;

Hive集合类型

建表

静态表

//建表语句
create table student(
    id int,
    name string,
    likes array<string>,
    address map<string,string>
)
row format delimited fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
lines terminated by '\n';


//加载本地数据
load data local inpath '/opt/stufile/student2.txt' into table student;
//加载HDFS文件,原文件将移动到student下
load data inpath '/student.txt' into table student;
//加载数据后覆盖原数据
load data inpath '/student.txt' overwrite  into table student;

分区表

//建表语句
create table student2(
    id int,
    name string,
    likes array<string>,
    address map<string,string>
)
partitioned by (age int)
row format delimited fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
lines terminated by '\n';

//分区表插入数据
load data local inpath '/opt/stufile/student2.txt' into table student2 partition(age=20);
load data local inpath '/opt/stufile/student2.txt' into table student2 partition(age=30);

//查看分区表信息
0: jdbc:hive2://guo146:10000> show partitions studen2;

多字段分区表

//建表语句
create table student3(
    id int,
    name string,
    likes array<string>,
    address map<string,string>
)
partitioned by (age int, gender string)
row format delimited fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
lines terminated by '\n';

//分区表插入数据
load data local inpath '/opt/stufile/student2.txt' into table student3 partition(age=20,gender='boy');
load data local inpath '/opt/stufile/student2.txt' into table student3 partition(age=20,gender='girl');

//查看动态分区信息
show partitions student3;

外部表

//创建外部表
create external table studentwb1(
    id int,
    name string,
    likes array<string>,
    address map<string,string>
)
row format delimited fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
lines terminated by '\n'
location '/tmp/hivedata/student';

//插入数据
[root@guo146 ~]# hdfs dfs -put /opt/stufile/student2.txt /tmp/hivedata/student/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值