Yii2安装使用InfluxDB

InfluxDB 是一个时间序列数据库,用于处理海量写入与负载查询,本文介绍PHP项目中如何使用InfluxDB,并且将基本操作封装成类使用,

Yii2安装使用InfluxDB
1、安装influxdb-php

在项目中打开cmder,执行如下命令,电脑必须安装了composer

composer require influxdb/influxdb-php
2、简单的操作
创建链接对象
方式一
$client = new InfluxDB\Client($host, $port);
方式二
$database = InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname));
$client = $database->getClient();
设置查询数据库
$database = $client->selectDB('testdb');
执行查询语句
$result = $database->query('select * from test LIMIT 5');
将结果集转成一个数组
$points = $result->getPoints();
可以使用getQueryBuilder简化查询
$result = $database->getQueryBuilder()
	->select('cpucount')
	->from('test')
	->limit(2)
	->offset(2)
	->getResultSet()
	->getPoints();
$query = $database->getQueryBuilder()
	->select('cpucount')
	->from('test')
	->where(["region = 'us-west'"])
	->getQuery();
添加数据
$points = array(
	new Point(
		'test', // name of the measurement
		0.64, // the measurement value
		['host' => 'server01', 'region' => 'us-west'], // optional tags
		['cpucount' => 10], // optional additional fields
		1435255849 // Time precision has to be set to seconds!
	),
    new Point(
    	'test', // name of the measurement
		0.84, // the measurement value
		['host' => 'server01', 'region' => 'us-west'], // optional tags
		['cpucount' => 10], // optional additional fields
		1435255849 // Time precision has to be set to seconds!
	)
);
$result = $database->writePoints($points, Database::PRECISION_SECONDS);
3、封装成类使用

详细的到一下地址查看

上一篇:Window环境下安装并使用InfluxDB 下一篇:解决Nginx自动忽略header包含下划线参数方法

评论

评论(0)

暂无评论