questdb / py-questdb-client

Python client for QuestDB InfluxDB Line Protocol
https://py-questdb-client.readthedocs.io
Apache License 2.0
50 stars 8 forks source link

How to create table if does not exists with py-quesdb-client #30

Closed AnimusXCASH closed 1 year ago

AnimusXCASH commented 1 year ago

Hi,

can someone help me on how to create table if it does not exist with the library.

i know there is a restAPI method for it as described on the page

import sys
import requests
import json

host = 'http://localhost:9000'

def run_query(sql_query):
    query_params = {'query': sql_query, 'fmt' : 'json'}
    try:
        response = requests.get(host + '/exec', params=query_params)
        json_response = json.loads(response.text)
        print(json_response)
    except requests.exceptions.RequestException as e:
        print(f'Error: {e}', file=sys.stderr)

# create table
run_query("CREATE TABLE IF NOT EXISTS trades (name STRING, value INT)")

however not with the package.

Thank you for your time and answer to my request

amunra commented 1 year ago

"When the table_name does not correspond to an existing table, QuestDB will create the table on the fly using the name provided. Column types will be automatically recognized and assigned based on the data."

See: https://questdb.io/docs/reference/api/ilp/overview#behavior

Different Python types map to different ILP types. Mappings are documented in the API docs: https://py-questdb-client.readthedocs.io/en/v1.0.2/api.html#questdb.ingress.Buffer.row

In case the default ILP types aren't specific enough, then you first need to create a table explicitly via either REST or the PosgreSQL Wire Protocol before ingesting the data via ILP: https://questdb.io/docs/develop/connect/

In such case, it's worth glancing at the casting rules that the QuestDB server may perform when you send data via ILP: https://questdb.io/docs/reference/api/ilp/columnset-types/

I'll close this issue, but feel free to follow up with more questions or contact us directly on Slack: https://slack.questdb.io/

AnimusXCASH commented 1 year ago

thank you for your answer 👍