文章目录

  • 一、Hive 中数据的导入
    • 1、本地文件系统 导入 Hive 表
    • 2、Hdfs 导入Hive
    • 3、查询结果 导入 Hive
    • 4、创建表时将查询结果 导入 Hive
  • 二、Hive 中数据的导出
    • 1、导出到 本地文件系统
    • 2、导出到 Hdfs
    • 3、导出到 Hive表

环境准备

  • Hadoop 完全分布式(一主两从即可)
  • MySQL环境、Hive环境

一、Hive 中数据的导入

1、本地文件系统 导入 Hive 表

首先,在 Hive 中创建一个 cat_group 表,包含 group_idgroup_name 字段,字符类型为 string,以 ‘\t’ 为分隔符:

hive (db)> create table if not exists cat_group(group_id string ,group_name string)> row format delimited fields terminated by '\t'> stored as textfile;
OK
Time taken: 0.156 secondshive (db)> show tables;
OK
cat
cat3
cat_group
Time taken: 0.021 seconds, Fetched: 3 row(s)

[row format delimited]关键字,是用来设置创建的表在加载数据的时候,支持的列分隔符。
[stored as textfile]关键字,是用来设置加载数据的数据类型,默认是TEXTFILE,如果文件数据是纯文本,就是使用[stored as textfile],然后从本地直接拷贝到HDFS上,Hive直接可以识别数据。

linux 本地 /home/data/hive-data 目录下的 cat_group 文件导入到 Hive 中的 cat_group 表中:

hive (db)> load data local inpath '/../home/data/hive-data/cat_group' into table cat_group;
Loading data to table db.cat_group
OK
Time taken: 2.097 seconds

通过 select...from ... limit 语句查询前10条记录:

hive (db)> select * from cat_group limit 10;
OK
501 有机食品
502 蔬菜水果
503 肉禽蛋奶
504 深海水产
505 地方特产
506 进口食品
507 营养保健
508 休闲零食
509 酒水茶饮
510 粮油副食
Time taken: 0.162 seconds, Fetched: 10 row(s)

返回顶部


2、Hdfs 导入Hive

首先,在 hdfs 上创建 data/hive 目录:

[root@server hive-data]# hdfs dfs -mkdir -p /data/hive


然后将 cat_group 文件上传至 hive 目录下:

[root@server hive-data]# hdfs dfs -put /../home/data/hive-data/cat_group /data/hive
[root@server hive-data]# hdfs dfs -ls  /data/hive
Found 1 items
-rw-r--r--   3 root supergroup       2164 2022-03-06 11:07 /data/hive/cat_group

在 Hive 中创建 cat_group1 表:

hive (db)> create table if not exists cat_group1(group_id string ,group_name string)> row format delimited fields terminated by '\t'> stored as textfile;
OK
Time taken: 0.156 secondshive (db)> show tables;
OK
cat
cat3
cat_group
cat_group1
Time taken: 0.021 seconds, Fetched: 3 row(s)

hdfsdata/hive 目录下的 cat_group 文件数据导入到 cat_group1 表中:

// 提示:hdfs数据导入的时候不用加 local
hive (db)> load data inpath '/data/hive/cat_group' into table cat_group1;
Loading data to table db.cat_group1
OK
Time taken: 0.539 seconds
hive (db)> select * from cat_group1 limit 10;
OK
501 有机食品
502 蔬菜水果
503 肉禽蛋奶
504 深海水产
505 地方特产
506 进口食品
507 营养保健
508 休闲零食
509 酒水茶饮
510 粮油副食
Time taken: 0.107 seconds, Fetched: 10 row(s)

值得注意的是,此时存在 /data/hive 中的数据文件转移到了 /user/hive/warehouse/db.db/cat_group1 文件目录下了:

返回顶部


3、查询结果 导入 Hive

首先,在 Hive 中创建 cat_group2 表:

hive (db)> create table if not exists cat_group2(group_id string ,group_name string)> row format delimited fields terminated by '\t'> stored as textfile;
OK
Time taken: 0.156 secondshive (db)> show tables;
OK
cat
cat3
cat_group
cat_group1
cat_group2
Time taken: 0.016 seconds, Fetched: 3 row(s)

两种方式将 cat_group1 表中的数据导入到 cat_group2 表中:

// 直接导入
hive (db)> insert into table cat_group2 select * from cat_group1;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Query ID = root_20220306111859_2bd20950-a787-4a76-8f2d-415dd3517c32
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1646527355398_0001, Tracking URL = http://server:8088/proxy/application_1646527355398_0001/
Kill Command = /usr/local/src/hadoop/bin/hadoop job  -kill job_1646527355398_0001
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2022-03-06 11:21:23,475 Stage-1 map = 0%,  reduce = 0%
2022-03-06 11:21:30,327 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.02 sec
MapReduce Total cumulative CPU time: 1 seconds 20 msec
Ended Job = job_1646527355398_0001
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to directory hdfs://192.168.64.183:9000/user/hive/warehouse/db.db/cat_group2/.hive-staging_hive_2022-03-06_11-18-59_462_4910696310996519402-1/-ext-10000
Loading data to table db.cat_group2
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1   Cumulative CPU: 1.02 sec   HDFS Read: 6128 HDFS Write: 1751 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 20 msec
OK
Time taken: 153.838 seconds
// 方式二:覆盖导入
hive (db)> insert overwrite  table cat_group2 select * from cat_group1;

最终的结果都是一样的,如下图:

返回顶部


4、创建表时将查询结果 导入 Hive

Hive 中创建表 cat_group3 并直接从表 cat_group2 中获取数据:

hive (db)> create table if not exists cat_group3 as select * from cat_group2;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Query ID = root_20220306112908_50eaa6bf-0723-478e-bb8d-0d5101c23c01
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1646527355398_0003, Tracking URL = http://server:8088/proxy/application_1646527355398_0003/
Kill Command = /usr/local/src/hadoop/bin/hadoop job  -kill job_1646527355398_0003
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2022-03-06 11:30:40,422 Stage-1 map = 0%,  reduce = 0%
2022-03-06 11:30:59,723 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.04 sec
MapReduce Total cumulative CPU time: 1 seconds 40 msec
Ended Job = job_1646527355398_0003
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to directory hdfs://192.168.64.183:9000/user/hive/warehouse/db.db/.hive-staging_hive_2022-03-06_11-29-08_568_2915460888753908036-1/-ext-10002
Moving data to directory hdfs://192.168.64.183:9000/user/hive/warehouse/db.db/cat_group3
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1   Cumulative CPU: 1.04 sec   HDFS Read: 5334 HDFS Write: 1751 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 40 msec
OK
Time taken: 114.647 seconds
hive (db)> select * from cat_group3 limit 10;
OK
501 有机食品
502 蔬菜水果
503 肉禽蛋奶
504 深海水产
505 地方特产
506 进口食品
507 营养保健
508 休闲零食
509 酒水茶饮
510 粮油副食
Time taken: 0.335 seconds, Fetched: 10 row(s)

返回顶部


二、Hive 中数据的导出

1、导出到 本地文件系统

首先,在 linux 本地新建 /../home/data/hive-data/out 目录:


将表 cat_group 的数据导出至本地的 out 目录下:

hive (db)> insert overwrite local directory '/../home/data/hive-data/out' > row format delimited fields terminated by '\t'> select * from cat_group;
FAILED: IllegalArgumentException Pathname /../home/data/hive-data/out/.hive-staging_hive_2022-03-06_11-49-38_025_4043006350015711521-1 from hdfs://192.168.64.183:9000/../home/data/hive-data/out/.hive-staging_hive_2022-03-06_11-49-38_025_4043006350015711521-1 is not a valid DFS filename.
hive (db)> insert overwrite local directory '/home/data/hive-data/out' > row format delimited fields terminated by '\t'> select * from cat_group;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Query ID = root_20220306115007_37878929-f5db-4e80-af09-0c1ca7b7c60d
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1646527355398_0004, Tracking URL = http://server:8088/proxy/application_1646527355398_0004/
Kill Command = /usr/local/src/hadoop/bin/hadoop job  -kill job_1646527355398_0004
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2022-03-06 11:50:39,500 Stage-1 map = 0%,  reduce = 0%
2022-03-06 11:50:44,601 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.03 sec
MapReduce Total cumulative CPU time: 1 seconds 30 msec
Ended Job = job_1646527355398_0004
Moving data to local directory /home/data/hive-data/out
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1   Cumulative CPU: 1.03 sec   HDFS Read: 5671 HDFS Write: 1680 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 30 msec
OK
Time taken: 37.904 seconds

导出完成后,查看本地目录下文件的前10行内容:

[root@server out]# cat ./000000_0 |head -n  10
501 有机食品
502 蔬菜水果
503 肉禽蛋奶
504 深海水产
505 地方特产
506 进口食品
507 营养保健
508 休闲零食
509 酒水茶饮
510 粮油副食

返回顶部


2、导出到 Hdfs

hdfs 上创建 data/hive/out 目录:

[root@server out]# hdfs dfs -mkdir /data/hive/out
[root@server out]# hdfs dfs -ls /data/hive
Found 1 items
drwxr-xr-x   - root supergroup          0 2022-03-06 15:57 /data/hive/out

Hivecat_group 表中的数据导出到 hdfsout 目录下:

hive> insert overwrite directory '/data/hive/out' > row format delimited fields terminated by '\t'> select group_id,group_name from cat_group;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Query ID = root_20220306164548_6780ee23-d932-40fb-b7e7-55afe932bb33
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1646556082284_0001, Tracking URL = http://server:8088/proxy/application_1646556082284_0001/
Kill Command = /usr/local/src/hadoop/bin/hadoop job  -kill job_1646556082284_0001
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2022-03-06 16:46:54,121 Stage-1 map = 0%,  reduce = 0%
2022-03-06 16:47:00,370 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.03 sec
MapReduce Total cumulative CPU time: 1 seconds 30 msec
Ended Job = job_1646556082284_0001
Stage-3 is selected by condition resolver.
Stage-2 is filtered out by condition resolver.
Stage-4 is filtered out by condition resolver.
Moving data to directory hdfs://192.168.64.183:9000/data/hive/out/.hive-staging_hive_2022-03-06_16-45-48_785_6015608084561284378-1/-ext-10000
Moving data to directory /data/hive/out
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1   Cumulative CPU: 1.03 sec   HDFS Read: 5561 HDFS Write: 1680 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 30 msec
OK
Time taken: 73.787 seconds

导出完成后,查看 hdfs 的文件:

[root@server ~]# hdfs dfs -ls /data/hive/out
Found 2 items
drwxr-xr-x   - root supergroup          0 2022-03-06 16:03 /data/hive/out/.hive-staging_hive_2022-03-06_16-03-19_927_4455543203588730803-1
-rwxr-xr-x   3 root supergroup       1680 2022-03-06 16:46 /data/hive/out/000000_0[root@server ~]# hdfs dfs -cat /data/hive/out/000000_0 |head -n 10
501 有机食品
502 蔬菜水果
503 肉禽蛋奶
504 深海水产
505 地方特产
506 进口食品
507 营养保健
508 休闲零食
509 酒水茶饮
510 粮油副食

返回顶部


3、导出到 Hive表

将 Hive 中的 cat_group 表的数导入到 cat_group4中(两表字段及字符类型形同)。

首先,在Hive 中创建 cat_group4 表:

hive (db)> create table if not exists cat_group4(group_id string ,group_name string)> row format delimited fields terminated by '\t'> stored as textfile;
OK
Time taken: 0.156 secondshive (db)> show tables;
OK
cat
cat3
cat_group
cat_group1
cat_group2
cat_group3
cat_group4
Time taken: 0.016 seconds, Fetched: 3 row(s)

然后,将 cat_group 中的数据导出到 cat_group4 中:

hive> insert into table cat_group4 select * from cat_group;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Query ID = root_20220306165034_e2a833b7-d761-4390-a417-4712380b338a
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1646556082284_0002, Tracking URL = http://server:8088/proxy/application_1646556082284_0002/
Kill Command = /usr/local/src/hadoop/bin/hadoop job  -kill job_1646556082284_0002
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2022-03-06 16:51:28,079 Stage-1 map = 0%,  reduce = 0%
2022-03-06 16:51:38,227 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.09 sec
MapReduce Total cumulative CPU time: 1 seconds 90 msec
Ended Job = job_1646556082284_0002
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to directory hdfs://192.168.64.183:9000/user/hive/warehouse/db.db/cat_group4/.hive-staging_hive_2022-03-06_16-50-34_649_124292144843961059-1/-ext-10000
Loading data to table db.cat_group4
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1   Cumulative CPU: 1.09 sec   HDFS Read: 6108 HDFS Write: 1751 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 90 msec
OK
Time taken: 65.267 secondshive> select * from cat_group4 limit 10;
OK
501 有机食品
502 蔬菜水果
503 肉禽蛋奶
504 深海水产
505 地方特产
506 进口食品
507 营养保健
508 休闲零食
509 酒水茶饮
510 粮油副食
Time taken: 0.132 seconds, Fetched: 10 row(s)

返回顶部


【Hive】Hive表数据的导入导出相关推荐

  1. kudu导出为mysql格式_怎么实现kudu表数据的导入导出

    一.kudu数据导出到文件 例: impala-shell -l -u xuyi  --auth_creds_ok_in_clear  -q "select sms_content from ...

  2. [Hadoop大数据]——Hive数据的导入导出

    Hive作为大数据环境下的数据仓库工具,支持基于hadoop以sql的方式执行mapreduce的任务,非常适合对大量的数据进行全量的查询分析. 本文主要讲述下hive载cli中如何导入导出数据: 导 ...

  3. MySQL数据库基础(三)数据的导入导出、管理表记录、匹配条件

    数据的导入导出 搜索路径 查看搜索路径 安装时已经自动创建 命令:mysql > show variables like "secure_file_priv"; mysql& ...

  4. 轻松实现SQL Server与Access、Excel数据表间的导入导出

    在SQL SERVER 2000/2005中除了使用DTS进行数据的导入导出,我们也可以使用Transact-SQL语句进行导入导出操作.在Transact-SQL语句中,我们主要使用OpenData ...

  5. Linux下oracle 数据库表空间、用户的创建,数据的导入导出操作指南

    Oracle新建用户并且将已有的数据dmp文件导入到新建的用户下的操作流程 1.切换到oracle用户下 su - oracle 2.登录sqlplus sqlplus /nolog 3.使用sysd ...

  6. mysql数据泵导入导出_ORACLE使用数据泵导入导出部分表

    1.导出名字为A的表(这里会导出SEQUENCES及FUNCTION等内容) expdp TEST/TEST@orcl schemas=TEST dumpfile=TEST.dmp DIRECTORY ...

  7. Sqoop安装部署和数据的导入导出案例详解

    目录 一.概述 二.Sqoop的工作机制 三.Sqoop1与Sqoop2架构对比 四.Sqoop安装部署 五.Sqoop的数据导入 1. 列举出所有的数据库 2. 准备表数据 3. 导入数据库表数据到 ...

  8. 使用Oracle 的 imp ,exp 命令实现数据的导入导出

    本文对Oracle数据的导入导出 imp ,exp 两个命令进行了介绍, 并对其相应的参数进行了说明,然后通过一些示例进行演练,加深理解. 文章最后对运用这两个命令可能出现的问题(如权限不够,不同or ...

  9. MATLAB中文件的读写和数据的导入导出

    http://blog.163.com/tawney_daylily/blog/static/13614643620111117853933/ 在编写一个程序时,经常需要从外部读入数据,或者将程序运行 ...

  10. oracle导出对象和数据,浅析Oracle对象和数据的导入导出

    对于Oracle对象和数据的导入导出,我们会用到一些小工具.以前我们一般都是使用PL/SQL Developer来实现,但是PL/SQL Developer在导出.导入数据时有两个问题: 1.要把表数 ...

最新文章

  1. 返回手势导致页面卡死并且UI错乱的问题解决
  2. Java并发编程:Thread类的使用
  3. 怎样才能提高搜索引擎对网站的信任度?
  4. php5.6的apaches的dll_Windows 10下 搭建Apache2.4、php5.6、mysql5.6
  5. 给HUSTOJ用户提供的源码阅读与修改建议
  6. 【干货】2020年人工智能十大技术进展及2021年十大技术趋势.pdf(附下载链接)...
  7. 矩池云上如何修改cuda版本
  8. 牛逼! IDEA 2020 要本土化,真的是全中文了!中国开发者话语权越来越大了
  9. MyEclipse—如何在MyEclipse中创建servlet3.0
  10. Axure Rp汉化安装
  11. [郝斌/王卓]数据结构C语句—链表
  12. Scrapy 抓取 当当图书2018畅销榜的所有图书
  13. 计算机通识之TCP/IP协议簇(二)
  14. PandoraBox潘多拉多线多播
  15. Java 获取主机ip地址(ipv4)
  16. 已成功与服务器建立连接,但是在登录前的握手期间发生错误
  17. Flask报错:sqlite3.IntegrityError: NOT NULL constraint failed: step. date
  18. 【vue+HT+flyTo】HT图扑软件中的flyTo应用
  19. oracle数据库报错:ORA-01654: 索引 aaa 无法通过 128 (在表空间 bb 中) 扩展
  20. 微信小程序:小程序服务器域名配置合法域名

热门文章

  1. C语言小项目----图书馆管理系统
  2. [转载]JXTA概念介绍
  3. JS中height、clientHeight、scrollHeight、offsetHeight区别
  4. Zabbix运维监控工具
  5. python下载快手视频教程_python如何下载快手视频
  6. java学生管理系统论文_(定稿)毕业论文基于java的学生信息管理系统设计报告(完整版)最新版...
  7. 推荐6款好用、免费的远程控制软件【远程管理工具】
  8. 收集整理近700套微信朋友圈h5小游戏源码合集大放送
  9. linkedin第三方登陆
  10. 设计模式--模板方法模式(12)