Created
July 20, 2020 16:07
-
-
Save alrocar/9b1b860cf74ac6f2ad115c3cb2945e93 to your computer and use it in GitHub Desktop.
Ingest to Tinybird from a Python array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import requests | |
from io import StringIO | |
from requests.adapters import HTTPAdapter | |
from urllib3.util.retry import Retry | |
rows = [[1, 2, 3], [4, 5, 6]] | |
endpoint = 'https://api.tinybird.co' | |
mode = 'append' | |
datasource = '{DATASOURCE}' | |
token = '{TOKEN}' | |
url = f'{endpoint}/v0/datasources?mode={mode}&name={datasource}' | |
retry = Retry(total=5, backoff_factor=0.2) | |
adapter = HTTPAdapter(max_retries=retry) | |
_session = requests.Session() | |
_session.mount('http://', adapter) | |
_session.mount('https://', adapter) | |
csv_chunk = StringIO() | |
writer = csv.writer(csv_chunk, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) | |
max_wait_records = 5000 | |
max_wait_bytes = 32 * 1024 ** 2 | |
records = 0 | |
for row in rows: | |
writer.writerow(row) | |
records += 1 | |
if (records > max_wait_records and csv_chunk.tell() > max_wait_bytes) or len(rows) == records: | |
data = csv_chunk.getvalue() | |
headers = { | |
'Authorization': f'Bearer {token}', | |
'X-TB-Client': 'pltx-0.1', | |
} | |
ok = False | |
try: | |
response = _session.post(url, headers=headers, files=dict(csv=data)) | |
result = response.json() | |
ok = response.status_code < 400 | |
if ok: | |
csv_chunk = StringIO() | |
writer = csv.writer(csv_chunk, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) | |
print(f"Flushed {len(data)} bytes, datasource={datasource}, response={response.status_code}") | |
print(f"Result id={result.get('import_id', None)}, error={result.get('error', False)}") | |
except Exception as e: | |
print(e) | |
print('Done') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment