0

I am trying to post query in python to get data from Kairosdb: meterreading is a metric that I already created.

import urllib
import urllib2

url = 'http://localhost:8080/api/v1/datapoints/query'
values = {
    "start_absolute": "1430454600",
        "end_relative": {
"value": "5",
    "unit": "days"
        },
            "metrics": [
                        {
                        "tags": {
                        "phase": [
                                  "769"
                                  ],
                        "uom": [
                                "72"
                                ]
                        },
                        "name": "materreadings",
                        "aggregators": [
                                        {
                                        "name": "sum",
                                        "sampling": {
                                        "value": "10",
                                        "unit": "minutes"
                                        }
                                        }
                                        ]
                        }
                        ]
}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req) // this line is giving errors
the_page = response.read()
print(the_page)

I am getting following result while executing this script:

python abc.py
Traceback (most recent call last):
  File "abc.py", line 37, in <module>
    response = urllib2.urlopen(req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1184, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 61] Connection refused>

It seems like problem is in line 37, when I try to get response by sending request. I am able to perform this in JAVA but in python I am stuck. Am I missing something.

Bharthan
  • 1,458
  • 2
  • 17
  • 29

1 Answers1

1

I have no experience with Python client (BTW there's a Python kairosDB library - I never used it but it exists here: https://github.com/pcn/pyKairosDB).

But I think I know what's wrong: try url = 'http://localhost:8080/api/v1/datapoints/query/' (note the trailing slash) You are using urlencode on the query JSON payload, I don't tink you need to ( urlencode is used to encode special characters for using in URLs with GET method).

BTW your query is a bit Strange with an absolute start very small -I think you entered Unix seconds rather than milliseconds- and a relative end 5 days ago... Is that what you mean to do - asking for data from the 17th of January 1970 up to 5 days ago?

May I ask you what king of data you have? I see that you have tags with numbered values, the KairosDB format doesn't play well with tags that may have too many values (maybe it's just an ID of the origin of the data, as long as you don't have thousands or millions of them you'll be OK).

I hope this helps.

Loic
  • 1,088
  • 7
  • 19