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

posted on   荣锋亮  阅读(448)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示