![](/img/trans.png)
[英]perl - help passing/looping in arguments from file or within a separate perl data structure
[英]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::Result
依賴於從子例程返回數據。
我想如果這是到Object :: Result文檔的鏈接或包含使用返回的對象的更好示例的話,它將更有幫助。
但是看一下源代碼,我看到結果對象是這樣創建的:
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
獲取所需的實際數據-兩者都將返回哈希引用。 或者,如果您想要原始查詢響應,則可以使用$res->raw
。
我認為您想要的值數據是$res->results->[0]{series}[0]{values}
。
哦,后來, query()
方法的文檔說:
如果返回的對象評估為true,表示查詢成功,則返回的對象的
data
屬性包含InfluxDB的整個響應(作為Perl哈希值)。 此外,屬性request_id
提供了InfluxDB在HTTP響應標頭中設置的請求標識符。 例如,這對於將請求與日志文件關聯起來很有用。
雖然沒有提及results
屬性。
Object returned by call to InfluxDB::HTTP::query() ... can't be used as <HASH>
是$res
是對象的線索。 請查閱相關的API,以查看對對象進行哪些操作以訪問其數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.