hive建表基础知识

1. 查看数据库

show databases;

2. 创建数据库

create database db_name;

Hive的数据都是存储在HDFS上的,默认有一个根目录,在hive-site.xml中,由参数hive.metastore.warehouse.dir指定。默认值为/user/hive/warehouse.

3. 查看数据库信息

describe database db_name;

Hive中的数据库在HDFS上的存储路径为:
${hive.metastore.warehouse.dir}/databasename.db

4. 内部表

内部表通常存储在/user/hive/warehouse目录下,例如通过其他表新建的表

-- 切换数据库
use helloworld;
-- 如果存在的话就删掉
-- drop table if exists dim_hellowrod;
-- 创建hive表
create table  if not exists dim_helloworld(
    rowkey string
    ,hello string
    ,world bigint
)
partition by (ds string);
-- 如果原来分区不存在就创建新的分区
alter table dim_helloworld add if not exists partition (ds='2022-02-02');
-- 如果原来分区存在就用新数据覆盖掉
insert overwrite if not exists dim_helloworld partition by (pt='2022-02-02')
-- 将ods的数据同步到dim表
select * 
from ods_hellowrod
where ds = '2022-02-02'

5. 外部表

但是很多时候我们的数据通常存储在其他的路径,例如通过spark处理过的数据存储在hdfs的某个位置,这个位置通常不是hive表默认存储数据的位置,这个时候我们就可以创建外部表。

create table  if not exists dim_helloworld(
    rowkey string
    ,hello string
    ,world bigint
)
LOCATION 'hdfs://localhost:9000/user/spark/helloworld/part-00000';

一个数据表简单的说由两个部分组成,表结构和表数据。表结构一定是存在hive的指定目录下面的,因为需要hive进行管理,但是数据并不是。当我们删除外部表的时候,其实只把表结构给删除了,但是数据还在原来的路径。删除内部表的时候,则会把表结构和数据都删除

6.表命名规范

命名规则:{project_name}.dim_{业务}_{维度}_[{自定义命名标签}],其中的pub与具体业务无关,各个业务部都可以共用,例如时间维度。


[一起学Hive]之三–Hive中的数据库(Database)和表(Table)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值