Skip to content

Commit

Permalink
feat(influxdb): influxdb时序数据库使用
Browse files Browse the repository at this point in the history
Signed-off-by: yangzl <[email protected]>
  • Loading branch information
youngzil committed Jun 11, 2020
1 parent a60ae54 commit d94785f
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 60 deletions.
4 changes: 2 additions & 2 deletions docs/SQL/数据库索引.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ http://jaskey.github.io/blog/2016/01/19/mysql-bad-sql-with-no-index/
oracle的行级锁与表级锁
https://www.cnblogs.com/li1111xin/p/4775240.html

悲观锁(加的是表级锁)
Pessimistic locking(悲观锁(加的是表级锁)
一方:查询语句加 for update;另一方:查询语句加 for update;当进行更新语句的时候,另一方不能进行更新操作

乐观锁
Optimistic locking(乐观锁
更新语句设置版本号或者时间,在指定版本中更新数据

如果更新多,查询少,用悲观锁;反之,乐观锁
Expand Down
15 changes: 11 additions & 4 deletions docs/influxdb/influxdb学习.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
1、
2、
3、
1、InfluxDB的关键概念
2、InfluxDB提供三种操作方式:
3、influxdb时序数据库安装
4、
5、

Expand Down Expand Up @@ -38,7 +38,7 @@ point,就是某个series的同一个时刻的多个field的value,就组成
InfluxDb不需要做schema定义,这意味着你可以随意的添加measurements, tags, and fields at any time,



---------------------------------------------------------------------------------------------------------------------
InfluxDB提供三种操作方式:
1)客户端命令行方式
2)HTTP API接口
Expand Down Expand Up @@ -175,17 +175,24 @@ InfluxDB会计算按照时间进行排序的字段值之间的差异,并将这


---------------------------------------------------------------------------------------------------------------------

influxdb时序数据库安装


时序数据库:Time Series Database (TSDB)
https://github.com/influxdata/influxdb
https://docs.influxdata.com/influxdb/v1.7/introduction/getting-started/
https://docs.influxdata.com/influxdb/v1.7/administration/config

Java客户端
https://github.com/influxdata/influxdb-java



安装
brew update
brew install influxdb
brew upgrade influxdb


服务端是influxd
Expand Down
4 changes: 4 additions & 0 deletions docs/数据库事务/数据库事务.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ https://www.zhihu.com/question/30272728
原子性并不能完全保证一致性。在多个事务并行进行的情况下,即使保证了每一个事务的原子性,仍然可能导致数据不一致的结果。
为了保证并发情况下的一致性,引入了隔离性,实现隔离性原则上无非是两种类型的锁:


Pessimistic locking(悲观锁) 与 Optimistic locking(乐观锁)

一种是悲观锁,即当前事务将所有涉及操作的对象加锁,操作完成后释放给其它对象使用。为了尽可能提高性能,发明了各种粒度(数据库级/表级/行级……)/各种性质(共享锁/排他锁/共享意向锁/排他意向锁/共享排他意向锁……)的锁。为了解决死锁问题,又发明了两阶段锁协议/死锁检测等一系列的技术。

一种是乐观锁,即不同的事务可以同时看到同一对象(一般是数据行)的不同历史版本。如果有两个事务同时修改了同一数据行,那么在较晚的事务提交时进行冲突检测。实现也有两种,一种是通过日志UNDO的方式来获取数据行的历史版本,一种是简单地在内存中保存同一数据行的多个历史版本,通过时间戳来区分。
Expand Down Expand Up @@ -128,6 +131,7 @@ http://blog.csdn.net/gaopu12345/article/details/50868501
http://blog.csdn.net/tolcf/article/details/49283575

MySQL的默认隔离级别就是Repeatable read。
MySQL 默认的是:可重复读(Repeatable read)

数据库事务的隔离级别有4个,由低到高依次为
Read uncommitted(未授权读取、读未提交)、可能出现脏读。也就是说事务B读取到了事务A未提交的数据。
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<packaging>pom</packaging>
<name>${project.artifactId}-${project.version}</name>
<url>http://maven.apache.org</url>
<description>Demo project for Spring Boot Admin</description>
<description>Demo project for XXX</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
22 changes: 22 additions & 0 deletions quickstart-influxdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
时序数据库:Time Series Database (TSDB)
https://github.com/influxdata/influxdb
https://docs.influxdata.com/influxdb/v1.7/introduction/getting-started/
https://docs.influxdata.com/influxdb/v1.7/administration/config

Java客户端
https://github.com/influxdata/influxdb-java















4 changes: 1 addition & 3 deletions quickstart-influxdb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.15</version>
<version>2.19</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -67,8 +67,6 @@

<!-- logback end -->



</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.quickstart.influxdb.example;

import java.util.concurrent.TimeUnit;
import org.influxdb.BatchOptions;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;

/**
* @author [email protected]
* @description TODO
* @createTime 2020/6/11 22:48
*/
public class InfluxDBTest {

public static void main(String[] args) throws InterruptedException {
// Create an object to handle the communication with InfluxDB.
// (best practice tip: reuse the 'influxDB' instance when possible)
final String serverURL = "http://127.0.0.1:8086", username = "root", password = "root";
final InfluxDB influxDB = InfluxDBFactory.connect(serverURL, username, password);

// Create a database...
// https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/
String databaseName = "NOAA_water_database";
influxDB.query(new Query("CREATE DATABASE " + databaseName));
influxDB.setDatabase(databaseName);

// ... and a retention policy, if necessary.
// https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/
String retentionPolicyName = "one_day_only";
influxDB.query(new Query("CREATE RETENTION POLICY " + retentionPolicyName
+ " ON " + databaseName + " DURATION 1d REPLICATION 1 DEFAULT"));
influxDB.setRetentionPolicy(retentionPolicyName);

// Enable batch writes to get better performance.
influxDB.enableBatch(BatchOptions.DEFAULTS);

// Write points to InfluxDB.
influxDB.write(Point.measurement("h2o_feet")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.tag("location", "santa_monica")
.addField("level description", "below 3 feet")
.addField("water_level", 2.064d)
.build());

influxDB.write(Point.measurement("h2o_feet")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.tag("location", "coyote_creek")
.addField("level description", "between 6 and 9 feet")
.addField("water_level", 8.12d)
.build());

// Wait a few seconds in order to let the InfluxDB client
// write your points asynchronously (note: you can adjust the
// internal time interval if you need via 'enableBatch' call).
Thread.sleep(5_000L);

// Query your data using InfluxQL.
// https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/#the-basic-select-statement
QueryResult queryResult = influxDB.query(new Query("SELECT * FROM h2o_feet"));

System.out.println(queryResult);
// It will print something like:
// QueryResult [results=[Result [series=[Series [name=h2o_feet, tags=null,
// columns=[time, level description, location, water_level],
// values=[
// [2020-03-22T20:50:12.929Z, below 3 feet, santa_monica, 2.064],
// [2020-03-22T20:50:12.929Z, between 6 and 9 feet, coyote_creek, 8.12]
// ]]], error=null]], error=null]

// Close it if your application is terminating or you are not using it anymore.
influxDB.close();
}


}
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
package org.quickstart.influxdb.example;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDB.ResponseFormat;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Pong;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InfluxdbClientTest {
import okhttp3.OkHttpClient;

//参考https://www.baeldung.com/java-influxdb
public class InfluxdbClientTest {

// 参考https://www.baeldung.com/java-influxdb

private final static Logger logger = LoggerFactory.getLogger(InfluxdbClientTest.class);


public static void main(String[] args) {
String databaseURL = "http://" + "127.0.0.1" + ":" + "8086";
String userName = null;
String password = null;

InfluxDB influxDB = InfluxDBFactory.connect(databaseURL);
//InfluxDB influxDB = InfluxDBFactory.connect(databaseURL, userName, password);
// InfluxDB influxDB = InfluxDBFactory.connect(databaseURL, userName, password);

//Verifying the Connection
// Verifying the Connection
Pong response = influxDB.ping();
if (response.getVersion().equalsIgnoreCase("unknown")) {
logger.error("Error pinging server.");
Expand All @@ -40,43 +37,25 @@ public static void main(String[] args) {
logger.debug("success");
}

//Creating a Database
//To create a retention policy
// Creating a Database
// To create a retention policy
String dbName = "baeldung";
influxDB.createDatabase(dbName);
influxDB.createRetentionPolicy("defaultPolicy", "baeldung", "30d", 1, true);

//Setting a Logging Level
// Setting a Logging Level
influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);

Point point = Point.measurement("memory")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField("name", "server1")
.addField("free", 4743656L)
.addField("used", 1015096L)
.addField("buffer", 1010467L)
.build();

BatchPoints batchPoints = BatchPoints
.database(dbName)
.retentionPolicy("defaultPolicy")
.build();

Point point1 = Point.measurement("memory")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField("name", "server1")
.addField("free", 4743656L)
.addField("used", 1015096L)
.addField("buffer", 1010467L)
.build();

Point point2 = Point.measurement("memory")
.time(System.currentTimeMillis() - 100, TimeUnit.MILLISECONDS)
.addField("name", "server1")
.addField("free", 4743696L)
.addField("used", 1016096L)
.addField("buffer", 1008467L)
.build();
Point point = Point.measurement("memory").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS).addField("name", "server1")
.addField("free", 4743656L).addField("used", 1015096L).addField("buffer", 1010467L).build();

BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy("defaultPolicy").build();

Point point1 = Point.measurement("memory").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS).addField("name", "server1")
.addField("free", 4743656L).addField("used", 1015096L).addField("buffer", 1010467L).build();

Point point2 = Point.measurement("memory").time(System.currentTimeMillis() - 100, TimeUnit.MILLISECONDS).addField("name", "server1")
.addField("free", 4743696L).addField("used", 1016096L).addField("buffer", 1008467L).build();

batchPoints.point(point1);
batchPoints.point(point2);
Expand All @@ -95,9 +74,8 @@ public static void main(String[] args) {

}


public static InfluxDB connectToInfluxDB(final OkHttpClient.Builder client, String apiUrl,
ResponseFormat responseFormat) throws InterruptedException, IOException {
public static InfluxDB connectToInfluxDB(final OkHttpClient.Builder client, String apiUrl, ResponseFormat responseFormat)
throws InterruptedException, IOException {
OkHttpClient.Builder clientToUse;
if (client == null) {
clientToUse = new OkHttpClient.Builder();
Expand All @@ -110,8 +88,7 @@ public static InfluxDB connectToInfluxDB(final OkHttpClient.Builder client, Stri
} else {
apiUrlToUse = apiUrl;
}
InfluxDB influxDB = InfluxDBFactory
.connect(apiUrlToUse, "admin", "admin", clientToUse, responseFormat);
InfluxDB influxDB = InfluxDBFactory.connect(apiUrlToUse, "admin", "admin", clientToUse, responseFormat);
boolean influxDBstarted = false;
do {
Pong response;
Expand All @@ -127,13 +104,10 @@ public static InfluxDB connectToInfluxDB(final OkHttpClient.Builder client, Stri
Thread.sleep(100L);
} while (!influxDBstarted);
influxDB.setLogLevel(InfluxDB.LogLevel.NONE);
System.out.println(
"##################################################################################");
System.out.println("##################################################################################");
System.out.println("# Connected to InfluxDB Version: " + influxDB.version() + " #");
System.out.println(
"##################################################################################");
System.out.println("##################################################################################");
return influxDB;
}


}

0 comments on commit d94785f

Please sign in to comment.