# InfluxDB

공식 <https://www.influxdata.com/>

pom.xml

```xml
<dependency>
    <groupId>org.influxdb</groupId>
    <artifactId>influxdb-java</artifactId>
</dependency>
```

application.properties

```
spring.influx.url=http://localhost:8086
spring.influx.user=admin
spring.influx.password=
```

{% hint style="info" %}
spring data 를 지원하지 않지만. springboot 2.x 에서 influx DB 를 자동으로 구성할수 있게 되어 있기때문에 구성정보만 입력해주면 된다.

org.springframework.boot.autoconfigure.influx.InfluxDbProperties 를 찾아보면 구체적 방법알수 있다.
{% endhint %}

서비스 클래스 작성

```java
@Service
@AllArgsConstructor
@Slf4j
public class Monitor {

    private InfluxDB influxDB;

    @Scheduled(fixedRate = 5000)
    public void writeQPS() {
        // 가상  report 데이터 
        int count = (int) (Math.random() * 100);

        Point point = Point.measurement("ApiQPS")     // ApiQPS 테이블
                .tag("url", "/hello")  // url
                .addField("count", count)        // 통계
                .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)  // 时间
                .build();

        // test database 에 저장
        influxDB.write("test", "autogen", point);

        log.info("총 건수：" + count);
    }

}
```

#### &#x20;접속

```
$ influx
> show databases
> create database "test"
```

#### 로그 확인

```

2022-12-27 01:52:47.732  INFO 94110 --- [           main] c.d.demo.InfluxDBApplication       : Started InfluxDBApplication in 2.326 seconds (JVM running for 3.027)
2022-12-27 01:52:47.764  INFO 94110 --- [   scheduling-1] com.blake.demo.Monitor          : 총 건수：25
2022-12-27 01:52:52.736  INFO 94110 --- [   scheduling-1] com.blake.demo.Monitor          : 총 건수：30
2022-12-27 01:52:57.737  INFO 94110 --- [   scheduling-1] com.blake.demo.Monitor          : 총 건수：38
2022-12-27 01:53:02.739  INFO 94110 --- [   scheduling-1] com.blake.demo.Monitor          : 총 건수：51
2022-12-27 01:53:07.739  INFO 94110 --- [   scheduling-1] com.blake.demo.Monitor          : 총 건수：31
```

#### 입력된 데이터를 확인

```
> select * from ApiQPS order by time desc;

name: ApiQPS
time                count url
----                ----- ---
1627926787730000000 31    /hello
1627926782730000000 51    /hello
1627926777729000000 38    /hello
1627926772727000000 30    /hello
1627926767728000000 25    /hello

```

끝!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blakes-organization.gitbook.io/rainsister/springboot-2.x/database/influxdb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
