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
rtexp Realtime 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 接口。
4.1.9 task(任务管理)命令列表
用于管理文件上传任务。任务管理分为两个阶段:
- 本地处理阶段:计算文件哈希和块大小(make)
- 服务端操作阶段:创建、查询、上传、下载、运行和删除任务
saastool task help
Usage: saastool task COMMAND [OPTIONS]
Commands:
list List tasks on server
make Make file hash for upload task
create Create a task on server
upload Upload task's file block to server
download Download task's file block to local
run Run a task on server
delete Delete a task on server
info Get a task info on server
"help" is the default command.
Use "saastool task COMMAND -help" for more information about a command.
4.1.9.1 task list(查询任务列表)
查询已创建的所有任务及其状态。
saastool task list -help
Usage of list:
-config string
Config file. (default "cfg.toml")
-status string
Filter status. enums 'all', 'waiting', 'running', 'success', 'fail', 'deleted'
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -status | 否 | 任务状态过滤:all、waiting、running、success、fail、deleted | waiting(默认all) |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
# 查询所有任务
saastool task list
# 查询正在运行的任务
saastool task list -status running
# 查询已完成的任务
saastool task list -status success
4.1.9.2 task make(生成任务哈希)
计算本地文件或目录的SHA256哈希值,生成任务文件。这是上传文件前的必要步骤。
saastool task make -help
Usage of make:
-accountid int
Account ID (advertiser ID), required when appid is not empty
-appid string
AppID for wuid dataspace
-blocksize string
Block size to make hash. using size mode K, M, G, T (default "50M")
-ds string
DataSpace ID (required)
-hashfile string
Output hash file (required)
-hashtype int
Hash Type. 0=HASH_TYPE_DEFAULT, 1=PHONE_MD5, 2=PHONE_SHA256
-source string
source file or directory path (required)
-desc string
Task description
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -source | 是 | 本地文件或目录路径 | ./data.jsonl 或 ./data_dir/ |
| -hashfile | 是 | 输出的哈希文件路径 | ./task.json |
| -ds | 是 | 数据空间ID | did、wuid、geo、geoip 或 geofac |
| -blocksize | 否 | 块大小,支持 K/M/G/T 单位(50M-200M) | 100M、1G(默认50M) |
| -appid | 否 | 小程序ID,当ds为wuid时必填 | wx1111111111111111 |
| -accountid | 否 | 广告主ID,当appid不为空时必填 | 12345 |
| -hashtype | 否 | 哈希类型:0=默认,1=PHONE_MD5,2=PHONE_SHA256 | 1 |
| -desc | 否 | 任务描述 | 用户属性批量导入 |
使用示例
# 计算单个文件的哈希
saastool task make -source ./users.jsonl -hashfile ./task.json -ds did
# 计算目录的哈希,指定块大小
saastool task make -source ./data_dir/ -hashfile ./task.json -ds did -blocksize 100M
# 为wuid数据空间创建任务(需要appid和accountid)
saastool task make -source ./users.jsonl -hashfile ./task.json -ds wuid -appid wx1111111111111111 -accountid 12345 -desc "openid用户导入"
# 为wuid数据空间创建任务,使用手机号MD5哈希(无需appid)
saastool task make -source ./phones.jsonl -hashfile ./task.json -ds wuid -hashtype 1
4.1.9.3 task create(创建任务)
将本地生成的任务文件上传到服务器,创建一个新任务。
saastool task create -help
Usage of create:
-config string
Config file. (default "cfg.toml")
-hashfile string
Input hash file (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -hashfile | 是 | 任务文件路径(由task make生成) | ./task.json |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
saastool task create -hashfile ./task.json
4.1.9.4 task upload(上传任务文件)
上传任务的文件块到服务器。
saastool task upload -help
Usage of upload:
-config string
Config file. (default "cfg.toml")
-sha256 string
Task SHA256 hash (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -sha256 | 是 | 任务SHA256哈希值 | abc123def456... |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
saastool task upload -sha256 abc123def456...
4.1.9.5 task download(下载任务文 件)
从服务器下载任务的文件块到本地。
saastool task download -help
Usage of download:
-config string
Config file. (default "cfg.toml")
-dest string
Destination path (required)
-sha256 string
Task SHA256 hash (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -sha256 | 是 | 任务SHA256哈希值 | abc123def456... |
| -dest | 是 | 目标下载路径 | ./output/ |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
saastool task download -sha256 abc123def456... -dest ./output/
4.1.9.6 task run(运行任务)
在服务器上运行任务,处理已上传的数据。
saastool task run -help
Usage of run:
-config string
Config file. (default "cfg.toml")
-sha256 string
Task SHA256 hash (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -sha256 | 是 | 任务SHA256哈希值 | abc123def456... |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
saastool task run -sha256 abc123def456...
4.1.9.7 task delete(删除任务)
从服务器删除任务。
saastool task delete -help
Usage of delete:
-config string
Config file. (default "cfg.toml")
-sha256 string
Task SHA256 hash (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -sha256 | 是 | 任务SHA256哈希值 | abc123def456... |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
saastool task delete -sha256 abc123def456...
4.1.9.8 task info(获取任务信息)
获取服务器上的任务详细信息。
saastool task info -help
Usage of info:
-config string
Config file. (default "cfg.toml")
-sha256 string
Task SHA256 hash (required)
参数说明
| 参数 | 必填 | 含义 | 样例 |
|---|---|---|---|
| -sha256 | 是 | 任务SHA256哈希值 | abc123def456... |
| -config | 否 | 配置文件路径 | cfg.toml(默认) |
使用示例
saastool task info -sha256 abc123def456...