Skip to content

Instantly share code, notes, and snippets.

@aksinghdce
Last active January 31, 2020 22:01
Show Gist options
  • Save aksinghdce/2a4164158cc45541d21d704fc141f4d3 to your computer and use it in GitHub Desktop.
Save aksinghdce/2a4164158cc45541d21d704fc141f4d3 to your computer and use it in GitHub Desktop.
Run SQL like query on ElasticSearch
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