简体   繁体   中英

Looping the data structure returned from InfluxDB::HTTP in perl

Trying to get out the value pair in values.

my $influx = InfluxDB::HTTP->new(host => 'localhost', port => 8086);
my $query = 'SELECT mean(value) FROM db1.autogen.num1,db1.autogen.num2 WHERE time > now() - 1h GROUP BY time(1s) LIMIT 20';

my $res = $influx->query([$query], epoch => 's',);
print $res;

The result I'm getting is this:

Returned data: {"results":[{"statement_id":0,"series":[{"name":"num1","columns":["time","value"],"values":[[1550842812,114.098],[1550842812,114.084],[1550842812,114.07],[1550842812,114.055],[1550842812,114.041],[1550842813,114.027],[1550842813,114.012],[1550842813,113.998],[1550842813,113.984],[1550842813,113.969],[1550842814,113.955],[1550842814,113.941],[1550842814,113.926],[1550842814,113.911],[1550842814,113.897],[1550842815,113.883],[1550842815,113.868],[1550842815,113.854],[1550842815,113.84],[1550842815,113.825]]},{"name":"num2","columns":["time"│ ,"value"],"values":[[1550842812,11.358],[1550842812,11.373],[1550842812,11.388],[1550842812,11.402],[1550842812,11.416],[1550842813,11.431],[1550842813,11.445],[1550842813,11.459],[155084│ 2813,11.474],[1550842813,11.488],[1550842814,11.502],[1550842814,11.517],[1550842814,11.531],[1550842814,11.545],[1550842814,11.56],[1550842815,11.575],[1550842815,11.589],[1550842815,11.│ 604],[1550842815,11.618],[1550842815,11.632]]}]}]}

But when I do a:

print $res->{results};

Object returned by call to InfluxDB::HTTP::query() at test.pl line 10 can't be used as <HASH> at test.pl line 12.

So it looks like a hash, but it isn't? Any tips on how to work with the data structure?

https://metacpan.org/pod/InfluxDB::HTTP

The documentation has a section entitled Return Values and Error Handling which says:

Object::Result is relied upon for returning data from subroutines.

I guess it would be more helpful if that was a link to the Object::Result documentation or contained better examples of using the returned object.

But looking at the source code, I see the result object is created like this:

result {
    raw         { return $response; }
    data        { return $data; }
    results     { return $data->{results}; }
    request_id  { return $response->header('Request-Id'); }
    <STR>       { return "Returned data: $content"; }
    <BOOL>      { return 1; }
}

From which I guess, you can get the actual data that you want from $res->data or $res->results - both of which will return hash references. Or if you want the raw query response, you can use $res->raw .

I think that the values data you want is $res->results->[0]{series}[0]{values} .

Oh, and later on, the documentation for the query() method says:

If the returned object evaluates to true, indicating that the query was successful, then the returned object's data attribute contains the entire response from InfluxDB as Perl hash. Additionally the attribute request_id provides the request identifier as set in the HTTP reponse headers by InfluxDB. This can for example be useful for correlating requests with log files.

Doesn't mention the results attribute though.

Object returned by call to InfluxDB::HTTP::query() ... can't be used as <HASH> is a clue that $res is an object. Consult the relevant API to see what operations are allowed on the object to access its data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM