学习笔记1 hive基本操作

本文详细介绍了Hive的基本操作,包括数据库创建、内部表与外部表的操作、分区表和分桶表的管理,以及如何修改表结构。内容涵盖了创建数据库、指定存储位置、创建表、删除表、加载数据、分区和分桶等关键知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、数据库操作

  1. 创建数据库
    创建数据库myhive
create database if not exists myhive;

2.创建数据库并指定位置

create database myhive2 location '/myhive2';
  1. 选择操作某数据库
use myhive;
  1. 创建表
create table t_test(id int , name string);

在hive内,数据库与数据库中的表是以目录的形式存在的。

  1. 创建数据库键值对信息
    数据库可以有一些描述性的键值对信息,在创建时添加:
create database foo with dbproperties ('owner'='itcast','date'='20210518');
  1. 查看数据库的键值对信息
describe database extended foo;
  1. 修改数据库的键值对信息
alter database foo set dbproperties ('itcast'='itheima');
  1. 查看数据库更多详细信息
desc database extended myhive2;
  1. 删除数据库
    删除空的数据库
drop database myhive2;

强制删除数据库(包括数据库下的表)

drop database myhive cascade;

二、数据库表操作

1. 创建表的语法:

create [external] table [if not exists] table_name (
col_name data_type [comment '字段描述信息']
col_name data_type [comment '字段描述信息'])
[comment '表的描述信息']
[partitioned by (col_name data_type,...)]
[clustered by (col_name,col_name,...)]
[sorted by (col_name [asc|desc],...) into num_buckets buckets]
[row format row_format]
[storted as ...]
[location '指定表的路径']

2. 内部表的操作

  1. 建表:
use myhive;
create table stu(id int,name string);
insert into stu vaalues(1,"zhangsan");  #插入数据
select * from stu;
  1. 创建表并指定字段之间的分隔符
    hive 表文件字段之间的分隔符
create table if not exists stu2(id int,name string) row format delimited fields terminated by '\t';
  1. 创建表并指定文件的存放路径
create table if not exists stu2(id int,name string) row format dalimited fields terminated by '\t' location '/user/stu2';
  1. 根据查询结果创建表
create table stu3 as select * from stu2;  # 通过复制表结构和标内容创建新表
  1. 根据已经存在的表结构创建表
create table stu4 like stu;
  1. 查询表的详细信息
desc formatted stu2;
  1. 删除表
drop table stu4;

3. 外部表的操作

外部表的数据一般用于共享。删除hive表时,数据仍然存放在hdfs中,不会删掉。

  1. 创建外部表:
create external table teacher (t_id string, t_name string) row format delimited fields terminated by '\t';
  1. 加载数据(本地加载)
  • 方法一(不建议使用)
hive> insert into stu values(1,'zhangzan');
  • 方法二:
    对文件做复制操作
load data local inpath '/export/servers/hivedatas/student.csv' into table student;
  1. 加载数据并覆盖已有数据
load data local inpath '/export/servers/hivedatas/student.csv' overwrite into table student;
  1. 从hdfs文件系统向表中加载数据(需要提前将数据上传到hdfs文件系统)
    hive可以将hdfs文件映射成一张表。(依靠hive的元数据)
hdfs dfs -put teacher.csv /hivedatas/
load data inpath 'hivedatas/teacher.csv' into table teacher;

对文件做剪切操作

  1. 表的修复(建立表与数据文件之间的一个关系映射)
msck repair table score4;

4.分区表的操作

将大的hive表文件分切成一个个小的文件,存放在不同的文件夹中。

  1. 创建分区表语法
create table score(s_id string,c_id string,s_score int) partitioned by (month string) row format delimited field terminated by '\t';
  1. 创建一个表带多个分区(创建多级文件夹)
create table score2(s_id string,c_id string,s_score int) partitioned by (year string,month string,day string) row format delimited field terminated by '\t';
  1. 加载数据到分区表中
load data local inpath '/export/servers/hivedatas/score.csv' into table score partition (month='202105');
  1. 加载数据到多分区表中
load data local inpath '/export/servers/hivedatas/score.csv' into table score2 partition (year='2021'month='05',day='18');
  1. 多分区表联合查询(使用 union all)
select * from score where month = '2021' union all select * from score where month = '202105';
  1. 查看分区
show partitions score;
  1. 添加一个分区
alter table score add partition(month = '202105');
  1. 删除分区
alter table score drop partition(month = '202105');

5. 分桶表操作

分桶:将数据按照指定的字段划分到多个文件当中去(即MapReduce中的分区)。
可避免单个表文件数据过大。
查询时会将所有数据输出。

  1. 开启hive的分桶功能
set hive.enforce.bucketing=true;
  1. 设置reduce个数
set mapreduce.job.reduces=3;
  1. 创建分桶表
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by '\t';
  1. 创建普通表(做中间表)
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by '\t';
  1. 普通表中加载数据
load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;
  1. 通过insert overwrite 给通表中加载数据
insert overwrite table course select * from course_common cluster by(c_id);

6.修改表结构

  1. 重命名
alter table old_table_name rename to new_table_name;
  1. 增加/修改列信息
  • 查询表结构
desc score5;
  • 添加列
alter table first add columns (mycol string,mysco int);
  • 更新列
alter table first change column mycol mycolnew string;
  • 删除表(略)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值