记录-mysql操作-crontab操作

一 mysql基本操作

show databases;
use [数据库名字]

show tables;
参考:https://www.cnblogs.com/dannyyao/p/6533567.html

查看建表语句
show create table t_xifan_user_login;

查看有哪些列
desc t_xifan_user_login;

限制显示行数
select * from t_xifan_user_login limit 10;

显示顺序 order by 默认升序(升序asc,降序desc)
select distinct f_day from t_xifan_user_login order by f_day desc;
select distinct f_act from t_duration_all order by f_act

join操作
https://www.cnblogs.com/fozero/p/6129537.html
以left join为例:
left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID).
B表记录不足的地方均为NULL.

in操作
SELECT * FROM Persons
WHERE LastName IN (‘Adams’,‘Carter’)

group by 分组统计数据的时候很方便
SELECT site_id, SUM(access_log.count) AS nums
FROM access_log GROUP BY site_id;
http://www.runoob.com/sql/sql-groupby.html

二 crontab

定时任务
crontab -l 查看已有crontab任务
crontab -e 编写crontab任务

示例一
0 */1 * * * source ~/.bash_profile; cd /home/liubingqi_rd/baidu/hpb-feed-vertical/offline-xifan/xifan_shangyang/xifan_rongzai/; sh run.sh >> /home/liubingqi_rd/baidu/hpb-feed-vertical/offline-xifan/xifan_shangyang/xifan_rongzai/crontab_log/task_log 2>&1

每小时执行一次,
source是用自己的配置,不加此句,可能会导致例如python版本的一些问题,缺少一些库的问题等。
cd切入脚本的目录,sh执行脚本,并把脚本输出输入到指定的文件中,作文log,
2>&1,对于& 1 更准确的说应该是文件描述符 1,而1标识标准输出,stdout。对于2 ,表示标准错误,stderr。2>&1 的意思就是将标准错误重定向到标准输出。

第一位需为0, 如果为* 还是每分钟执行一次,解释如下
https://blog.csdn.net/qq125293177/article/details/80434218

示例二
0 1 * * * source ~/.bash_profile; cd /home/liubingqi_rd/mysql_data/; sh group.sh >> /home/liubingqi_rd/mysql_data/crontab_log/task_log 2>&1

每天 1 点执行该脚本

更多用法:
https://www.jianshu.com/p/d93e2b177814

crontab任务示例:
打开 crontab -e
0 */1 * * * source ~/.bash_profile; cd /home/work/liubingqi/task20190227_user_tmp/; sh group.sh >> /home/work/liubingqi/task20190227_user_tmp/crontab_log/task_log 2>&1

shell示例

today=$(date +%Y%m%d)
echo "today:${today}"

current=$(date +%Y%m%d%H%M%S)
echo "current:${current}"


# get data from mysql
echo "get data from mysql"
mysql -h XXXXXX.com -Dsn_db -P 9030 -uXXXX -pXXXXXX --execute="select * from t_xifan_user_login where f_day='${today}'" -Ns > user_login_${today}

#why red?

echo "wait 60s"
sleep 60

# run python
echo "run python"
python exp_group.py ${today} >> log/${today}

echo "wait 60s"
sleep 60

#put data
echo "put data"
echo "show load where label = 'hour_t_xifan_user_exp_group_load${current}'"
curl --location-trusted -u XXXXXX:XXXXX -T t_xifan_user_exp_group_${today} http://XXXXXXXX:8030/api/sn_db/t_xifan_user_exp_group/_load?label=hour_t_xifan_user_exp_group_load${current}%26max_filter_ratio=0

echo "wait 20s"
sleep 20

echo "done"

其中的exp_group.py程序如下:

import sys

if __name__ == '__main__':

    count = 0

    #input = "user_login_20190212" + sys.argv[1]

    input = "user_login_" + sys.argv[1]
    output = "t_xifan_user_exp_group_" + sys.argv[1]

    f1 = open(output, 'a')

    for line in open(input):
        row = line.split('\t')
        cuid = row[5]

        total = 0

        for i in range(0, len(cuid)):
            if(cuid[i] == '|'):
                break
            if (cuid[i] >= '0' and cuid[i] <= '9'):
                total = total + int(cuid[i])

        if total == 0:
            continue
        sid = total % 10

        tmp_str = row[0] + '\t' + str(sid) +  '\t' + cuid + '\n'
        f1.write(tmp_str)

        count = count + 1
        if count % 1000 == 0:
            print count

    f1.close()
    print "done"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值