Skip to content

Commit 39769d0

Browse files
committed
Added tb config
1 parent c45d943 commit 39769d0

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ _build
4646

4747
# Tinibird
4848
bl-*
49-
out-*
49+
out-*
50+
.e

clickhouse_mysql/clioptions.py

+23
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class CLIOptions(Options):
9393
#
9494
# general app section
9595
#
96+
97+
'tb_host': 'https://ui.tinybird.co',
98+
'tb_token': None,
99+
96100
'config_file': '/etc/clickhouse-mysql/clickhouse-mysql.conf',
97101
'log_file': None,
98102
'log_level': None,
@@ -171,6 +175,20 @@ def options(self):
171175
#
172176
# general app section
173177
#
178+
argparser.add_argument(
179+
'--tb-host',
180+
type=str,
181+
default=self.default_options['tb_host'],
182+
help='Tinybird host'
183+
)
184+
185+
argparser.add_argument(
186+
'--tb-token',
187+
type=str,
188+
default=self.default_options['tb_token'],
189+
help='Tinybird host'
190+
)
191+
174192
argparser.add_argument(
175193
'--config-file',
176194
type=str,
@@ -508,6 +526,11 @@ def options(self):
508526
#
509527
# general app section
510528
#
529+
530+
'tb_host': args.tb_host,
531+
'tb_token': args.tb_token,
532+
533+
511534
'config_file': args.config_file,
512535
'log_file': args.log_file,
513536
'log_level': args.log_level,

clickhouse_mysql/config.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from clickhouse_mysql.writer.chwriter import CHWriter
88
from clickhouse_mysql.writer.csvwriter import CSVWriter
9-
from clickhouse_mysql.writer.chcsvwriter import CHCSVWriter
9+
from clickhouse_mysql.writer.tbcsvwriter import TBCSVWriter
1010
from clickhouse_mysql.writer.poolwriter import PoolWriter
1111
from clickhouse_mysql.writer.processwriter import ProcessWriter
1212
from clickhouse_mysql.objectbuilder import ObjectBuilder
@@ -61,6 +61,10 @@ def __init__(self):
6161
#
6262
#
6363
#
64+
'tinybird': {
65+
'host': self.options['tb_host'],
66+
'token': self.options['tb_token'],
67+
},
6468
'app': {
6569
'config_file': self.options['config_file'],
6670
'log_file': self.options['log_file'],
@@ -359,8 +363,11 @@ def writer_builder_csvpool(self):
359363
'dst_table': self.config['writer']['file']['dst_table'],
360364
'dst_table_prefix': self.config['writer']['file']['dst_table_prefix'],
361365
'next_writer_builder': ObjectBuilder(
362-
class_name=CHCSVWriter,
363-
constructor_params=self.config['writer']['clickhouse']
366+
class_name=TBCSVWriter,
367+
constructor_params={
368+
'tb_host': self.config['tinybird']['host'],
369+
'tb_token': self.config['tinybird']['token']
370+
}
364371
),
365372
'converter_builder': self.converter_builder(CONVERTER_CSV),
366373
})

clickhouse_mysql/writer/chcsvwriter.py renamed to clickhouse_mysql/writer/tbcsvwriter.py

+22-23
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,39 @@
1212
from requests_toolbelt.multipart.encoder import MultipartEncoder
1313
import json
1414

15-
class CHCSVWriter(Writer):
16-
"""Write into ClickHouse via CSV file and clickhouse-client tool"""
15+
class TBCSVWriter(Writer):
16+
"""Write into Tinybird via CSV file"""
1717

1818
dst_schema = None
1919
dst_table = None
2020
dst_distribute = None
2121

22-
host = None
23-
port = None
24-
user = None
25-
password = None
22+
tb_host = None
23+
tb_token = None
2624

2725
def __init__(
2826
self,
29-
connection_settings,
27+
tb_host,
28+
tb_token,
3029
dst_schema=None,
3130
dst_table=None,
3231
dst_table_prefix=None,
3332
dst_distribute=False,
3433
):
35-
if dst_distribute and dst_schema is not None:
36-
dst_schema += "_all"
37-
if dst_distribute and dst_table is not None:
38-
dst_table += "_all"
39-
logging.info(
40-
"CHCSWriter() connection_settings={} dst_schema={} dst_table={}".format(connection_settings, dst_schema,
41-
dst_table))
42-
self.host = connection_settings['host']
43-
self.port = connection_settings['port']
44-
self.user = connection_settings['user']
45-
self.password = connection_settings['password']
34+
# if dst_distribute and dst_schema is not None:
35+
# dst_schema += "_all"
36+
# if dst_distribute and dst_table is not None:
37+
# dst_table += "_all"
38+
# logging.info(
39+
# "CHCSWriter() connection_settings={} dst_schema={} dst_table={}".format(connection_settings, dst_schema,
40+
# dst_table))
41+
self.tb_host = tb_host
42+
self.tb_token = tb_token
43+
44+
if self.tb_host is None or self.tb_token is None:
45+
logging.critical(f" Host: {self.tb_host} or token {self.tb_token} is missing")
46+
return None
47+
4648
self.dst_schema = dst_schema
4749
self.dst_table = dst_table
4850
self.dst_table_prefix = dst_table_prefix
@@ -75,17 +77,14 @@ def insert(self, event_or_events=None):
7577
'name': self.dst_table,
7678
'mode': 'append'
7779
}
78-
token = 'p.eyJ1IjogIjUzMWUwMDUxLTRmZGQtNDc5MC05OGY0LWNlNTA3NDVkYzFkNSIsICJpZCI6ICIzNzMyOWExNy1mMGM2LTQyNWMtODhmOC1hZDE2MTU4OWIwNmMifQ.80_ZbRL9D8x2mlWay1EjdeA-zi7asxqKqp06NoOmadg'
79-
# token = 'p.eyJ1IjogImJjYzkyYjJkLTY3YWItNDIxMC05YjkzLWVhYjc4ODA3ZTVkMiIsICJpZCI6ICJiMjNiNGQwNC01N2FhLTRiOTMtOTNkMS1lNzE0ODcxZWJkYTUifQ.YNzxkpebfTIgZGSQ9UiUpunKymeY6n7cWUY-3jgSp8E'
8080

8181
f = open(event.filename, 'rb')
8282
m = MultipartEncoder(fields={'csv': ('csv', f, 'text/csv')})
8383

84-
url = 'https://tb-test.tinybird.co/v0/datasources'
85-
# url = 'http://localhost:8001/v0/datasources'
84+
url = f"{self.tb_host}/v0/datasources"
8685

8786
response = requests.post(url, data=m,
88-
headers={'Authorization': 'Bearer ' + token, 'Content-Type': m.content_type},
87+
headers={'Authorization': 'Bearer ' + self.tb_token, 'Content-Type': m.content_type},
8988
params=params
9089
)
9190

0 commit comments

Comments
 (0)