模拟步骤:
这边模拟实时算订销售总额,和订单量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | —创建演示的 ord _order 表 CREATE TABLE ord_order ( order_id INT NOT NULL AUTO_INCREMENT COMMENT ‘订单ID’ , amount INT NOT NULL DEFAULT 0 COMMENT ‘订单金额(分)’ , create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘创建时间’ , PRIMARY KEY ( order_id ) ) COMMENT = ‘订单表’ ; — 查看 当前日志所在位置 SHOW MASTER STATUS ; + — — — — — — — — — + — — — — — + — — — — — — — + — — — — — — — — — + — — — — — — — — — – + | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + — — — — — — — — — + — — — — — + — — — — — — — + — — — — — — — — — + — — — — — — — — — – + | mysql – bin . 000012 | 469 | | | | + — — — — — — — — — + — — — — — + — — — — — — — + — — — — — — — — — + — — — — — — — — — – + — 插入几笔订单 INSERT INTO ord_order ( amount ) VALUES ( 1000 ) , ( 2000 ) , ( 3000 ) ; — 查看 当前日志所在位置 SHOW MASTER STATUS ; + — — — — — — — — — + — — — — — + — — — — — — — + — — — — — — — — — + — — — — — — — — — – + | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + — — — — — — — — — + — — — — — + — — — — — — — + — — — — — — — — — + — — — — — — — — — – + | mysql – bin . 000012 | 712 | | | | + — — — — — — — — — + — — — — — + — — — — — — — + — — — — — — — — — + — — — — — — — — — – + |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #!/usr/bin/env python # -*- coding:utf-8 -*- from pymysqlreplication import BinLogStreamReader # 链接数据库的参数 因为 pymysqlreplication 底层使用的是 python-mysql mysql_settings = { ‘host’ : ‘192.168.1.233’ , ‘port’ : 3306 , ‘user’ : ‘HH’ , ‘passwd’ : ‘oracle’ } # 这 pymysqlreplication 的 server_id 和从 Binlog 的什么位置开始解析 stream = BinLogStreamReader ( connection_settings = mysql_settings , server_id = 100 , blocking = True , log_file = ‘mysql-bin.000012’ , log_pos = 469 ) # 初始化订单统计数据 order_count_total = 0 order_amount_total = 0 # 不停的解析 获取解析的 Binlog for binlogevent in stream : # 碰到 WriteRowsEvent 并且 表是 ord_order 则进行统计 if binlogevent . event_type == 30 and binlogevent . table == ‘ord_order’ : binlogevent . dump ( ) # 打印事件相关信息 # 同时计算出 订单数 和 金额数组, 如: [(1, 9), (1, 4)] stat = [ ( 1 , row [ ‘values’ ] [ ‘amount’ ] ) for row in binlogevent . rows ] # 分别获得 订单数数组如:[1, 1]。 销售额, 如: [9, 4] order_count , order_amount = zip ( * stat ) order_count_total += sum ( order_count ) order_amount_total += sum ( order_amount ) # 打印本次事件 产生的订单数和销售额 print ‘order_count:’ , order_count print ‘order_amount:’ , order_amount # 打印总的订单数和销售额 print ‘order_count_total:’ , order_count_total print ‘order_amount_total:’ , order_amount_total |
运行代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [ root @ centos7 tmp ] # python test.py === WriteRowsEvent === Date : 2016 – 11 – 16T17 : 11 : 11 Log position : 681 Event size : 54 Read bytes : 12 Table : test . ord_order Affected columns : 3 Changed rows : 3 Values : — ( ‘*’ , u ‘order_id’ , ‘:’ , 1 ) ( ‘*’ , u ‘amount’ , ‘:’ , 1000 ) ( ‘*’ , u ‘create_time’ , ‘:’ , datetime . datetime ( 2016 , 11 , 16 , 17 , 11 , 11 ) ) — ( ‘*’ , u ‘order_id’ , ‘:’ , 2 ) ( ‘*’ , u ‘amount’ , ‘:’ , 2000 ) ( ‘*’ , u ‘create_time’ , ‘:’ , datetime . datetime ( 2016 , 11 , 16 , 17 , 11 , 11 ) ) — ( ‘*’ , u ‘order_id’ , ‘:’ , 3 ) ( ‘*’ , u ‘amount’ , ‘:’ , 3000 ) ( ‘*’ , u ‘create_time’ , ‘:’ , datetime . datetime ( 2016 , 11 , 16 , 17 , 11 , 11 ) ) ( ) order_count : ( 1 , 1 , 1 ) order_amount : ( 1000 , 2000 , 3000 ) order_count_total : 3 order_amount_total : 6000 |
1 2 3 4 | ALTER TABLE ord_order ADD PARTITION ( PARTITION p201701 VALUES IN ( 201701 ) ) ; ALTER TABLE ord_order DROP PARTITION p201601 ; |
文章转载来自:trustauth.cn
上一篇:zabbix windows性能计数器使用详解(90)
下一篇:MySQL管理工具MySQL Utilities — 查询某个连接用户的信息(19)