redporter
redporter 一个旨在从 MySQL 和文本文件读取数据缓存到 Redis 的命令行工具,支持断点续传,其目的不在于快速迁移数据,而是为了辅助数据清洗工作,借用 redporter 可以把多种来源的数据读取缓存到 Redis 中,方便清洗脚本的并发运行
安装
$ git clone https://github.com/Zhiwei1996/redporter && cd redporter
$ python setup.py install
使用
任务配置
reader: 读取数据类型 [mysql|json|file]
redis: redis 连接配置
host:
port:
db:
password:
key: 任务名称
queue_key_prefix: 数据队列名前称缀,留空则默认为porter.queue.
cache_key_prefix: 缓存名前称缀,留空则默认 porter.cache.
mysql: mysql 连接配置
host:
port:
user:
password:
db:
table:
pk: 表的主键,留空默认 id
column: 需要上传的字段名,留空默认选取全部字段
append_db_info: 可选,是否同时上传库名表名信息,true OR false,默认 false
appendices: 可选,指定附加字段数据上传,详细配置见模板文件
file:
path: 文件路径
delimiter: 文件分隔符
header: true: 使用 header 拼接为 json 格式数据; false: 不处理上传整条数据
appendices: 可选,指定附加字段数据上传,详细配置见模板文件
json:
path: 文件路径
appendices: 可选,指定附加字段数据上传,详细配置见模板文件
MySQL 读取配置样例
---
reader: mysql
redis:
host: 127.0.0.1
port: 6379
db: 0
password: 123456
key: TEST
queue_key_prefix: redporter.queue.
cache_key_prefix: redporter.cache.
mysql:
host: 127.0.0.1
port: 3306
user: root
password: 123456
db: db_test
table: t_test
pk: id
column:
sharding: 0
# append_db_info: true
# appendices:
# - field_name:"hello world"
JSON 读取配置样例
---
reader: json
...
json:
path: /pathto/data/test.json
# appendices:
# - field_name:"hello world"
CSV 文件读取配置样例
---
reader: file
...
file:
path: /pathto/data/test.csv
delimiter: ","
header: True
# appendices:
# - field_name:"hello world"
无头文件读取配置样例(上传原始数据,不做 json 拼接处理)
---
reader: file
...
file:
path: /pathto/data/test.csv
delimiter:
header: false
# appendices:
# - "hello world"
任务命令
$ redporter -h
Usage: redporter [OPTIONS] [sync|monitor|clear|new]
A command line tool for extracting data and load into Redis.
Options:
-V, --version Show the version and exit.
-f, --config-file PATH Task config file
-l, --limit INTEGER Limit of each reading from data source
[default: 1000]
--limit-scale INTEGER The maximum lengeth of redis queue is (limit
* scale) [default: 3]
--blocking / -B, --no-blocking Enable blocking mode [default: True]
-t, --time-sleep INTEGER Time to wait when up to the maximum limit of
queue [default: 10]
-C, --clean-type [status|queue|all]
Type of redis cache
-T, --task-type [mysql|json|file|csv]
Type of task template
-o, --output-task-file PATH Save task template into file
-v, --verbose Print debug information
--debug-file PATH File to be used as a stream for DEBUG
logging
-h, --help Show this message and exit.
sync 同步数据
monitor 监控任务状态
clear 清空任务状态
new 创建新的任务模板
每次读取 100 条数据并上传,超过最大限制数量则停止读取
$ redporter sync -f task_template.yaml -l 100
读取全部数据并上传
$ redporter sync -f task_template.yaml --no-blocking
打印详细日志
$ redporter sync -f task_template.yaml -l 100 -v --debug-file /tmp/redporter.log
查看任务进度
$ redporter monitor -f task_template.yaml
{
"db": "db_test",
"table": "t_test",
"count": 61,
"page": 6,
"record": {xxx}
}
清空缓存数据
all 所有
status 任务状态
queue 队列数据
$ redporter clear -f task_template.yaml --clean-type all
创建一个任务配置模板
[mysql|json|file|csv]
$ redporter new -T mysql
---
reader: mysql
redis:
host: 127.0.0.1
port: 6379
db: 0
password: 123456
key: task_read_from_mysql
queue_key_prefix: redporter.queue.
cache_key_prefix: redporter.cache.
mysql:
host: 127.0.0.1
port: 3306
user: root
password: 123456
db: db_test
table: t_test
pk: id
column:
创建任务模板并写入到指定文件
$ redporter new -T mysql -o /tmp/mysql.yaml
注意
每个数据同步任务都必须是唯一指定的 cache_key_prefix + key,但是可以多个任务往同一个队列里推数据,即多个任务可以用相同的 queue_key_prefix + key