在perl中循环从InfluxDB::HTTP返回的数据结构
Looping the data structure returned from InfluxDB::HTTP in perl
试图找出值中的值对。
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;
我得到的结果是这样的:
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]]}]}]}
但是当我做一个:
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.
所以它看起来像一个散列,但它不是?关于如何使用数据结构的任何提示?
Object returned by call to InfluxDB::HTTP::query() ... can't be used as <HASH>
是一个线索,表明 $res
是一个对象。 Consult the relevant API 查看允许对对象执行哪些操作以访问其数据。
文档中有一个标题为 Return Values and Error Handling 的部分说:
Object::Result
is relied upon for returning data from subroutines.
我想如果那是 Object::Result 文档的 link 或包含更好的使用 returned object 的示例会更有帮助。
但是查看源代码,我看到结果 object 是这样创建的:
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; }
}
据我猜测,您可以从 $res->data
或 $res->results
中获取所需的实际数据 - 两者都将 return 散列引用。或者如果你想要原始查询响应,你可以使用 $res->raw
.
我认为你想要的值数据是$res->results->[0]{series}[0]{values}
。
哦,稍后,query()
方法的文档说:
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.
虽然没有提到 results
属性。
试图找出值中的值对。
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;
我得到的结果是这样的:
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]]}]}]}
但是当我做一个:
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.
所以它看起来像一个散列,但它不是?关于如何使用数据结构的任何提示?
Object returned by call to InfluxDB::HTTP::query() ... can't be used as <HASH>
是一个线索,表明 $res
是一个对象。 Consult the relevant API 查看允许对对象执行哪些操作以访问其数据。
文档中有一个标题为 Return Values and Error Handling 的部分说:
Object::Result
is relied upon for returning data from subroutines.
我想如果那是 Object::Result 文档的 link 或包含更好的使用 returned object 的示例会更有帮助。
但是查看源代码,我看到结果 object 是这样创建的:
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; }
}
据我猜测,您可以从 $res->data
或 $res->results
中获取所需的实际数据 - 两者都将 return 散列引用。或者如果你想要原始查询响应,你可以使用 $res->raw
.
我认为你想要的值数据是$res->results->[0]{series}[0]{values}
。
哦,稍后,query()
方法的文档说:
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 attributerequest_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.
虽然没有提到 results
属性。