在输出结果较多,需要输出到文件中时,可以在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. 如何将字段中带逗号的SQLite数据库数据导入到MySQL

    以前在数据库导入中没有遇到过什么问题,如下这样导入 load data local infile 'D:\data.csv' into table table1 fields terminated b ...

  2. Shell Step by Step (3) —— Stdin &amp; if

    4.输入输出 #! /bin/bash # Read users input and then get his name read -p "Please input your first n ...

  3. Android的PVPlayer介绍

    1 Player的组成 OpenCore的Player的编译文件是pvplayer/Android.mk,将生成动态库文件 libopencoreplayer.so.这个库包括了双方面的内容:一方是P ...

  4. python 整数中1出现的次数

    把整数转换为字符串 用count计数 # -*- coding:utf- -*- class Solution: def NumberOf1Between1AndN_Solution(self, n) ...

  5. WPF-- 合并资源字典

    原文:WPF-- 合并资源字典 1.        合并多个外部资源字典成为本地字典   语言 XAML 示例代码 <Page.Resources>   <ResourceDicti ...

  6. 工作流管理平台Airflow

    Airflow 1. 引言 Airflow是Airbnb开源的一个用Python写就的工作流管理平台(workflow management platform).在前一篇文章中,介绍了如何用Cront ...

  7. iOS 5.1.1 设备不能安装AdHoc问题版本号

    之前苹果更新了审计规范,要求必须支持64通过苹果的审核权限位架构的应用.     但运营商表示反馈.使用iOS5.1.1该系统无法安装我们的包Adhoc版本号.     认为非常莫名.由于我们在Dep ...

  8. WPF_界面_图片/界面/文字模糊解决之道整理

    原文:WPF_界面_图片/界面/文字模糊解决之道整理 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010265681/article/detai ...

  9. rc-form(翻译)

    原地址:https://npm.taobao.org/package/rc-form rc-form React 高阶表单控制组件.       开发 npm install npm start op ...

  10. WPF中的 Layout To Layout

    原文:WPF中的 Layout To Layout   WPF中的 Layout To Layout                             周银辉 WPF的布局功能异常强大,当有时我 ...