美文网首页
Hive中导入数据和导出数据

Hive中导入数据和导出数据

作者: Byte猫 | 来源:发表于2019-04-03 13:30 被阅读0次

    一、向Hive导入数据

    先在Hive里面创建好表,如下:

    hive> create table user
        > (id int, 
        > name string,
        > age int, 
        > tel string)
        > ROW FORMAT DELIMITED
        > FIELDS TERMINATED BY '\t'
        > STORED AS TEXTFILE;
    OK
    Time taken: 2.832 seconds
    

    1、从本地文件系统中导入数据到Hive表

    本地文件系统里面有个/home/bcat/user.txt文件,内容如下:

    [bcat@master ~]$ cat user.txt
    1       admin     25      13188888888888
    2       test      30      13888888888888
    

    user.txt文件中的数据列之间是使用\t分割的,可以通过下面的语句将这个文件里面的数据导入到user表里面,操作如下:

    hive> load data local inpath 'user.txt' into table user;
    Copying data from file:/home/bcat/user.txt
    Copying file: file:/home/bcat/user.txt
    Loading data to table default.user
    Table default.user stats:
    [num_partitions: 0, num_files: 1, num_rows: 0, total_size: 67]
    OK
    Time taken: 5.967 seconds
    

    这样就将user.txt里面的内容导入到user表里面去了.

    在Hive中,表的每一个分区对应表下的相应目录,所有分区的数据都是存储在对应 的目录中。假如order表有dt和city两个分区,则对应dt=20131218,city=BJ对应表的目录为/user/hive /warehouse/dt=20131218/city=BJ,所有属于这个分区的数据都存放在这个目录中。

    如果要导入某个分区,格式为

    hive> load data local inpath 'user.txt' into table user partition (dt='201803');
    

    2、HDFS上导入数据到Hive表

    从本地文件系统中将数据导入到Hive表的过程中,其实是先将数据临时复制到HDFS的一个目录下(典型的情况是复制到上传用户的HDFS home目录下,比如/home/bcat/),然后再将数据从那个临时目录下移动(注意,这里说的是移动,不是复制!)到对应的Hive表的数据目录里 面。
    Hive也支持将数据直接从HDFS上的一个目录移动到相应Hive表的数据目录下,假设文件已经存放在HDFS目录的/home/bcat/下,名为user2.txt,那么导入操作如下:

    hive> load data inpath '/home/bcat/user2.txt' into table user;
    Loading data to table default.user
    Table default.user stats:
    [num_partitions: 0, num_files: 2, num_rows: 0, total_size: 215]
    OK
    Time taken: 0.47 seconds
    

    注意"load data inpath ‘/home/bcat/user2.txt’ into table user;"里面是没有local这个单词的,这个是和第一种方法的区别。

    3、从别的表中查询出相应的数据并导入到Hive表中

    先建立一张新表

    hive> create table test(
        > id int, 
        > name string,
        > tel string)
        > partitioned by (age int)
        > ROW FORMAT DELIMITED
        > FIELDS TERMINATED BY '\t'
        > STORED AS TEXTFILE;
    

    这里使用了age作为分区字段。
    下面将user表中的查询结果并插入到test表中

    hive> insert into table test
        > partition (age='25')
        > select id, name, tel
        > from user;
    

    Hive也支持insert overwrite方式来插入数据,从字面就可以看出,overwrite是覆盖的意思,执行后相应数据目录下的数据将会被覆盖,而insert into则不会。
    另外,在Hive中可以把from放在最前面,insert语句写在后面生成多个不相交的输出。

    hive> from user
        > insert into table test
        > partition(age)
        > select id, name, tel, age
        > insert into table test2
        > select id, name
        > where age>25;
    

    4、在创建表的时候通过从别的表中查询出相应的记录并插入到所创建的表中

    在实际情况中,表的输出结果可能太多,不适于显示在控制台上,这时候,将Hive的查询输出结果直接存在一个新的表中是非常方便的,我们称这种情况为CTAS(create table .. as select)。CTAS操作是原子的,因此如果select查询由于某种原因而失败,新表是不会创建的。

    hive> create table test3
        > as
        > select id, name, tel
        > from user;
    

    5、用Sqoop 将Mysql数据表导入Hive中

    sqoop import --connect jdbc:mysql://localhost:3306/test --username root --password mysql-password --table t1 --hive-import
    

    二、从Hive导出数据

    1、Hive中数据导出到本地

    insert overwrite local directory "/home/bcat/1"
           select * from user;
    

    注意:一定要选择子文件夹,否则会覆盖目录,造成数据丢失

    2、Hive中数据导出到HDFS

    insert overwrite  directory 'hdfs://hadoop111:8020/user/hive/warehouse/1/'
            select * from user where id>7800;
    

    3、Bash shell覆盖追加导出

    $ bin/hive -e "select * from staff;"  > /home/bcat/backup.log
    

    4、用Sqoop 导出Hive数据到MySQL

    $ ./sqoop export --connect jdbc:mysql://localhost:3306/test --username root --password admin --table uv_info --export-dir /user/hive/warehouse/uv/dt=2011-08-03
    

    更多Sqoop用法

    相关文章

      网友评论

          本文标题:Hive中导入数据和导出数据

          本文链接:https://www.haomeiwen.com/subject/hkoabqtx.html