Hive 视图 索引 动态分区装载数据

本文介绍了Hive SQL的基本操作,包括视图的创建与删除、索引的创建与管理、数据加载与导出等关键步骤。同时提供了动态分区的支持配置及使用方法。

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

视图

创建视图
create view v_emp AS select t.name, t.age, t.addr from t_emp;

删除视图
drop view if exists v_emp;


索引

创建索引
create index t_emp_index
    on table t_emp (name)
    as ‘org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler’
    with deferred rebuild in table t_index;

显示索引
查看索引

重建索引
alter index t_emp_index on t_emp rebuild;

删除索引
drop index if exists t_emp_index on t_emp ;


普通装载数据(分区需指定)

  • 从文件中装载数据

    hive>LOAD DATA [LOCAL] INPATH ‘…’ [OVERWRITE] INTO TABLE t_employee [PARTITION (…)];

  • 通过查询表装载数据

    hive>INSERT OVERWRITE TABLE t_emp PARTITION (…) SELECT * FROM xxx WHERE xxx

  • 批量插入

    hive>FROM t_emp
                INSERT OVERWRITE TABLE t_test PARTITION (…) SELECT … WHERE …
                INSERT OVERWRITE TABLE t_test PARTITION (…) SELECT … WHERE …
                INSERT OVERWRITE TABLE t_test PARTITION (…) SELECT … WHERE …

    batch_insert
    shows


动态分区装载数据(分区不需指定)

  • 若没有开启动态分区只支持以下写法

    hive>INSERT OVERWRITE TABLE t_test PARTITION (country=’china’, city=’chengDu’)
                  SELECT name, age, addr
                   FROM t_emp
                 WHERE t_emp.country = ‘china’
                     AND t_emp.city = ‘chengDu’;

  • 开启动态分区支持

    hive>set hive.exec.dynamic.partition=true; // 开启动态分区
    hive>set hive.exec.dynamic.partition.mode=nostrict; // 设置为非严格模式
    hive>set hive.exec.max.dynamic.partitions.pernode=1000; // 最大动态可分区数
    hive> insert overwrite table t_dynamic partition(country, city) select name, age, addr, country, city from t_emp;

    dynamic
    dynamic2


数据导出

  • 通过hdfs方式导出

    到本地
    hive> dfs -copyToLocal /user/hive_remote/warehouse/t_dynamic /home/tt;

    hive> dfs -get /user/hive_remote/warehouse/t_dynamic /home/t_dynamic;
    这里写图片描述

    到hdfs
    hive> dfs -cp /user/hive_remote/warehouse/t_dynamic /tmp/t_dynamic;
    export hdfs

  • 使用DIRECTORY

    hive> insert overwrite [local] directory ‘/home/tt2’ select * from t_dynamic;
      加local到本地/默认到hdfs

    这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值