通过crontab命令,我们可以按照固定的时间间隔,来执行shell脚本。本次我们利用sqoop数据迁移这个例子,来体现crontab的实现过程。
一、数据库、表的准备工作
1.新建一个 MySQL 的数据库
create database test;
2.新建一张 MySQL 表
create table cron_test(
id int auto_increment primary key,
name varchar(20),
regTime timestamp
);
3.向 MySQL 表中导入数据
insert into cron_test(name) values('xiaoming'),('angela'),('huangbo'),('xuzhen');
4.新建一个 Hbase 数据库
create_namespace 'test'
5.新建一张 Hbase 表
create 'test:hb_cron_test','info'
6.新建一张 Hive 外部表,映射 Hbase 中 test:hb_cron_test
create external table hv_cron_test(
id int,
name string,
regTime timestamp
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties ("hbase.columns.mapping" = ":key,info:name,info:regTime")
tblproperties ( "hbase.table.name" = "test:hb_cron_test");
二、Sqoop命令编辑
1.从 MySQL 向 Hbase 传输数据
sqoop import \
--connect jdbc:mysql://single:3306/test \
--username root \
--password root \
--table cron_test \
--columns id,name,regTime \
--hbase-table test:hb_cron_test \
--hbase-row-key id \
--column-family info \
--incremental append \
--check-column id \
--last-value 0
2.创建 sqoop job
sqoop job --create mhhjob -- import \
--connect jdbc:mysql://single:3306/test \
--username root \
--password root \
--table cron_test \
--columns id,name,regTime \
--hbase-table test:hb_cron_test \
--hbase-row-key id \
--column-family info \
--incremental append \
--check-column id \
--last-value 4
3.sqoop 部分命令
#创建job
sqoop job --create JOB_NAME
#罗列出所有job
sqoop job -list
#执行job
sqoop job -exec JOB_NAME
#显示指定job
sqoop job -show JOB_NAME
#删除指定job
sqoop job -delete JOB_NAME
三、编辑 shell 脚本
1.编辑时间脚本
#创建对应文件夹并编辑
mkdir crontab
vi crontab/mhhjob.sh
#讲下方代码贴入 mhhjob.sh 中
#!/bin/bash
source /ect/profile
function now(){
echo $1" mhhjob at "` date +"%Y-%m-%d %H:%M:%S"`
}
now start
sqoop job -exec mhhjob
now end
2.查看 crond 状态
systemctl status crond #查看状态
systemctl start crond #开启服务
systemctl stop crond #关闭服务
systemctl disable crond #禁用服务
3.创建crontab的配置文件
mkdir crontab
vi crontab/mhh.log
#讲下方代码贴入 mhh.log 中
*/1 * * * * bash /root/crontab/mhhjob.sh
四、crontab指令
1.创建并挂载crontab任务
crontab mhh.log
2.查看crontab任务
crontab -l mhh.log
3.删除crontab任务
crontab -r mhh.log
4.实时查看任务执行情况
tail -f /var/log/cron
五、新增数据
通过上述4个主要步骤,我们已经大致构建好了 crontab 的执行环境,下面我们通过向 MySQL 表中新增数据,再来回顾一下。
insert into cron_test(name) values('chengcheng'),('anren'),('qiuqianchi'),('morofeng');
如果 Hive 表、Hbase 表中出现了新增数据,则表明你的 contab 脚本命令时成功的。
优化
一、crontab时间设置相关笔记
# crontab [ -u user ] file
# crontab [ -u user ] { -l | -r | -e }
-u:表示设定谁的时间表,默认设定自己的时间表,只有root才有权限设定别人的时间表
-e:执行文字编辑器来设定时程表,内定的文字编辑器是VI,如果你想用别的文字编辑器,则请先设定VISUAI环境变数来指定使用那个文字编辑器(比如说setenvVISUAL joe)
-r:删除目前的时程表
-l:列出目前的时程表
* * * * *
- - - - -
| | | | |
| | | | +------- 星期中星期几(0-7)(星期天为0)
| | | +----------- 月份(1-12)
| | +--------------- 一个月中的第几天(1-31)
| +------------------- 小时(0-23)
+----------------------- 分钟(0-59)
*/1 * * * * /bin/ls #每分钟执行一次/bin/ ls
0 6-12/3 * 12 * /usr/bin/backup #在12月内,每天的早上6点到12点,每隔3个小时0分钟执行一次/usr/bin/backup
#查看crontab服务状态
service crond status
systemctl restart crond
tail -f /lvar/ log/ cron #执行日志查看看
二、sqoop “记住密码”设置
输入命令:cd /opt/software/hadoop/sqoop146/conf/
,进到conf目录下,找到sqoop-site.xml这个文件,进入配置文件后找到下图指示的地方,将两个注释符号删除即可。