• 数据的导入和导出


    数据加载到表

    非分区表

    方式1:
    假设当前已经存在一张非分区表,那么可以直接通过拷贝的方式把数据拷贝到hdfs上面的对应目录
    eg

    1、创建测试表
    hive (r_db2)> create table input_test(id int, name string);
    2、创建测试数据
    [hduser@yjt test8.out]$ cat 000000_1   # 这个数据之间的分隔符^A或者1
    1hadoop
    2spark
    3、put数据
    [hduser@yjt test8.out]$ hadoop fs -put 000000_1 /hive/warehouse/r_db2.db/input_test
    4、查询数据
    hive (r_db2)> select * from input_test;
    OK
    input_test.id	input_test.name
    1	hadoop
    2	spark
    

    方式2:
    与方式1类似,其实底层都是hive进行数据的拷贝

    表创建好以后,直接使用   load data [local] inpath "path" [overwrite] into table tblname;
    

    方式3:
    使用insert方式添加

    方式4:
    通过其他表的数据插入到当前表

    hive (r_db2)> truncate table input_test;   # 清空表数据
    hive (r_db2)> insert into table input_test select s.id, s.name from test3;   # 通过其他表填充当前表
    或者
    hive (r_db2)> from test3 as s insert into table input_test select s.id, s.name;
    

    分区表

    分区表与非分区表类似
    eg

    方式1:

    1、创建分区表
    hive (r_db2)> create table input_test1(id int, name string) partitioned by(longtime string) row format delimited fields terminated by ',';
    2、创建hdfs目录
    hive (r_db2)> dfs -mkdir "/hive/warehouse/r_db2.db/input_test1/2020-06-29";
    3、put数据
    [hduser@yjt hive]$ hadoop fs -put test.txt /hive/warehouse/r_db2.db/input_test1/2020-06-29
    4、修复元数据
    hive (r_db2)> msck repair table input_test1;  # 或者使用alter添加对应的分区
    
    

    方式2:

    hive (r_db2)> load data local inpath "/tmp/hive/test.txt" into table input_test1 partition(longtime='2002-06-29');
    

    方式3:

    hive (r_db2)> insert into table input_test1 partition(longtime="2021-06-29") values(1, 'test');
    

    方式4:

    hive (r_db2)> from test3 as s insert into table input_test1  partition(longtime)  select s.id,s.name, s.currentdate;
    

    数据导出

    导出到本地

    hive (r_db2)> insert overwrite local directory "/tmp/hive/out" select * from input_test1;
    

    导出到hdfs

    hive (r_db2)> insert overwrite directory "/tmp/hive/out" select * from input_test1;
    
  • 相关阅读:
    关于前端基础框架的思考和尝试
    通过当前IP获取当前网卡的MAC地址
    shell及脚本2——shell 环境及命令
    shell及脚本1——变量
    linux显示git commit id,同时解决insmod模块时版本不一致导致无法加载问题
    大于16MB的QSPI存放程序引起的ZYNQ重启风险
    insmod模块的几种常见错误
    shell及脚本3——正则表达式
    修改/etc/profile和/etc/environment导致图形界面无法登陆的问题
    Sql 2008的merge关键字
  • 原文地址:https://www.cnblogs.com/yjt1993/p/13214908.html
Copyright © 2020-2023  润新知