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

一、数据导入(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 表名;

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive导入数据的命令可以使用以下两种方式: 1. 使用dfs命令导入: 首先,使用dfs命令将数据文件复制到HDFS的目录,例如: dfs -put /path/to/local/file /user/hive/warehouse/table_name 2. 使用LOAD命令导入: 可以使用LOAD命令将数据文件直接加载到Hive,例如: LOAD DATA LOCAL INPATH '/path/to/local/file' INTO TABLE table_name; 其,"/path/to/local/file"是本地文件的路径, "/user/hive/warehouse/table_name"是Hive表在HDFS的路径。具体命令根据你的实际情况来变化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Hive几种常见的数据导入方式](https://blog.csdn.net/lz_N_one/article/details/126052663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Hive 安装与hive数据导入导出与3种数据的格式的导入附查看命令](https://blog.csdn.net/Mogeko1/article/details/127442078)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值