在输出结果较多,需要输出到文件中时,可以在hive CLI之外执行hive -e "sql" > output.txt操作

但当SQL语句太长或太多时,这种方式不是很方便,可以考虑将SQL语句存为sql.hql文件中,然后执行 hive -f sql.hql >output.txt操作

如果是多个语句,且要输出到多个文件,只好把SQL写在shell脚本中,下面附一个例子

start_day=$
end_day=$
start_date=`date +"%Y-%m-%d" -d "${start_day}"`
end_date=`date +"%Y-%m-%d" -d "${end_day}"` active="
use ycappdata;
select ctl_dt,'active' ,count(distinct dvid) from sa_daydau_detail
where ctl_dt between '${start_date}' and '${end_date}'
group by ctl_dt,'active' ;" loss="
use ycappdata;
select date_add(from_unixtime(unix_timestamp(lastactivedate,'yyyy/MM/dd hh:mm:ss'),'yyyy-MM-dd'),),'loss' ,count(distinct deviceid) from ext_db_apploginstats
where from_unixtime(unix_timestamp(lastactivedate,'yyyy/MM/dd hh:mm:ss'),'yyyy-MM-dd') between date_sub('${start_date}',) and date_sub('${end_date}',)
group by date_add(from_unixtime(unix_timestamp(lastactivedate,'yyyy/MM/dd hh:mm:ss'),'yyyy-MM-dd'),),'loss';" active_month_distribute="
use ycappdata;
select a.ctl_dt,'active_month_distribute',concat('m',month(start_dt)),count(distinct b.dvid) from
(select ctl_dt,dvid from sa_daydau_detail where ctl_dt between '${start_date}' and '${end_date}')a
left outer join
(select start_dt,dvid from sa_firststartdate_dvid where start_dt between '2017-01-01' and '${end_date}')b
on lower(a.dvid)=lower(b.dvid)
group by a.ctl_dt,'active_month_distribute',concat('m',month(start_dt)) ;" active_date_distribute="
use ycappdata;
select a.ctl_dt,'active_date_distribute',
case when datediff(a.ctl_dt,b.start_dt)= then 'd0' when datediff(a.ctl_dt,b.start_dt)<= then 'd30'
when datediff(a.ctl_dt,b.start_dt)<= then 'd60' when datediff(a.ctl_dt,b.start_dt)<= then 'd90'
when datediff(a.ctl_dt,b.start_dt)<= then 'd120' when datediff(a.ctl_dt,b.start_dt)<= then 'd150'
when datediff(a.ctl_dt,b.start_dt)<= then 'd180' else 'd181' end,count(distinct b.dvid) from
(select ctl_dt,dvid from sa_daydau_detail where ctl_dt between '${start_date}' and '${end_date}')a
left outer join
(select start_dt,dvid from sa_firststartdate_dvid where start_dt between '2017-01-01' and '${end_date}')b
on lower(a.dvid)=lower(b.dvid)
group by a.ctl_dt,'active_date_distribute',case when datediff(a.ctl_dt,b.start_dt)= then 'd0' when datediff(a.ctl_dt,b.start_dt)<= then 'd30'
when datediff(a.ctl_dt,b.start_dt)<= then 'd60' when datediff(a.ctl_dt,b.start_dt)<= then 'd90'
when datediff(a.ctl_dt,b.start_dt)<= then 'd120' when datediff(a.ctl_dt,b.start_dt)<= then 'd150'
when datediff(a.ctl_dt,b.start_dt)<= then 'd180' else 'd181' end ;" hive -e "${active}" >> app_operate.txt
hive -e "${loss}" >> app_operate.txt
hive -e "${active_month_distribute}" >> app_operate.txt
hive -e "${active_date_distribute}" >> app_operate.txt while [ ${start_day} -le ${end_day} ]
do
current_date=`date +"%Y-%m-%d" -d "${start_day}"` week_active="
use ycappdata;
select '${current_date}','week_active',count(distinct dvid) from sa_daydau_detail
where ctl_dt between date_sub('${current_date}',pmod(datediff('${current_date}', '2017-01-02'), )) and '${current_date}'
group by '${current_date}','week_active'; " month_active="
use ycappdata;
select '${current_date}','month_active',count(distinct dvid) from sa_daydau_detail
where ctl_dt between trunc('${current_date}','MM') and '${current_date}'
group by '${current_date}','month_active'; " active_active_distribute="
use ycappdata;
select '${current_date}','active_active_distribute',concat('d',days),count(distinct ab.dvid) from
(select b.dvid,count(distinct b.ctl_dt) as days from
(select ctl_dt,dvid from sa_daydau_detail
where ctl_dt='${current_date}')a
join
(select ctl_dt,dvid from sa_daydau_detail
where ctl_dt between date_sub('${current_date}',) and '${current_date}')b
on lower(a.dvid)=lower(b.dvid)
group by b.dvid )ab
group by '${current_date}','active_active_distribute',concat('d',days);" newuser_retain="
use ycappdata;
select a.start_dt,'newuser_retain',concat('d',datediff(b.ctl_dt,a.start_dt)),count(distinct b.dvid) from
(select start_dt,dvid from sa_firststartdate_dvid
where start_dt between date_sub('${current_date}',) and '${current_date}')a
left outer join
(select ctl_dt,dvid from sa_daydau_detail
where ctl_dt between date_sub('${current_date}',) and '${current_date}')b
on lower(a.dvid)=lower(b.dvid)
group by a.start_dt,'newuser_retain',concat('d',datediff(b.ctl_dt,a.start_dt)); " active_retain="
use ycappdata;
select a.ctl_dt,'active_retain',concat('d',datediff(b.ctl_dt,a.ctl_dt)),count(distinct b.dvid) from
(select ctl_dt,dvid from sa_daydau_detail
where ctl_dt between date_sub('${current_date}',) and '${current_date}')a
left outer join
(select ctl_dt,dvid from sa_daydau_detail
where ctl_dt between date_sub('${current_date}',) and '${current_date}')b
on lower(a.dvid)=lower(b.dvid)
where a.ctl_dt<=b.ctl_dt
group by a.ctl_dt,'active_retain',concat('d',datediff(b.ctl_dt,a.ctl_dt)); " echo "${week_active}"
echo "${month_active}"
echo "${active_active_distribute}"
echo "${newuser_retain}"
echo "${active_retain}" hive -e "${week_active}" >> app_operate.txt
hive -e "${month_active}" >> app_operate.txt
hive -e "${active_active_distribute}" >> app_operate.txt
hive -e "${newuser_retain}" >> app_operate.txt
hive -e "${active_retain}" >> app_operate.txt
start_day=`date +"%Y%m%d" -d "${start_day} 1 days" `
done

hadoop之hive高级操作的更多相关文章

  1. Hadoop 上Hive 的操作

    数据dept表的准备: --创建dept表 CREATE TABLE dept( deptno int, dname string, loc string) ROW FORMAT DELIMITED ...

  2. 大数据技术之_08_Hive学习_04_压缩和存储(Hive高级)+ 企业级调优(Hive优化)

    第8章 压缩和存储(Hive高级)8.1 Hadoop源码编译支持Snappy压缩8.1.1 资源准备8.1.2 jar包安装8.1.3 编译源码8.2 Hadoop压缩配置8.2.1 MR支持的压缩 ...

  3. 初识Hadoop、Hive

    2016.10.13 20:28 很久没有写随笔了,自打小宝出生后就没有写过新的文章.数次来到博客园,想开始新的学习历程,总是被各种琐事中断.一方面确实是最近的项目工作比较忙,各个集群频繁地上线加多版 ...

  4. Hadoop之Hive篇

    想了解Hadoop整体结构及各框架角色建议飞入这篇文章,写的很好:http://www.open-open.com/lib/view/open1385685943484.html .以下文章是本人参考 ...

  5. 大数据技术生态圈形象比喻(Hadoop、Hive、Spark 关系)

    [摘要] 知乎上一篇很不错的科普文章,介绍大数据技术生态圈(Hadoop.Hive.Spark )的关系. 链接地址:https://www.zhihu.com/question/27974418 [ ...

  6. hadoop记录-hive常见设置

    分区表 set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict;create tabl ...

  7. hadoop安装hive及java调用hive

     1.安装hive 在安装hive前,请确保已经安装好了hadoop,如未安装,请参考centoos 安装hadoop集群进行安装: 1.1.下载,解压 下载hive2.1.1:http://mirr ...

  8. HIVE简单操作

    1.hive命令登录HIVE数据库后,执行show databases;命令可以看到hive数据库中有一个默认的default数据库. [root@hadoop hive]# hive Logging ...

  9. Hadoop生态圈-Hive快速入门篇之HQL的基础语法

    Hadoop生态圈-Hive快速入门篇之HQL的基础语法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客的重点是介绍Hive中常见的数据类型,DDL数据定义,DML数据操作 ...

随机推荐

  1. 全分布式的Hadoop初体验

    背景 之前的时间里对 Hadoop 的使用都是基于学长所搭建起的实验环境的,没有完整的自己部署和维护过,最近抽时间初体验了在集群环境下装机.配置.运行的全过程,梳理总结到本文中. 配置 内存:8G C ...

  2. ASP.Net Core 2.2使用SQLite数据库unable to open database file

    原文:ASP.Net Core 2.2使用SQLite数据库unable to open database file 最近把项目更新到了ASP.Net Core 2.2,发布之后发现在IIS下使用SQ ...

  3. oracle授权grant

    alter any cluster 修改任意簇的权限 alter any index 修改任意索引的权限 alter any role 修改任意角色的权限 alter any sequence 修改任 ...

  4. Information centric network (icn) node based on switch and network process using the node

    The present invention relates to an apparatus for supporting information centric networking. An info ...

  5. Matlab Tricks(十七)—— 使用 Latex

    >> set(text, 'Interpreter') 'none' 'tex' 'latex' % Matlab将返回'Interpreter'(解释器,对 text 文本的解释)所包含 ...

  6. Go语言的网络功能太强了,这么多项目。。。

    Centrifugo 是一个用 Golang 实现的基于 Websocket 或者 SockJS 的实时通信平台.https://www.oschina.net/p/centrifugalrpcx是一 ...

  7. wpf 绑定数据无法更新ui控件可能存在的问题

    BindingMode的枚举值有: ① OneWay ② TwoWay ③ OneTime:根据源端属性值设置目标属性值,之后的改变会被忽略,除非调用BindingExpression.UpdateT ...

  8. 【wpf】在win10系统上弹出toast和notification

    原文:[wpf]在win10系统上弹出toast和notification 老规矩,先看效果 右下角的notification: 操作中心的notification: 整体效果: 前提条件 1.需要在 ...

  9. DSP Builder 12.0安装及crack方法

    在安装dsp_builder之前请确保已安装所需要的matlab版本 在此之前我已经安装了matlab R2011a,下面安装dsp builder 下面就是破解了,因为12.0的版本刚出,还没有相应 ...

  10. Win8 Metro(C#)数字图像处理--2.43图像马赛克效果算法

    原文:Win8 Metro(C#)数字图像处理--2.43图像马赛克效果算法  [函数名称] 图像马赛克效果        MosaicProcess(WriteableBitmap src, i ...