Hive中数据的导入与导出的几种方式

本文详细介绍了Hive中的五种数据导入方法,包括load数据、通过查询语句插入数据、查询语句中创建并加载数据、创建表时指定HDFS路径以及使用import命令。同时,文章也阐述了五种数据导出方式,如export导出到HDFS、使用insert导出、hadoop命令导出、hiveshell命令导出以及sqoop导出。此外,还涉及了如何清除管理表中的数据。

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

一、数据导入(5种方式)

  1. 向表中装载数据(load在HDFS上表现为剪切)
    hive> load data [local] inpath ‘/opt/module/datas/student.txt’ [overwrite] into table student [partition (partcol1=val1,…)];
    (1)load data:表示加载数据
    (2)local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表
    (3)inpath:表示加载数据的路径
    (4)overwrite:表示覆盖表中已有数据,否则表示追加
    (5)into table:表示加载到哪张表
    (6)student:表示具体的表
    (7)partition:表示上传到指定分区
  2. 通过查询语句向表中插入数据
    create table stu(id int ,name string,……)partitioned by (month string);
    insert into table stu select * from stu2;(这时从表2查询的字段数必须和表1的字段数相同
    insert into【overwrite】 table stu values(id1,name1,……),(id2,name2,……);
    多表多分区插入模式:
    insert overwrite table stu partition(month) select id,name from stu1 where month=‘201709’
    insert overwrite table stu partition(month) select id,name from stu2 where month=‘201709’;
  3. 查询语句中创建表并加载数据(as select)
    create table if not exists stu1 as select * from stu;
  4. 创建表时通过Location指定加载HDFS数据路径
    create external table stu(id int,name string) location ‘/student’;
  5. import数据到指定Hive表中(用import之前要先用export将数据导出,数据导入后,HDFS上的数据仍在)
    import table stu partition(month=‘201709’) from ‘/user/hive/warehouse/export/stu’;

二、数据导出(5种方式)

  1. export 导出到HDFS上(主要用于两个集群之间的hive表迁移,export不仅导出数据,而且导出表结构)
    export table default.stu to ‘/user/hive/warehouse/export/stu’;

  2. insert导出
    将查询结果导出到本地文件:insert overwrite local directory ‘/opt/module/datas/stu.txt’select * from stu;
    将查询结果格式化导出到本地:insert overwrite local directory ‘/opt/module/datas/stu.txt’row format delimited fields terminated by ‘/t’ select * from stu;
    将查询结果格式化导出到HDFS上(没有local):insert overwrite directory ‘/user/hive/warehouse/stu’row format delimited fields terminated by ‘/t’ select * from stu;

  3. hadoop命令导出到本地(下载)
    dfs -get /user/hive/warehouse/stu/000000_0(HDFS文件路径) /opt/module/datas/stu.txt(本地路径);

  4. hive shell 命令导出
    bin/hive -e ‘select * from stu ;’ > /opt/module/datas/stu.txt;

  5. sqoop导出

三、清楚表中数据(管理表)
truncate table 表名;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值