Skip to content

Commit

Permalink
add influx db module
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Apr 30, 2019
1 parent eef327b commit 1248905
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath/> <!-- lookup parent from update -->
</parent>

<properties>
Expand Down
5 changes: 5 additions & 0 deletions spring-boot/130-influxdb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
<artifactId>spring-data-influxdb</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.15</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.git.hui.boot.influx;

import com.git.hui.boot.influx.delete.DeleteService;
import com.git.hui.boot.influx.insert.InsertService;
import com.git.hui.boot.influx.query.QueryService;
import com.git.hui.boot.influx.update.UpdateService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Created by @author yihui in 16:49 19/4/30.
*/
@SpringBootApplication
public class Application {

private InsertService insertService;
private QueryService queryService;
private UpdateService updateService;
private DeleteService deleteService;

public Application(InsertService insertService, QueryService queryService, UpdateService updateService,
DeleteService deleteService) {
this.insertService = insertService;
this.queryService = queryService;
this.updateService = updateService;
this.deleteService = deleteService;

insert();
// query();
// update();
// delete();
}

private void insert() {
insertService.time();
// insertService.add();
// insertService.batchAdd();
}

private void query() {
queryService.query();
}

private void update() {
updateService.update();
}

private void delete() {
deleteService.delete();
}

public static void main(String[] args) {
SpringApplication.run(Application.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.git.hui.boot.influx.config;

import org.influxdb.dto.Point;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.influxdb.DefaultInfluxDBTemplate;
import org.springframework.data.influxdb.InfluxDBConnectionFactory;
import org.springframework.data.influxdb.InfluxDBProperties;
import org.springframework.data.influxdb.InfluxDBTemplate;
import org.springframework.data.influxdb.converter.PointConverter;

/**
* Created by @author yihui in 16:51 19/4/30.
*/
@Configuration
@EnableConfigurationProperties(InfluxDBProperties.class)
public class InfluxAutoConfiguration {
@Bean
public InfluxDBConnectionFactory connectionFactory(final InfluxDBProperties properties) {
return new InfluxDBConnectionFactory(properties);
}

@Bean
public InfluxDBTemplate<Point> influxDBTemplate(final InfluxDBConnectionFactory connectionFactory) {
/*
* You can use your own 'PointCollectionConverter' implementation, e.g. in case
* you want to use your own custom measurement object.
*/
return new InfluxDBTemplate<>(connectionFactory, new PointConverter());
}

@Bean
public DefaultInfluxDBTemplate defaultTemplate(final InfluxDBConnectionFactory connectionFactory) {
/*
* If you are just dealing with Point objects from 'influxdb-java' you could
* also use an instance of class DefaultInfluxDBTemplate.
*/
return new DefaultInfluxDBTemplate(connectionFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.git.hui.boot.influx.delete;

import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.influxdb.InfluxDBTemplate;
import org.springframework.stereotype.Service;

/**
* Created by @author yihui in 18:57 19/4/30.
*/
@Service
public class DeleteService {
@Autowired
private InfluxDBTemplate influxDBTemplate;


public void delete() {
Query query = new Query("delete from kline_1_day where id='2'");
QueryResult result = influxDBTemplate.query(query);
System.out.println(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.git.hui.boot.influx.insert;

import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.influxdb.InfluxDBTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
* Created by @author yihui in 16:53 19/4/30.
*/
@Service
public class InsertService {

@Autowired
private InfluxDBTemplate<Point> influxDBTemplate;

public static BigDecimal formatDecimal(double num) {
return new BigDecimal(num).setScale(8, RoundingMode.CEILING);
}

public void add() {
// 以行情的kline为实例
Point point = Point.measurement("kline_1_day").tag("id", "1").tag("date", "2019-01-10 08:00:00")
.addField("open", formatDecimal(1.213)).addField("close", formatDecimal(1.32))
.addField("high", formatDecimal(1.52143132424)).addField("low", formatDecimal(1.000123))
.addField("amount", formatDecimal(13409834.1341234))
.addField("volume", formatDecimal(123897489131234.3214)).build();
influxDBTemplate.write(point);

Query query = new Query("select * from kline_1_day where id='1' and date='2019-01-10 08:00:00'", "hhui");
QueryResult result = influxDBTemplate.query(query);
System.out.println(result);
}


public void batchAdd() {
// 以行情的kline为实例
Point point = Point.measurement("kline_1_day").tag("id", "2").tag("date", "2019-01-10 08:00:00")
.addField("open", formatDecimal(1.213)).addField("close", formatDecimal(1.32))
.addField("high", formatDecimal(1.52143132424)).addField("low", formatDecimal(1.000123))
.addField("amount", formatDecimal(13409834.1341234))
.addField("volume", formatDecimal(123897489131234.3214)).build();

Point point2 = Point.measurement("kline_1_day").tag("id", "3").tag("date", "2019-01-10 08:00:00")
.addField("open", formatDecimal(1.213)).addField("close", formatDecimal(1.32))
.addField("high", formatDecimal(1.52143132424)).addField("low", formatDecimal(1.000123))
.addField("amount", formatDecimal(13409834.1341234))
.addField("volume", formatDecimal(123897489131234.3214)).build();
influxDBTemplate.write(Arrays.asList(point, point2));
}

public void time() {
// 以行情的kline为实例
Point point = Point.measurement("kline_1_day").tag("id", "3").time(1547078400000L, TimeUnit.MILLISECONDS)
.addField("open", formatDecimal(1.213)).addField("close", formatDecimal(1.32))
.addField("high", formatDecimal(1.52143132424)).addField("low", formatDecimal(1.000123))
.addField("amount", formatDecimal(13409834.1341234))
.addField("volume", formatDecimal(123897489131234.3214)).build();
influxDBTemplate.write(point);

Query query = new Query("select * from kline_1_day where id='3'", "hhui");
QueryResult result = influxDBTemplate.query(query);
System.out.println(result);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.git.hui.boot.influx.query;

import lombok.Data;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import org.influxdb.dto.BoundParameterQuery;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.influxdb.InfluxDBTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* Created by @author yihui in 18:15 19/4/30.
*/
@Service
public class QueryService {
@Autowired
private InfluxDBTemplate influxDBTemplate;

public void query() {
basicQuery();
basicQueryAndParsePOJO();
queryByTemplateSql();
}

private void basicQuery() {
Query query = new Query("select * from kline_1_day where id='1' and date='2019-01-10 08:00:00'", "hhui");
QueryResult result = influxDBTemplate.query(query);
System.out.println(result);
}

@Data
@Measurement(name = "kline_1_day")
public static class Kline1Day {
@Column(name = "open")
private Double open;
@Column(name = "close")
private Double close;
@Column(name = "high")
private Double high;
@Column(name = "low")
private Double low;
@Column(name = "amount")
private Double amount;
@Column(name = "volume")
private Double volume;

@Column(name = "date", tag = true)
private String date;
@Column(name = "id", tag = true)
private String id;
}

private void basicQueryAndParsePOJO() {
try {
// fixme 这里不支持BigDecimal类型的转换,通过源码,POJO中的类型只支持 String, Long, Double, Integer
// fixme 类型必须和db中的完全一致

Query query = new Query("select * from kline_1_day where id='1' and date='2019-01-10 08:00:00'", "hhui");
QueryResult result = influxDBTemplate.query(query);
InfluxDBResultMapper mapper = new InfluxDBResultMapper();
List<Kline1Day> res = mapper.toPOJO(result, Kline1Day.class);
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
}
}

private void queryByTemplateSql() {
Query query = BoundParameterQuery.QueryBuilder.newQuery("select * from kline_1_day where id=$id and date=$date")
.forDatabase("hhui").bind("id", "1").bind("date", "2019-01-10 08:00:00").create();

QueryResult results = influxDBTemplate.query(query);
System.out.println(results);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.git.hui.boot.influx.schema;

import org.influxdb.dto.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.influxdb.InfluxDBTemplate;
import org.springframework.stereotype.Service;

/**
* Created by @author yihui in 16:58 19/4/30.
*/
@Service
public class SchemaService {
@Autowired
private InfluxDBTemplate<Point> influxDBTemplate;

public void createDatabase() {
influxDBTemplate.createDatabase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.git.hui.boot.influx.update;

import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.influxdb.InfluxDBTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.git.hui.boot.influx.insert.InsertService.formatDecimal;

/**
* Created by @author yihui in 19:08 19/4/30.
*/
@Service
public class UpdateService {
@Autowired
private InfluxDBTemplate<Point> influxDBTemplate;

/**
* 插入一个tag完全相同, 以及时间戳也完全相同, 就表示覆盖数据
*/
public void update() {
Point point = Point.measurement("kline_1_day").tag("id", "3").time(1547078400000L, TimeUnit.MILLISECONDS)
.addField("open", formatDecimal(1.213)).addField("close", formatDecimal(1.32))
.addField("high", formatDecimal(1.52143132424)).addField("low", formatDecimal(1.000123))
.addField("amount", formatDecimal(200)).addField("volume", formatDecimal(200)).build();
influxDBTemplate.write(point);

Query query = new Query("select * from kline_1_day where id='3'", "hhui");
QueryResult result = influxDBTemplate.query(query);
System.out.println(result);
}
}
11 changes: 11 additions & 0 deletions spring-boot/130-influxdb/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
influxdb:
url: http://localhost:8086
username: admin
password:
database: hhui
retention-policy: autogen
connect-timeout: 10
read-timeout: 30
write-timeout: 10
gzip: true

0 comments on commit 1248905

Please sign in to comment.