• Hive——DML数据操作(数据导入&数据导出)


    1. 数据导入

    1.1 向表中装载数据(Load)

    语法

    load data [local] inpath '数据的 path' [overwrite] into table  student [partition (partcol1=val1,)];      
    
    • 1

    (1) load data:表示加载数据
    (2) local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
    (3) inpath:表示加载数据的路径
    (4) overwrite:表示覆盖表中已有数据, 否则表示追加
    (5) into table:表示加载到哪张表(6) student:表示具体的表
    (7) partition:表示上传到指定分区

    实操案例:

    创建一张表

    create table student(id string, name string) 
    row format delimited fields terminated by '\t';  
    
    • 1
    • 2

    加载数据方式一:加载本地文件到 hive

    load data local inpath '/opt/module/hive/datas/student.txt' into table default.student;
    
    • 1

    加载数据方式二:加载HDFS数据到hive

    首先,将Linux系统的文件上传到HDFS根目录【可以自己选择路径】

    hadoop fs -put /opt/module/data/student.txt /
    
    • 1

    之后,将上传到HDFS的文件上传到hive

    load data inpath '/student.txt' into table student;
    
    • 1

    1.2 通过查询语句向表中插入数据(Insert)

    使用格式:

    insert into table new_table
    select field1,field2,,, from old_table;
    
    • 1
    • 2

    insert into :以追加数据的方式插入到表或分区,原有数据不会删除

    insert overwritetable new_table
    select field1,field2,,, from old_table;
    
    • 1
    • 2

    insert overwrite :会覆盖表中已存在的数据
    操作案例:

    创建一张表:

    create table student_par (id int, name string) 
    row format  delimited fields terminated by '\t';         
    
    • 1
    • 2

    基本模式插入(根据单张表查询结果)

    insert overwrite table student_par                        	
    select id, name from student where month='201709'; 
    
    • 1
    • 2

    1.3 查询语句中创建表并加载数据(As Select)

    了解

    根据查询结果创建表(查询的结果会添加到新创建的表中)

    create table if not exists student3                                       
    as select id, name from student;  
    
    • 1
    • 2

    1.4 创建表时通过 Location 指定加载数据路径

    使用Location指定路径时,数据不会存放在hive的默认路径

    使用格式

    create [external] table if not exists table_name(             		
    field type, field type)                                                             	
    row format delimited fields terminated by '\t'                	
    location 'path';
    
    • 1
    • 2
    • 3
    • 4

    准备工作:

    (1)上传数据到 hdfs 上

    hive (default)> dfs -mkdir /student;                                      
    hive (default)> dfs -put /opt/module/datas/student.txt /student;         
    
    • 1
    • 2

    (2)创建表,并指定在 hdfs 上的位置

    create external table if not exists student5(             		
    id int, name string)                                                             	
    row format delimited fields terminated by '\t'                	
    location '/student;  
    
    • 1
    • 2
    • 3
    • 4

    (3) 查询数据

     select * from student5;  
    
    • 1

    2. 数据导出

    2.1 Insert 导出

    基本格式:

    【直接使用案例体会语法】

    insert overwrite [local] directory                          		
    'path'                                    	
    查询语句; 
    
    • 1
    • 2
    • 3

    将查询的结果导出到本地

    insert overwrite local directory                          		
    '/opt/module/hive/data/export/student'                                    	
    select * from student; 
    
    • 1
    • 2
    • 3

    将查询的结果格式化导出到本地【在原来的基础上加一个导出格式限制】

    insert overwrite local directory                            		'/opt/module/hive/data/export/student1'                                   
    ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'                            	
    select * from student;      
    
    • 1
    • 2
    • 3

    将查询的结果导出到 HDFS 上(没有 local)

    insert overwrite directory '/user/atguigu/student2'       	
    ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'                 		
    select * from student;
    
    • 1
    • 2
    • 3

    2.2 Hadoop 命令导出到本地

    【需要指明导出的具体文件路径】

    dfs -get /user/hive/warehouse/student/student.txt         
    /opt/module/data/export/student3.txt;
    
    • 1
    • 2

    2.3 Hive Shell 命令导出

    基本语法:(hive -f/-e 执行语句或者脚本 > file)

    来到hive的根目录下操作:

    cd /opt/module/hive-3.1.2
    
    • 1
    bin/hive -e 'select * from default.student;' >  		
    /opt/module/hive/data/export/student4.txt
    
    • 1
    • 2

    提示>表示追加,>>表示覆盖

    2.4 Export 导出到 HDFS 上 & Import 数据到指定 Hive 表中

    使用语法:

    export table table_name
    to 'path';
    
    • 1
    • 2
    import table table_name
    from 'path';
    
    • 1
    • 2

    先用 export 导出后,再将数据导入。

    导入

     import table student2                                     
    from '/user/hive/warehouse/export/student';   
    
    • 1
    • 2

    导出:

    export table student                               
    to '/user/hive/warehouse/export/student';          
    
    • 1
    • 2

    使用场景
    export 和 import 主要用于两个 Hadoop 平台集群之间 Hive 表迁移。

    清除表中数据(Truncate):
    Truncate 只能删除管理表,不能删除外部表中数据

    truncate table table_name; 
    
    • 1
  • 相关阅读:
    单目标应用:火鹰优化算法(Fire Hawk Optimizer,FHO)求解微电网优化--提供MATLAB代码
    力扣hot100:146. LRU 缓存
    FFmpeg工具使用总结
    TypeScript(五)知识扩展
    33基于MATLAB的对RGB图像实现中值滤波,均值滤波,维纳滤波。程序已通过调试,可直接运行。
    three图形控制页面
    计算机毕业设计node+vue基于微信小程序的西餐外卖系统 uniapp 小程序
    香港第一金:美元美债依然是影响贵金属主要因素
    JFinal学习
    FCIS 2023网络安全创新大会:洞察前沿技术,探索安全新境界(附大会核心PPT下载)
  • 原文地址:https://blog.csdn.net/weixin_44606952/article/details/127874269