Skip to content

Commit 155adc8

Browse files
committed
async version
1 parent b2d4030 commit 155adc8

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

tests/test_pipeline.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
config.read(".env")
1111

1212
def test_kafkasink() -> None:
13-
database_url = config['kafka'].get("url")
13+
database_url = config['sink'].get("url")
1414
sink = KafkaSink(database_url)
1515

tests/test_sql.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ def run_sync(*args, **kwargs):
3131

3232

3333
def test_parse() -> None:
34-
database_url = config['mysql'].get("url")
34+
database_url = config['source'].get("url")
3535
db = parse_url(database_url)
3636
debug(db)
3737

3838
@async_adapter
3939
async def test_mock() -> None:
4040
meta = sqlalchemy.MetaData()
41-
database_url = config['mysql'].get("url")
41+
database_url = config['source'].get("url")
4242
debug(database_url)
4343

4444
engine = sqlalchemy.create_engine(database_url)

unimeta/pipeline.py

+34-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import requests
1919
from concurrent.futures import ProcessPoolExecutor
2020
import aiohttp
21-
from aiokafka import AIOKafkaProducer
21+
from aiokafka import AIOKafkaProducer, AIOKafkaConsumer
2222
from aioch import Client
2323

2424

@@ -102,6 +102,7 @@ async def publish(self, event):
102102
columns=",".join(columns))
103103
try:
104104
await self.client.execute(sql,[event.data])
105+
logger.success("{sql} insert clickhouse".format(sql=sql))
105106
except:
106107
logger.error(event.data)
107108
logger.exception("what?")
@@ -118,13 +119,13 @@ def delivery_report(err, msg):
118119
class KafkaSink(Sink):
119120

120121
def __init__(self, database_url):
121-
Source.__init__(self)
122+
Sink.__init__(self)
122123
settings = parse_url(database_url)
123124
loop = asyncio.get_event_loop()
125+
bootstrap = '{host}:{port}'.format(host=settings['host'],port=settings['port'])
124126
self.producer = AIOKafkaProducer(
125127
loop=loop,
126-
bootstrap_servers = '{host}:{port}'.format(host=settings['host'],port=settings['port']),
127-
128+
bootstrap_servers = bootstrap
128129
)
129130
self.producer.start()
130131
self.topic = settings['name']
@@ -142,6 +143,31 @@ async def publish(self, event):
142143

143144
#logger.success("publish {event} success".format(event=str(event)))
144145

146+
class KafkaSource(Source):
147+
148+
def __init__(self, database_url):
149+
Source.__init__(self)
150+
settings = parse_url(database_url)
151+
name = settings['name']
152+
user = settings['user']
153+
if user is None:
154+
user = "default"
155+
loop = asyncio.get_event_loop()
156+
bootstrap = '{host}:{port}'.format(host=settings['host'],port=settings['port'])
157+
self.consumer = AIOKafkaConsumer(
158+
name, loop=loop,
159+
bootstrap_servers = bootstrap,
160+
group_id = user
161+
)
162+
163+
async def start(self):
164+
await self.consumer.start()
165+
166+
async def subscribe(self):
167+
async for msg in self.consumer:
168+
print(msg)
169+
yield msg
170+
145171

146172

147173
class MetaServer():
@@ -186,6 +212,8 @@ def __init__(self,source:str, sink:str, meta:str):
186212
sconf = parse_url(source)
187213
if sconf['scheme'] == 'mysql':
188214
self.source = MysqlSource(source)
215+
elif sconf['scheme'] == 'kafka':
216+
self.source = KafkaSource(source)
189217
else:
190218
raise Exception("unregister source")
191219

@@ -223,5 +251,7 @@ def rebuild_table(self, table):
223251
async def sync(self):
224252
await self.sink.start()
225253
async for event in self.source.subscribe():
254+
debug(event)
255+
continue
226256
await self.metaserver.reg(event)
227257
await self.sink.publish(event)

unimeta/table.py

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def avro_types(self) -> Optional[str,List[str]]:
126126
return "string"
127127

128128

129+
class PhoneField(StringColumn):
130+
pass
131+
129132

130133

131134
class TextColumn(Column):

0 commit comments

Comments
 (0)