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"}}}}
{"type":"RECORD","stream":"hello","schema":"hello","record":{"value":"world"}}
'
 

说明:
上边的编写了数据{"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

原文地址:https://www.cnblogs.com/rongfengliang/p/10245087.html