Last active
January 31, 2020 22:01
-
-
Save aksinghdce/2a4164158cc45541d21d704fc141f4d3 to your computer and use it in GitHub Desktop.
Run SQL like query on ElasticSearch
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 requests | |
import json | |
import numpy as np | |
import pandas as pd | |
url = 'http://<ElasticSearch_IP_Address>:9200/_sql?format=csv' | |
headers = { | |
'Content-Type': 'application/json', | |
} | |
query = { | |
'query': ''' | |
SELECT | |
(ResponseTime - ConnectTime) AS CorrectedResponseTime, SampleStartTime, ErrorCount, SampleLabel | |
FROM jmeter | |
WHERE SampleStartTime >'2020-01-29T17:03:13.445Z' AND SampleStartTime < '2020-01-29T17:15:13.445Z' | |
''' | |
} | |
response = requests.post(url, headers=headers, auth=('<ElasticSearch_User>', '<ElasticSearch_Password>'), data=json.dumps(query)) | |
result_rows = str(response.content).split("\\r\\n") | |
records = [r.split(",") for r in result_rows] | |
result_df = pd.DataFrame.from_records(records) | |
cleaned_df = result_df.iloc[:, 0:4] | |
cleaned_df = cleaned_df.rename(columns={x:y for x,y in zip(range(4), records[0])}) | |
cleaned_df = cleaned_df.iloc[1:,:] | |
cleaned_df = cleaned_df.set_index("SampleStartTime") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment