zoukankan      html  css  js  c++  java
  • hive查询结果保存

    参考:

    https://blog.csdn.net/zhuce1986/article/details/39586189

    一、保存结果到本地

    方法1:调用hive标准输出,将查询结果写到指定的文件中

     

    这个方法最为常见,笔者也经常使用。sql的查询结果将直接保存到/tmp/out.txt中

    $ hive -e "select user, login_timestamp from user_login" > /tmp/out.txt

     

    当然我们也可以查询保存到某个文件file.sql中,按下面的方式执行查询,并保存结果

    $ hive -f test.sql > /tmp/out.txt

    cat test.sql

    select * from user_login

     

     

    方法2:使用INSERT OVERWRITE LOCAL DIRECTORY结果到本地

    hive> insert overwrite local directory "/tmp/out/"                                        

        > select user, login_time from user_login;

    上面的命令会将select user, login_time from user_login的查询结果保存到/tmp/out/本地目录下

    $ find /tmp/out/ -type f

    /tmp/out/.000000_0.crc

    /tmp/out/000000_0

    这两个文件存放的内容不一样,其中000000_0存放查询的结果,带有crc后缀的存放那个文件的crc32校验

    用vim打开查看下000000_0的内容:

    vim /tmp/out/000000_0

     1 user_1^A20140701

     2 user_2^A20140701

     3 user_2^A20140701

    可以看到,导出的查询结果字段之间是用^A(Ctrl+A)作为分割符,行与行之间用 作为分割

    默认的字段分割符有时候可能不太方便,幸好Hive提供了修改分割符号的方法,我们只要在导出时指定就可以了:

    hive> insert overwrite local directory "/tmp/out/"

        > row format delimited fields terminated by " " 

        > select user, login_time from user_login;

    可以看到字段分割符已经变成了tab(人眼看起来更舒服^-^)。

     

    二、保存结果到hdfs

    保存查询结果到hdfs很简单,使用INSERT OVERWRITE DIRECTORY就可以完成操作:

    hive> insert overwrite directory "/tmp/out/"

        > row format delimited fields terminated by " " 

        > select user, login_time from user_login;

    需要注意的是,跟保存到本地文件系统的差别是,保存到hdfs时命令不需要指定LOCAL项

     

    三、保存结果到HIVE表

    方法1、已经建好结果表,使用INSERT OVERWRITE TABLE以覆盖方式写入结果表

    如果结果表已经建好,可以使用INSERT OVERWRITE TABLE将结果写入结果表:

    hive> create table query_result 

        > as

        > select user, login_time from user_login;

     

    hive> select * from query_result;            

    OK

    user_120140701

    user_220140701

    user_320140701

     

    四、使用hdfs直接导出表

    Hive是构建在hdfs上的,因此,我们可以使用hdfs的命令hadoop dfs -get直接导出表。

    首先、我们先找到要导出的表存放到哪个目录下:

    hive> show create table user_login;

    OK

    CREATE  TABLE `user_login`(

      `user` string, 

      `login_time` bigint)

    ROW FORMAT SERDE 

      'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 

    STORED AS INPUTFORMAT 

      'org.apache.hadoop.mapred.TextInputFormat' 

    OUTPUTFORMAT 

      'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'

    <span style="color:#ff0000;">LOCATION

      'file:/user/hive/warehouse/test.db/user_login'</span>

    TBLPROPERTIES (

      'totalSize'='160', 

      'numRows'='10', 

      'rawDataSize'='150', 

      'COLUMN_STATS_ACCURATE'='true', 

      'numFiles'='1', 

      'transient_lastDdlTime'='1411544983')

    Time taken: 0.174 seconds, Fetched: 18 row(s)

    可以看到,user_login表存放到在file:/user/hive/warehouse/test.db/user_login

    接下来,直接利用hadoop dfs -get导出到本地:

    hadoop dfs -get file:/user/hive/warehouse/test.db/user_login  /tmp/out/


    第一种,在bash中直接通过hive -e命令,并用 > 输出流把执行结果输出到制定文件hive -e "select * from student where sex = '男'" > /tmp/output.txt 
    第二种,在bash中直接通过hive -f命令,执行文件中一条或者多条sql语句。并用 > 输出流把执行结果输出到制定文件 
    hive -f exer.sql  > /tmp/output.txt
    文件内容select * from student where sex = '男';select count(*) from student; 
    第三种,在hive中输入hive-sql语句,通过使用INSERT OVERWRITE LOCAL DIRECTORY结果到本地系统和HDFS文件系统语法一致,只是路径不同
    insert overwrite local directory "/tmp/out" > select cno,avg(grade) from sc group by(cno); 
    insert overwrite directory 'hdfs://server71:9000/user/hive/warehouse/mystudent'select * from student1; 
    以上是三种,包含了3执行hive-sql的方法。结果保存到本地的方法前两种都属于linxu BASH自带的方法。第三种才是HIVE本身的导出数据的方法。 
    第四种,就是基本的SQL语法,从一个表格中抽取数据,直接插入另外一个表格。参考SQL语法即可。insert overwrite table student3 select sno,sname,sex,sage,sdept from student3 where year='1996'; http://blog.csdn.net/zhuce1986/article/details/39586189

    一、保存结果到本地方法1:调用hive标准输出,将查询结果写到指定的文件中
    这个方法最为常见,笔者也经常使用。sql的查询结果将直接保存到/tmp/out.txt中$ hive -e "select user, login_timestamp from user_login" > /tmp/out.txt
    当然我们也可以查询保存到某个文件file.sql中,按下面的方式执行查询,并保存结果$ hive -f test.sql > /tmp/out.txtcat test.sqlselect * from user_login

    方法2:使用INSERT OVERWRITE LOCAL DIRECTORY结果到本地hive> insert overwrite local directory "/tmp/out/"                                            > select user, login_time from user_login;上面的命令会将select user, login_time from user_login的查询结果保存到/tmp/out/本地目录下$ find /tmp/out/ -type f/tmp/out/.000000_0.crc/tmp/out/000000_0这两个文件存放的内容不一样,其中000000_0存放查询的结果,带有crc后缀的存放那个文件的crc32校验用vim打开查看下000000_0的内容:vim /tmp/out/000000_0 1 user_1^A20140701 2 user_2^A20140701 3 user_2^A20140701可以看到,导出的查询结果字段之间是用^A(Ctrl+A)作为分割符,行与行之间用 作为分割默认的字段分割符有时候可能不太方便,幸好Hive提供了修改分割符号的方法,我们只要在导出时指定就可以了:hive> insert overwrite local directory "/tmp/out/"    > row format delimited fields terminated by " "     > select user, login_time from user_login;可以看到字段分割符已经变成了tab(人眼看起来更舒服^-^)。
    二、保存结果到hdfs保存查询结果到hdfs很简单,使用INSERT OVERWRITE DIRECTORY就可以完成操作:hive> insert overwrite directory "/tmp/out/"    > row format delimited fields terminated by " "     > select user, login_time from user_login;需要注意的是,跟保存到本地文件系统的差别是,保存到hdfs时命令不需要指定LOCAL项
    三、保存结果到HIVE表方法1、已经建好结果表,使用INSERT OVERWRITE TABLE以覆盖方式写入结果表如果结果表已经建好,可以使用INSERT OVERWRITE TABLE将结果写入结果表:hive> create table query_result     > as    > select user, login_time from user_login;
    hive> select * from query_result;            OKuser_120140701user_220140701user_320140701
    四、使用hdfs直接导出表Hive是构建在hdfs上的,因此,我们可以使用hdfs的命令hadoop dfs -get直接导出表。首先、我们先找到要导出的表存放到哪个目录下:hive> show create table user_login;OKCREATE  TABLE `user_login`(  `user` string,   `login_time` bigint)ROW FORMAT SERDE   'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' STORED AS INPUTFORMAT   'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT   'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'<span style="color:#ff0000;">LOCATION  'file:/user/hive/warehouse/test.db/user_login'</span>TBLPROPERTIES (  'totalSize'='160',   'numRows'='10',   'rawDataSize'='150',   'COLUMN_STATS_ACCURATE'='true',   'numFiles'='1',   'transient_lastDdlTime'='1411544983')Time taken: 0.174 seconds, Fetched: 18 row(s)可以看到,user_login表存放到在file:/user/hive/warehouse/test.db/user_login接下来,直接利用hadoop dfs -get导出到本地:hadoop dfs -get file:/user/hive/warehouse/test.db/user_login  /tmp/out/

  • 相关阅读:
    C++类中使用new及delete小例子(续)
    C++类中使用new及delete小例子
    C++类中static修饰的函数的使用
    C++类使用static小例子(新手学习C++)
    C++结构体中使用函数与类中使用函数小结
    记一次简单的性能优化
    [转载]Java的内存回收机制
    写给自己的项目总结
    [转载]正则表达式30分钟入门教程
    使用JRockit进行性能优化一:环境搭建
  • 原文地址:https://www.cnblogs.com/hongfeng2019/p/11460714.html
Copyright © 2011-2022 走看看