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'
columnclear Clear columns for data space
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
admincode Admin code commands
daemon Run in daemon mode
web Run web management console
"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 -config 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:
-accountid int
Account ID (advertiser ID), required when appid is not empty
-appid string
Wechat appid
-config string
Config file. (default "cfg.toml")
-ds string
Data space ID (required)
-hashtype int
Hash Type. 0=HASH_TYPE_DEFAULT, 1=PHONE_MD5, 2=PHONE_SHA256
-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 |
| -accountid | 否 | 广告主ID,当appid不为空时必填 | 12345 |
| -hashtype | 否 | 哈希类型:0=默认,1=PHONE_MD5,2=PHONE_SHA256;当ds为wuid且appid为空时必填 | 1 |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
wuid数据空间约束
当 ds 为 wuid 时,需要满足以下条件之一:
- 提供
-appid且同时提供-accountid(按openid读取) - 不提供
-appid,但必须设置-hashtype为 1 或 2(按手机号哈希读取)
使用示例
# 读取did数据空间的用户数据
saastool read -ds did -userids cfcd208495d565ef66e7dff9f98764da
# 读取多个用户数据
saastool read -ds did -userids cfcd208495d565ef66e7dff9f98764da,a87ff679a2f3e71d9181a67b7542122c
# 读取wuid数据空间的用户数据(openid方式)
saastool read -ds wuid -userids o_e3j4ggVPO2CP8iCPBLunzKL79n -appid wx1111111111111111 -accountid 12345
# 读取wuid数据空间的用户数据(手机号MD5方式)
saastool read -ds wuid -userids e10adc3949ba59abbe56e057f20f883e -hashtype 1
4.1.4 write(写入用户数据
向指定数据空间写入用户数据。支持批量写入和清空操作。
saastool write -help
Usage of write:
-accountid int
Account ID (advertiser ID), required when appid is not empty
-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)
-hashtype int
Hash Type. 0=HASH_TYPE_DEFAULT, 1=PHONE_MD5, 2=PHONE_SHA256
-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 |
| -accountid | 否 | 广告主ID,当appid不为空时必填 | 12345 |
| -hashtype | 否 | 哈希类型:0=默认,1=PHONE_MD5,2=PHONE_SHA256;当ds为wuid且appid为空时必填 | 1 |
| -batchsize | 否 | 批处理大小 | 10000(默认) |
| -clear | 否 | 写入前是否清空所有数据 | 不填时为false |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
wuid数据空间约束
当 ds 为 wuid 时,需要满足以下条件之一:
- 提供
-appid且同时提供-accountid(按openid写入) - 不提供
-appid,但必须设置-hashtype为 1 或 2(按手机号哈希写入)
数据格式说明
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数据空间(openid方式,需要appid和accountid)
saastool write -ds wuid -source ./openid_users.jsonl -appid wx1111111111111111 -accountid 12345
# 写入wuid数据空间(手机号MD5方式)
saastool write -ds wuid -source ./phone_users.jsonl -hashtype 1
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(默认) |
使用示例
# 重置geo数据空间
saastool resetds -ds geo
# 重置geofac数据空间
saastool resetds -ds geofac
4.1.6 convert(数据转换)
将简化的数据格式转换为saastool写入格式,便于批量数据导入。
saastool convert -help
Usage of convert:
-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 |
映射文件格式
{
"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}}
4.1.7 columnclear(列清零)
将指定数据空间中全量用户的一个或多个列(byte、uint32、flag)清零。对于 DID/WUID 数据空间采用惰性失效机制,对于 Geo/GeoIP/GeoFAC 采用直接 Redis 覆写方式。
saastool columnclear -help
Usage of columnclear:
-all
Clear all columns
-config string
Config file. (default "cfg.toml")
-ds string
Data space ID (required)
-flag string
Flag column indexes to clear (1-4), comma separated. e.g. '1,2'
-u32 string
Uint32 column indexes to clear (1-8), comma separated. e.g. '1,2'
-u8 string
Uint8 column indexes to clear (1-64), comma separated. e.g. '1,2,3'
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -ds | 是 | 数据空间ID | did、wuid、geo、geoip 或 geofac |
| -all | 否 | 是否清零所有列 | 不填时为false |
| -u8 | 否 | 要清零的 uint8 列索引(1-64),逗号分隔 | 1,2,3 |
| -u32 | 否 | 要清零的 uint32 列索引(1-8),逗号分隔 | 1,2 |
| -flag | 否 | 要清零的 flag 列索引(1-4),逗号分隔 | 1,2 |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
必须指定 -all 或至少一个 -u8、-u32、-flag 参数。
使用示例
# 清零 did 数据空间的所有列
saastool columnclear -ds did -all
# 清零 did 数据空间的指定 uint8 列
saastool columnclear -ds did -u8 1,2,3
# 清零 wuid 数据空间的指定 uint32 和 flag 列
saastool columnclear -ds wuid -u32 1,2 -flag 1
# 清零 geo 数据空间的所有列
saastool columnclear -ds geo -all
4.1.8 daemon(守护进程/HTTP服务)
以守护进程模式启动saastool HTTP服务,提供HTTP接口进行数据读写操作。
saastool daemon -help
环境变量
| 变量名 | 含义 | 样例 |
|---|---|---|
| SRTA_ACCOUNT | sRTA账号(必填) | 2000 |
| SRTA_TOKEN | sRTA Token(必填) | test_token |
| SRTA_ENV | 环境类型:demo、prd 或 dev | demo、prd |
| SRTA_PORT | 服务监听端口(可选) | 8080(默认) |
使用示例
# 启动daemon模式
export SRTA_ACCOUNT=2000
export SRTA_TOKEN=mytoken
export SRTA_ENV=demo
export SRTA_PORT=8080
saastool daemon
启动后,HTTP服务监听在配置的端口(默认8080),支持 /read 和 /write 接口。