Hive常用交互命令

Hive常用交互命令
–创建表
create table flowinfo
(id string,phonenum string,mac string,ip string,url string,num1 string,num2 string,upflow int,downflow int,code string,
datelong string)row format delimited fields terminated by " ";

–导入表
load data local inpath “/home/jixiong/log01.data” into table flowinfo;

–建库 已存在时不会报异常
create database if not exists db_hive;

–显示数据库信息
show databases like ‘db_’;
–显示详细信息
desc database extended db_hive;

–修改数据库
alter database db_hive set dbproperties(‘createtime’=‘20201101’);

–删除(空)数据库
drop database db_hive;
–根据查询结果创建表
create table if not exists student2 as select id,name from stu;

–根据已经存在的表结构创建表
create table if not exists student3 like student;

–查询表的类型
desc formatted stu;

–创建外部表
create external table if not exists student4(
id int,
name string,
age int);

–把内部表修改为外部表
alter table student2 set tblproperties(‘EXTERNAL’=‘TRUE’);
–把外部表转为内部表
alter table student2 set tblproperties(‘EXTERNAL’=‘FALSE’);

–创建分区表
create table studentfq(
id int,name string,age int)
partitioned by (year string,month string)
row format delimited fields terminated by ’ ';
–加载数据到分区表中
load data local inpath ‘/home/jixiong/stu.txt’ into table student.studentfq partition(year=‘2020’,month=‘12’);
load data local inpath ‘/home/jixiong/stu.txt’ into table student.studentfq partition(year=‘2020’,month=‘11’);
load data local inpath ‘/home/jixiong/stu.txt’ into table student.studentfq partition(year=‘2019’,month=‘12’);
–查询分区表中数据
select * from studentfq where year=‘2020’ and month =‘12’;
–创建多个分区 中间用空格分开 单个分区时只写一个
alter table studentfq add partition(year=‘2020’,month=‘07’) partition(year=‘2018’,month=‘10’);
–删除多个分区 中间用逗号,隔开 单个则直接分号结束
alter table studentfq drop partition(year=‘2020’,month=‘07’),partition(year=‘2018’,month=‘10’);
–查看分区表有多少分区
show partitions studentfq;
–查看分区表结构
desc formatted studentfq;
–把数据直接上传到分区目录上让分区表和数据产生关联
–上传数据后load数据到分区
–创建目录
dfs -mkdir -p /user/hive/warehouse/student.db/studentfq/year=2018/month=03;
–上传目录
load data local inpath ‘/home/jixiong/stu.txt’ into table studentfq partition(year=‘2018’,month=‘03’);
–查询数据
select * from studentfq where year=‘2018’ and month=‘03’;

–修改表名
alter table stu1 rename to student1;

–查询表结构‘
desc student1;
–添加列(添加字段)
alter table stu add columns(stuno int);
–更新列(更新字段)
alter table stu change column stuno desc string;
–替换列 (全部替换)
alter table stu replace columns (id int,name string,age1 int,stuno string);
–通过查询语句向表中插入数据
–创建一张分区表
create table student4(id int,name string) partitioned by (year string,month string) row format delimited
fields terminated by " ";
–插入数据
insert into table student4 partition(month=“201810”) values(1001,‘wuyifan’);
–根据查询其他表的结果插入
from student2
insert overwrite table student4 partition(month=“201704”) select id,name
insert overwrite table student4 partition(month=“201705”) select id,name;

–根据查询结果创建表
create table if not exists student5 as select id,name,age from studentfq;

–创建表时通过location指定加载数据路径
–创建表并指定在hdfs上的位置
create table if not exists student6(id int,name string,age int)
row format delimited fields terminated by " "
location “/user/hive/warehouse/student.db/student6”;
–上传数据到hdfs上
dfs -put /stu.txt /user/hive/warehouse/student.db/student6;
–查询数据库
select * from student6;
–将查询的结果导出到本地
insert overwrite local directory “/home/jixiong/student” select * from student5;
–将查询的结果格式化导出到本地
insert overwrite local directory “/home/jixiong/student1” row format delimited fields terminated by " " select * from student5;
–将查询的结果导出到hdfs上(没有local)
insert overwrite directory “/user/hive/warehouse/student.db/student7” row format delimited fields terminated by " " select * from student5;

===================================================
注释:
CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXISTS 选项来忽略这个异常。
EXTERNAL关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(LOCATION),Hive创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
COMMENT:为表和列添加注释。
PARTITIONED BY创建分区表
CLUSTERED BY创建分桶表
SORTED BY不常用
ROW FORMAT
STORED AS指定存储文件类型
load data:表示加载数据
local:表示从本地加载数据到hive表(复制);否则从HDFS加载数据到hive表(移动)
inpath:表示加载数据的路径
overwrite into:表示覆盖表中已有数据,否则表示追加
into table:表示加载到哪张表
student:表示具体的表
partition:表示上传到指定分区
常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件)
如果文件数据是纯文本,可以使用STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。
LOCATION :指定表在HDFS上的存储位置。
LIKE允许用户复制现有的表结构,但是不复制数

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值