Singer 学习八 运行&&开发taps、targets (三 开发tap)
如何没有找到适合的tap,那么我们可以自己开发一个
hello world
tap 仅仅是一个程序,我们可以使用任何语言进行编写,根据singer 指南,输出数据到stdout 即可,实际上一个简单的
demo,可以直接使用命令行工具,不需要编写任何代码
内容
printf '{"type":"SCHEMA", "stream":"hello","key_properties":[],"schema":{"type":"object", "properties":{"value":{"type":"string"}}}}\n{"type":"RECORD","stream":"hello","schema":"hello","record":{"value":"world"}}\n'
说明:
上边的编写了数据{"value":"world"}
到一个hello 的stream,同时指定了value 的数据类型为string,数据可以pipe 到
任何的target
python tap demo
singer 提供了python 的工具包,我们可以方便的开发tap
(1). 安装singer-python
推荐的做法,是使用指定的版本
pip install singer-python
(2). 简单demo
tap_ip.py
import singer
import urllib.request
from datetime import datetime, timezone
now = datetime.now(timezone.utc).isoformat()
schema = {
'properties': {
'ip': {'type': 'string'},
'timestamp': {'type': 'string', 'format': 'date-time'},
},
}
with urllib.request.urlopen('http://icanhazip.com') as response:
ip = response.read().decode('utf-8').strip()
singer.write_schema('my_ip', schema, 'timestamp')
singer.write_records('my_ip', [{'timestamp': now, 'ip': ip}])
说明:
singer.write_schema 编写了一个 my_ip stream 同时定义了 primary key
singer.write_records 写record 数据到stream
(3). 运行
下边的demo 是将数据pipe 到google sheet 的target
python tap_ip.py | target-gsheet -c config.json
说明
官方同时提供了一个tap 的脚手架模版singer-tap-template
参考资料
https://github.com/singer-io/getting-started/blob/master/docs/RUNNING_AND_DEVELOPING.md
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)