一、背景
很多需求是日常用sql手工执行出结果的,有的需求需要周期性去出数据,每天执行固定的sql比较繁琐;设计一个模块就是直接通过sql,生成excel附件并且发送邮件,提供工作效率。
二、数据库设计
两个表组成,邮件发送设置表,sql和excel的sheet的对应关系
一个需求可能需要好几个sql才能产生结果,可以写到同一个sheet也可以写到不同的sheet,自由组合。
建表脚本
drop index idx_exec_sql_id on usys_getsqldata_sendmail;drop table if exists usys_getsqldata_sendmail;/*==============================================================*//* Table: usys_getsqldata_sendmail *//*==============================================================*/create table usys_getsqldata_sendmail(mail_id bigint not null auto_increment comment '编码',cycle_id char(1) not null comment '周期 D W M',exec_day smallint not null comment '执行日期:周期D = -1 W =周几 M=几号',send_type_id smallint not null comment '邮件发送类型:1 附件 2 动态内容 3 静态内容',exec_sql_id bigint not null comment '执行sql编码:无sql -1',excel_path varchar(100) comment '生成excel路径',excel_template varchar(100) comment '模板',to_users varchar(2500) not null comment '收信人 多个人用空格',sto_users varchar(2500) comment '抄送 多个人用空格',mail_subject varchar(255) not null comment '邮件主题',mail_content text comment '邮件内容',state_id smallint not null default 1 comment '记录状态1正常 0不正常',data_date int not null comment '数据日期',creater_name varchar(30) not null comment '创建人',modifier_name varchar(30) not null comment '修改人',gmt_created datetime not null default '1700-01-01 00:00:00' comment '创建时间',gmt_modified datetime not null default '1700-01-01 00:00:00' comment '修改时间',primary key (mail_id));alter table usys_getsqldata_sendmail c/*==============================================================*//* Index: idx_exec_sql_id *//*==============================================================*/create index idx_exec_sql_id on usys_getsqldata_sendmail(exec_sql_id);dmail(exec_sql_id);drop index idx_exec_sql_id on usys_exec_sql_defined;d/*==============================================================*//* Table: usys_exec_sql_defined *//*==============================================================*/create table usys_exec_sql_defined(id bigint not null auto_increment comment '编码',exec_sql_id bigint not null comment 'sql 组编码',step_id int not null comment '步骤',db_id int not null comment '数据库编码',start_sql text comment '预处理sql',getdata_sql text not null comment '出结果sql',end_sql text comment '清理sql',rsheet_name varchar(255) not null comment '生成excel 的sheet名',tsheet_name varchar(255) not null comment '模板的 sheet名',start_position varchar(10) not null comment '写excel单元格位置',gmt_created datetime not null default '1700-01-01 00:00:00' comment '创建时间',gmt_modified datetime not null default '1700-01-01 00:00:00' comment '修改时间',primary key (id)/*==============================================================*//* Index: idx_exec_sql_id *//*==============================================================*/create index idx_exec_sql_id on usys_exec_sql_defined(exec_sql_id=========================*/create index idx_exec_sql_id on usys_exec_sql_defined(exec_sql_id););
需要配置的表:usys_getsqldata_sendmail
mail_id 编码 调用时候需要传输这个id
cycle_id 周期 D W M 这个邮件的发送周期
exec_day 执行日期:周期D = -1 W =周几 M=几号 跟周期有关系
send_type_id 邮件发送类型:1 附件 2 动态内容 3 静态内容 目前支持excel附件 静态内容
exec_sql_id 执行sql编码:无sql -1 静态内容的时候是-1
excel_path 生成excel路径 配置excel生成的路径
excel_template 模板 配置excel的模板
to_users 收信人 多个人用空格
sto_users 抄送 多个人用空格
mail_subject 邮件主题
mail_content 邮件内容
state_id 记录状态1正常 0不正常
data_date 数据日期
creater_name 创建人
modifier_name 修改人
gmt_created 创建时间
gmt_modified 修改时间
需要配置的表 usys_exec_sql_defined
id 编码 主键,其他没有用处
exec_sql_id sql 组编码 关联usys_getsqldata_sendmail
step_id 步骤 一个邮件需要包含多个sheet的时候这个需要往下增
db_id 数据库编码 执行sql连接的数据库对应ganglia.usys_database_info
start_sql 预处理sql 有些sql比较复杂,需要做预处理
getdata_sql 出结果sql
end_sql 清理sql start_sql产生的临时表等可以在这里清除
rsheet_name 生成excel 的sheet名
tsheet_name 模板的 sheet名
start_position 写excel单元格位置
gmt_created 创建时间
gmt_modified 修改时间
三、模块设计
通过kettle来完成两个表的循环取数,写数,程序入口 见j_send_data_bymail_main.kjb
两个主要的流程设计如下:
四、调用
调用入口,并且传递邮件id参数
kitchen.sh j_send_data_bymail_main.kjb -param:mail_id=1