-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
347 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
spring-boot/130-influxdb/src/main/java/com/git/hui/boot/influx/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...ot/130-influxdb/src/main/java/com/git/hui/boot/influx/config/InfluxAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
spring-boot/130-influxdb/src/main/java/com/git/hui/boot/influx/delete/DeleteService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
spring-boot/130-influxdb/src/main/java/com/git/hui/boot/influx/insert/InsertService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
spring-boot/130-influxdb/src/main/java/com/git/hui/boot/influx/query/QueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
spring-boot/130-influxdb/src/main/java/com/git/hui/boot/influx/schema/SchemaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
spring-boot/130-influxdb/src/main/java/com/git/hui/boot/influx/update/UpdateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
spring-boot/130-influxdb/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |