4 saastool工具
saastool 是提供给客户的便捷命令行工具,使客户在不开发或少开发的情况下即可实现【数据管理】【策略管理】的对应功能。
开发者也可以参考该工具的功能源码(golang),实现贴合自身业务的处理逻辑。
源码:saastool
4.1 命令行模式
saastool help
Usage: saastool COMMAND [OPTIONS]
Commands:
info Saas Info
write Write user's 'bytes / uint32s / flags'
read Read user's 'bytes / uint32s / flags'
columnwrite Write columns for 'deviceid / openid' users
resetds Reset data space
convert Convert data to write format
task Task commands
target Target commands
bind Bind commands
grant Grant commands
script Script commands
exp Exp commands
daemon Run in daemon mode
"help" is the default command.
Use "saastool COMMAND -help" for more information about a command.
4.1.1 cfg.toml配置文件
saastool需要一个配置文件,其中可填写自己的Account、Token。使用配置文件可实现多账号多环境的区分。
该配置文件默认名称为 cfg.toml,请置于saastool 同目录下。也可以通过 -cfg 参数指定别的配置文件,例如 saastool -cfg my.toml。
# 样例
[auth]
account = "2000"
token = "test"
[apiurls]
baseurl = "https://api.rta.qq.com" # 正式环境
#baseurl = "https://srta.algo.com.cn" # 演示环境
4.1.2 info(获取Saas信息)
获取sRTA服务的基本信息,包括数据空间、策略ID等配置信息。
saastool info -help
Usage of info:
-config string
Config file. (default "cfg.toml")
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
saastool info
4.1.3 read(读取用户数据)
读取指定用户在数据空间中的数据(包括字节、uint32和标志位)。
saastool read -help
Usage of read:
-appid string
Wechat appid
-config string
Config file. (default "cfg.toml")
-ds string
Data space ID (required)
-userids string
Device ID or Wechat UserID, separated by comma (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -userids | 是 | 用户ID列表,多个ID用逗号分隔;设备号MD5值(小写)或微信openid | cfcd208495d565ef66e7dff9f98764da |
| -ds | 是 | 数据空间ID | did、wuid、geo、geoip 或 geofac |
| -appid | 否 | 小程序ID,当ds为wuid时必填 | wx1111111111111111 |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
# 读取did数据空间的用户数据
saastool read -ds did -userids cfcd208495d565ef66e7dff9f98764da
# 读取多个用户数据
saastool read -ds did -userids cfcd208495d565ef66e7dff9f98764da,a87ff679a2f3e71d9181a67b7542122c
# 读取wuid数据空间的用户数据
saastool read -ds wuid -userids o_e3j4ggVPO2CP8iCPBLunzKL79n -appid wx1111111111111111
4.1.4 write(写入用户数据
向指定数据空间写入用户数据。支持批量写入和清空操作。
saastool write -help
Usage of write:
-appid string
Wechat appid
-batchsize uint
Batch size to sync (default 10000)
-clear
Clear all data before write
-config string
Config file. (default "cfg.toml")
-ds string
Data space ID (required)
-source string
Source path or filename (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -source | 是 | 本地文件或目录路径,JSONL格式 | ./users.jsonl 或 ./data_dir/ |
| -ds | 是 | 数据空间ID | did、wuid、geo、geoip 或 geofac |
| -appid | 否 | 小程序ID,当ds为wuid时必填 | wx1111111111111111 |
| -batchsize | 否 | 批处理大小 | 10000(默认) |
| -clear | 否 | 写入前是否清空所有数据 | 不填时为false |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
数据格式说明
JSONL 文件格式,每行一个JSON对象,包含 userid 和数据字段:
{"userid":"cfcd208495d565ef66e7dff9f98764da","bytesKv":{"1":1,"2":100},"uint32sKv":{"1":1000000}}
{"userid":"a87ff679a2f3e71d9181a67b7542122c","bytesKv":{"1":2,"2":200},"flagsWithExpireKv":{"1":{"flag":true}}}
使用示例
# 写入单个 文件数据
saastool write -ds did -source ./users.jsonl
# 写入目录下所有文件
saastool write -ds did -source ./data_dir/ -batchsize 5000
# 写入前清空所有数据
saastool write -ds did -source ./users.jsonl -clear
# 为wuid数据空间写入数据
saastool write -ds wuid -source ./openid_users.jsonl -appid wx1111111111111111
4.1.5 resetds(重置数据空间)
重置指定数据空间,清除所有用户数据。
注意
此操作为破坏性操作,将删除数据空间中的所有数据,请谨慎使用。
saastool resetds -help
Usage of resetds:
-config string
Config file. (default "cfg.toml")
-ds string
Data space ID (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -ds | 是 | 数据空间ID | 当前仅支持 geo、geoip 或 geofac |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
# 重置did数据空间
saastool resetds -ds did
# 重置wuid数据空间
saastool resetds -ds wuid
4.1.6 convert(数据转换)
将简化的数据格式转换为saastool写入格式,便于批量数据导入。
saastool convert -help
Usage of convert:
-config string
Config file. (default "cfg.toml")
-dest string
Destination path or filename (required)
-map string
target map setting (required)
-source string
Source path or filename (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -source | 是 | 源数据文件或目录路径 | ./raw_data/ 或 ./raw_data.txt |
| -dest | 是 | 转换后数据的输出目录 | ./converted_data/ |
| -map | 是 | 映射配置文件路径(JSON格式) | ./map.json |
| -config | 否 | saastool配置文件路径 | cfg.toml(默认) |
映射文件格式
{
"targets": {
"news_1": {
"write_byte": 1,
"write_byte_pos": 0
},
"music_2": {
"write_byte": 2,
"write_byte_pos": 1
}
}
}
使用示例
saastool convert -map ./map.json -source ./raw_data/ -dest ./converted_data/
转换结果示例:
{"userid":"692873b822ef89cb7e935ff370881026","bytesKv":{"1":1,"2":2}}
{"userid":"a763b592c846f0a78fb9b326d5c8ba78","bytesKv":{"1":1,"2":3}}