Created
June 29, 2021 12:48
-
-
Save bsmth/720b7ad0384e37d66252be150ee3e808 to your computer and use it in GitHub Desktop.
Import CSV to QuestDB from Python
This file contains 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 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