Skip to content

Instantly share code, notes, and snippets.

@bsmth
Created June 29, 2021 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bsmth/720b7ad0384e37d66252be150ee3e808 to your computer and use it in GitHub Desktop.
Save bsmth/720b7ad0384e37d66252be150ee3e808 to your computer and use it in GitHub Desktop.
Import CSV to QuestDB from Python
import urllib.request
import pandas as pd
import requests
def import_data(file_name, table_name):
params = {}
params['timestamp'] = 'TradeDate'
params['partitionBy'] = 'MONTH'
url_values = urllib.parse.urlencode(params)
query_url = "http://%s:9000/imp?" % (server_ip) + url_values
df = pd.read_csv(file_name, sep=",")
df.drop_duplicates(subset=None, inplace=True)
df.to_csv(file_name, index=False)
with open(file_name, 'rb') as csv:
file_data = csv.read()
files = {'data': (table_name, file_data)}
schema = '[{"name":"TradeDate", "type": "TIMESTAMP", "pattern": "yyyy-MM-dd" }]'
response = requests.post(query_url, data={'schema': schema}, files=files)
print(response.text)
server_ip = '127.0.0.1'
import_data('my_data.csv', 'test_import')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment