hive基本操作

启动Metastore server

hive --service metastore &

启动hive server2

hive --service hiveserver2 &

查看所有的数据库

show  databases;

查看当前库中所有的表

show tables;

查看表中所有的数据

select * from table_name;

hive数据类型
结构体
键值对
数组
在这里插入图片描述
hive会为每个数据库创建一个目录,数据库中的表都将会以这个数据库目录的子目录形式存储,有一个例外是default数据库中的表,这个数据库本身没有自己的数据库

创建数据库

create (data|shema) [if not exists] database_name
[comment database_comment]
[location hdfs_path]
[with dbproperties (property_name=property_value,...)

if not exists 当数据库不存在使创建
comment 添加注释
location 增加hdfs的地址,不指定迷人使用数据仓库地址
with dbproperties 指定数据库的属性信息
data和shema 功能相同,可以互换,代表数据库
切换数据库

use database_name;

删除数据库

drop (database|shema)[if exists] database_name [restrict|cascade]

IF EXISTS 当数据库不存在时不报异常’
RESTRICT|CASCADE 约束|级联 默认约束 约束即如果删除的数据库中有表数据,则删除失败,如果指定为级联无论删除的数据库是否有表数据,强制删除
过滤显示数据库前缀为db_hive的所有数据库

show databases like 'db_hive*';

查看当前使用的数据库

select current_database();

使提示符显示当前所在的数据库

set hive.cli.print.current.db=true;

若要不显示,true改为false即可
为数据库添加描述信息

 create database financials1 comment 'holds all financial tables';

查看comment添加的描述信息

describe database database_name;

显示详细信息

 describe database extended database_name;

修改数据库

alter database databse_name set dbproperties(' ...'='. ..',...);

注意,没有办法可以删除或者重置数据库属性,数据库的其他元信息都是不能更改的,我们可以修改data、owner、location等,但修改location后,原有的内容不会被修改,只是再写入的时候会往新的location里写入。

hive 表操作

Create [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]

EXTERNAL 声明外部表
PARTITIONED 创建分区
CLUSTERED 创建桶

创建表的时候指定表所在的数据库有两种方法:(1)在创建表的之前使用USE命令指定当前使用的数据库 (2)在表名前添加数据库声明,例如database_name.table_name

查看表结构

desc table_name;

查看详细的表结构

desc formatted table_name;

将本地文件导入hive

load data local inpath'/你的文件路径/' into table table_name;

若不在hive命令行下则执行这个命令

hive -e "load data local inpath '/你的路径/' into table table_name";

删除表

drop table if exists table_name;

我们可以拷贝一张已经存在的表的表格式(而无需拷贝数据)

create table if not exists database_name.newtable_name  like database_name.oldtable_name;

创建外部表

create external table test_db.emp(id int,name string);

创建外部表,指定location关键字,指定表创建的位置

hive >create external table test_db.emp2(
>id int,
>name STRING)
>ROW FORMAT DELTMITED FIFLDS
>TERMINATED BY '\t' LOCATION '/input/hive';

删除外部表

drop table 库名.表名

分区表
创建分区表

hive> create table partitiontable(
    > id int ,name string,gender string)
    > partitioned by (age int)
    > row format delimited fields terminated by '\t';

查询分区
在这里插入图片描述

增加分区

alter table table_name add partition(分区条件);

增加多个分区,eg:

alter table studentx add partition(age=22) partition (age=33)
 ;

删除分区

alter table studentx drop partition (age=33);

查看分区

show partitions partitiontable_name;

导入数据时同时指定分区

load data local inpath '/../../..' into table table_name partition(..);

使用load关键字导入数据列表中,默认使用的是静态分区
分桶表的创建

hive> create table user_info2(user_id int,name string)
    > clustered by(user_id)
    > into 6 buckets
    > row format delimited fields terminated by'\t';

创建中间表

create table user_info_tmp2(user_id int,name string)
 row format delimited fields terminated by '\t';

将临时表导入分区表

insert into table table_name select 需要的列,需要的列 from tmptable_name;

这是一个MapReduce操作,所以有点慢
查看桶数据对应的hdfs数据仓库地址
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

且从容.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值