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)作为分割符,行与行之间用\n作为分割
默认的字段分割符有时候可能不太方便,幸好Hive提供了修改分割符号的方法,我们只要在导出时指定就可以了:
hive> insert overwrite local directory "/tmp/out/"
> row format delimited fields terminated by "\t"
> 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 "\t"
> 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)作为分割符,行与行之间用\n作为分割默认的字段分割符有时候可能不太方便,幸好Hive提供了修改分割符号的方法,我们只要在导出时指定就可以了:hive> insert overwrite local directory "/tmp/out/" > row format delimited fields terminated by "\t" > 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 "\t" > 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/
hive查询结果保存的更多相关文章
- 使用shell+awk完成Hive查询结果格式化输出
好久不写,一方面是工作原因,有些东西没发直接发,另外的也是习惯给丢了,内因所致.今天是个好日子,走起! btw,实际上这种格式化输出应该不只限于某一种需求,差不多是通用的. 需求: --基本的:当前H ...
- Hive学习笔记——保存select结果,Join,多重插入
1. 保存select查询结果的几种方式: 1.将查询结果保存到一张新的hive表中 create table t_tmp as select * from t_p; 2.将查询结果保存到一张已经存在 ...
- hive查询不加分区的一个异常
今天下午有同事反馈她提交了了一个SQL后,hive 查询就停止响应了. 我看了下,发现hiveserver确实hug住了.听过查看日志,发现了一个牛逼的SQL, 这个SQL很简单: select a. ...
- hive查询遇到java.io.EOFException: Unexpected end of input stream错误
hive查询遇到java.io.EOFException: Unexpected end of input stream错误 原因基本上有两个: 空文件 不完整的文件 解决办法: 删除对应文件- 参考 ...
- SQL 查询结果保存为 临时表
-- 1. 在使用select into前,可以先做一下判断 if OBJECT_ID('tempdb..#TT')is not NULL drop table #TT -- 2. 查询结果保存为临时 ...
- Entity Framework异步查询和保存
EF6开始提供了通过async和await关键字实现异步查询和保存的支持(.net 4.5及更高版本).虽然不是所有的操作都能从异步中获益,但是耗时的操作.网络或IO密集型任务中,使用异步可以提升客户 ...
- hive查询ncdc天气数据
使用hive查询ncdc天气数据 在hive中将ncdc天气数据导入,然后执行查询shell,可以让hive自动生成mapredjob,快速去的想要的数据结果. 1. 在hive中创建ncdc表,这个 ...
- hive查询语句入门(hive DDL)
hive DDL 启动hadoop /apps/hadoop/sbin/start-all.sh 开启MySQL库,用于存放hive的元数据 sudo service mysql start 启动hi ...
- 求解:为什么impala实现hive查询 可以使用ifnull()函数,不可以使用length() 函数
求大神解惑,找了很久都没有找到为什么??? hive支持length() 函数,不支持ifnull()函数??? impala实现hive查询 支持ifnull()函数,不支持length() 函数 ...
随机推荐
- 关于在react里面es6构建类的时候,一些开始的问题
一般来说我们写react代码,个人习惯 这个里面没有constructor和super的,这样写也没啥问题.因为他会默认加上 但是有的时候有人会加上这两个 可以不写constructor,一旦写了co ...
- document.writeln绑定数据 --点击跳转添加样式
document.writeln(" "); document.writeln(" "); document.writeln(" "); d ...
- head first 设计模式笔记6-命令模式
命令模式:将“请求”封装成对象,以便使用不同的请求.队列或者日志来参数化其他对象.命令模式也支持可撤销的操作.这个模式允许我们将动作封装成命令对象,然后可以传递和调用. 1)命令模式将发出请求的对象和 ...
- CF D. Number Of Permutations 排列
挺水的一道题~ 拿全排列随便乘一下就好了. #include <cstdio> #include <algorithm> #define N 300004 #define ll ...
- TTTTTTTTTTTTTTTTT CF #182 div1 B floyd
题意: 有 n(3≤n≤100) 个站点,当第一次到达站点 u 的时候会增加寿命 au(1≤au≤103),题目给了 n 个站点的二位空间坐标,每两个站点之间的距离为曼哈顿距离(dis(i, j)=| ...
- 智能指针之shared_ptr基本概述
1.shared_ptr允许有多个指针指向同一个对象,unique_ptr独占所指向的对象. 2.类似于vector,智能指针也是模板.创建智能指针: shared_ptr<string> ...
- Spark译文(三)
Structured Streaming Programming Guide(结构化流编程指南) Overview(概貌) ·Structured Streaming是一种基于Spark SQL引擎的 ...
- Python中的不可变对象类型与可变对象类型
https://blog.csdn.net/answer3lin/article/details/86430074 其实各个标准资料中没有说明Python有值类型和引用类型的分类,这个分类一般是C++ ...
- Atcoder ARC101 E 树dp
https://arc101.contest.atcoder.jp/tasks/arc101_c 题解是也是dp,好像是容斥做的,但是看不懂,而且也好像没讲怎么变n^2,看了写大佬的代码,自己理解了一 ...
- gojs常用API
操作类API: 添加节点: myDiagram.model.addNodeData(node); var node = {}; node["key"] = "节点Key& ...