暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Hive往表写数据,你知道多少种方法?[一]

程序员实用技能 2021-07-22
1012

1. 使用insert......select......

语法:

    insert overwrite table dest_table partition(dt='xxxxxx')
    select
    c1,
    c2
    from
    src_table
    where
    ......

    select中的字段顺序与dest_table的一定要一致,字段名与dest_table的最好要一致。

    1. 映射时是以字段顺序为准,不以字段名或字段别名为准。

    2. insert overwrite是覆盖目的路径下已存在的数据文件。

    3. insert into是继续添加记录,该路径下之前的数据依然存在。

    4. 会自动添加新分区信息,会自动新建分区路径。



    2. 从本地导入数据文件:load data local inpath

    语法:

      hive> load data local inpath '/home/a/b/c/xxxx.txt' 
      overwrite into table dest_table 
      partition(dt='xxxxxx');

      实例:

        $ hive -e "load data local 
        inpath '/home/a/b/c/tb_v1.txt'
        into table test.tb_test
        partition(dt='2020-03-10') ";


        hive> dfs -du -h hdfs://d/test.db/tb_test/dt=2020-03-10 ;
        131 hdfs://d/test.db/tb_test/dt=2020-03-10/tb_v1.txt
        141 hdfs://d/test.db/tb_test/dt=2020-03-10/tb_v1_copy_1.txt -- 重命名的文件
        hive> select * from tb_test where dt = '2020-03-10';
        OK
        123 华为Mate10 31 999.0 20 2020-03-10
        456 华为Mate30 31 2999.0 30 2020-03-10
        789 小米5 31 800.0 20 2020-03-10
        1235 小米6 31 900.0 100 2020-03-10
        4562 OPPO Findx 31 3900.0 50 2020-03-10
        123 华为1010Mate10 31 999.0 20 2020-03-10
        -- 如下是第二次复制过来的5条记录
        456 华为3030Mate30 31 2999.0 30 2020-03-10
        789 小米5 31 800.0 20 2020-03-10
        1235 小米6 31 900.0 100 2020-03-10
        4562 OPPO Findx 31 3900.0 50 2020-03-10
        Time taken: 0.051 seconds, Fetched: 10 row(s)
        hive>
        1. 目的表要先存在,不然报错。

        2. 如果该分区路径和分区信息不存在,会自动新建分区路径和添加分区信息,不用add partition ,能直接查数。

        3. 这里的本地是指安装Hive的机器。

        4. 该命令相当于复制,把源文件复制到指定分区路径下,源文件依然存在。

        5. 导入的文件格式要与表的INPUTFORMAT格式相同才能查到数据,比如这里表的INPUTFORMAT是text格式   所以导入也需要txt格式的文件,不然查不到数。

        6. 用overwrite时,相当于覆盖,目的路径下已存在的所有文件和子目录全被移动到回收站,然后再复制。

        7. 用into时,相当于复制,目的路径下已存在的文件不变,如果存在同名文件,后面复制过来的文件会被重命名成新文件。

        8. 最好写上源文件的绝对路径。




        3.  从hdfs导入数据文件:load data  inpath ,不用写'local'

        语法:

          hive> load data inpath 'hdfs:....../part-r-00000001.lzo' 
          overwrite into table dest_table 
          partition(dt='xxxxxx');

          实例:

            hive> load data inpath 
            'hdfs://d/test.db/tb_test/dt=2020-03-08/tb_v2.txt'
            overwrite into table test.tb_test2
            partition(dt = '2020-03-08');


            日志:
            Loading data to table test.tb_test2 partition (dt=2020-03-08)
            Partition test.tb_test2{dt=2020-03-08} stats: [numFiles=1, numRows=0, totalSize=131, rawDataSize=0]
            OK
            Time taken: 0.767 seconds


            hive> dfs -du -h hdfs://d/test.db/tb_test/dt=2020-03-08 ;
            -- 原数据文件已不存在


            hive> dfs -du -h hdfs://d/test.db/tb_test2/dt=2020-03-08 ;
            131 hdfs://d/test.db/tb_test2/dt=2020-03-08/tb_v2.txt
            hive> select * from test.tb_test2 ;
            OK
            123 华为Mate10 31 999.0 20 2020-03-08
            456 华为Mate30 31 2999.0 30 2020-03-08
            789 小米5 31 800.0 20 2020-03-08
            1235 小米6 31 900.0 100 2020-03-08
            4562 OPPO Findx 31 3900.0 50 2020-03-08
            Time taken: 0.407 seconds, Fetched: 5 row(s)
            1. 这里是把hdfs上的文件移动(剪切)到目的分区的路径下,源文件已不存在。



            另外5种方法,请见第二篇(已发布)。历史文章:

            Hive常用命令你会哪些?

            Hive,load data你真的会用吗?附全面解析

            Hadoop,distcp你真的会用吗?附避坑指南


            「关注企业内的技术应用和使用技巧

            文章转载自程序员实用技能,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论