提交 e2026265 编写于 作者: Z zyyang-taosdata

[TD-1643]<test>: springboot demo for rainstation data use tdengine

上级 635583e6
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
<dependency> <dependency>
<groupId>com.taosdata.jdbc</groupId> <groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId> <artifactId>taos-jdbcdriver</artifactId>
<version>2.0.4</version> <version>2.0.8</version>
</dependency> </dependency>
<dependency> <dependency>
...@@ -71,8 +71,6 @@ ...@@ -71,8 +71,6 @@
<artifactId>druid-spring-boot-starter</artifactId> <artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version> <version>1.1.17</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
```properties ```properties
# datasource config # datasource config
spring.datasource.driver-class-name=com.taosdata.jdbc.TSDBDriver spring.datasource.driver-class-name=com.taosdata.jdbc.TSDBDriver
spring.datasource.url=jdbc:TAOS://127.0.0.1:6030/log spring.datasource.url=jdbc:TAOS://192.168.1.59:6030/rainstation
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=taosdata spring.datasource.password=taosdata
......
package com.taosdata.jdbc.springbootdemo.controller; package com.taosdata.jdbc.springbootdemo.controller;
import com.taosdata.jdbc.springbootdemo.domain.Rainfall; import com.taosdata.jdbc.springbootdemo.domain.Rainfall;
import com.taosdata.jdbc.springbootdemo.service.RainStationService; import com.taosdata.jdbc.springbootdemo.service.RainStationService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RestController
@RequestMapping("/rainstation") @RequestMapping("/rainstation")
public class RainStationController { public class RainStationController {
@Autowired @Autowired
private RainStationService service; private RainStationService service;
private int keep = 7665;
private int days = 120;
private int blocks = 30;
@GetMapping("/init") @GetMapping("/init")
public boolean init() { public boolean init(@RequestParam("stationCount") int stationCount) {
service.init(); // create database rainstation keep 7665 days 120 blocks 30
service.createTable(); service.createDb(keep, days, blocks);
// create table monitoring(ts timestamp, rainfall float) tags(station_code BINARY(8), station_name NCHAR(10))
service.createSuperTable();
// create table s_xxx using monitoring tags("s_xxxx","北京");
service.createSubTables(stationCount);
return true; return true;
} }
@PostMapping("/insert") @GetMapping("/generate")
public int insert(@RequestBody Rainfall rainfall){ public long generate(@RequestParam("stationCount") int stationCount, @RequestParam("timeGap") int timeGap, @RequestParam("batchSize") int batchSize) {
return service.insert(rainfall); return service.generate(keep, stationCount, timeGap, batchSize);
}
@GetMapping("/latestHour")
public List<Rainfall> latestHour() {
return service.latestHour();
}
@GetMapping("/latestDay")
public List<Rainfall> latestDay() {
return service.latestDay();
}
@GetMapping("/latestWeek")
public List<Rainfall> latestWeek() {
return service.latestWeek();
}
@GetMapping("/latestMonth")
public List<Rainfall> latestMonth() {
return service.latestMonth();
}
@GetMapping("/latestYear")
public List<Rainfall> latestYear() {
return service.latestYear();
}
@DeleteMapping
public boolean dropDatabase() {
return service.dropDatabase();
} }
} }
...@@ -57,7 +57,6 @@ public class WeatherController { ...@@ -57,7 +57,6 @@ public class WeatherController {
*/ */
@PostMapping("/batch") @PostMapping("/batch")
public int batchSaveWeather(@RequestBody List<Weather> weatherList) { public int batchSaveWeather(@RequestBody List<Weather> weatherList) {
return weatherService.save(weatherList); return weatherService.save(weatherList);
} }
......
package com.taosdata.jdbc.springbootdemo.dao;
import java.util.Map;
public interface DatabaseMapper {
int createDatabase(String dbname);
int dropDatabase(String dbname);
int creatDatabaseWithParameters(Map<String,String> map);
int useDatabase(String dbname);
}
package com.taosdata.jdbc.springbootdemo.dao;
import java.util.Map;
public interface RainfallMapper {
int save(Map<String, Object> map);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.taosdata.jdbc.springbootdemo.dao.RainfallMapper">
<insert id="save" parameterType="map">
INSERT INTO ${table} using ${dbname}.${stable} tags(#{values.station_code}, #{values.station_name}) (ts, name, code, rainfall) values (#{values.ts}, #{values.name}, #{values.code}, #{values.rainfall})
</insert>
</mapper>
\ No newline at end of file
package com.taosdata.jdbc.springbootdemo.dao;
import com.taosdata.jdbc.springbootdemo.domain.Rainfall;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface RainstationMapper {
int dropDatabase(String dbname);
int creatDatabase(Map<String, String> map);
int useDatabase(String dbname);
boolean createSuperTable(String dbname, String stbName);
boolean createSubTable(String tableName, String stationCode, String stationName);
int insertOne(Rainfall rainfall);
int insertMany(@Param("tableName") String tableName, @Param("rainfalls") List<Rainfall> rainfallList);
List<Rainfall> findAll(Map<String, Object> condi);
}
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.taosdata.jdbc.springbootdemo.dao.DatabaseMapper"> <mapper namespace="com.taosdata.jdbc.springbootdemo.dao.RainstationMapper">
<update id="createDatabase" parameterType="java.lang.String">
create database if not exists ${dbname}
</update>
<update id="dropDatabase" parameterType="java.lang.String"> <update id="dropDatabase" parameterType="java.lang.String">
DROP database if exists ${dbname} DROP database if exists ${dbname}
</update> </update>
<update id="creatDatabase" parameterType="map">
<update id="creatDatabaseWithParameters" parameterType="map">
CREATE database if not EXISTS ${dbname} CREATE database if not EXISTS ${dbname}
<if test="keep != null"> <if test="keep != null">
KEEP ${keep} KEEP ${keep}
...@@ -41,4 +36,36 @@ ...@@ -41,4 +36,36 @@
use ${dbname} use ${dbname}
</update> </update>
<update id="createSuperTable">
create table if not exists ${dbname}.${stbName} (ts timestamp, rainfall float) tags(station_code BINARY(8), station_name NCHAR(10))
</update>
<update id="createSubTable">
create table if not exists rainstation.${tableName} using rainstation.monitoring tags(#{stationCode}, #{stationName})
</update>
<update id="dropTable" parameterType="java.lang.String">
drop ${tablename}
</update>
<insert id="insertOne">
INSERT INTO ${station_code} (ts, rainfall) values (#{ts}, #{rainfall})
</insert>
<insert id="insertMany">
insert into ${tableName} (ts, rainfall) values
<foreach collection="rainfalls" item="item" index="index" separator=" ">
( #{item.ts}, #{item.rainfall} )
</foreach>
</insert>
<select id="findAll" parameterType="map" resultType="com.taosdata.jdbc.springbootdemo.domain.Rainfall">
select * from rainstation.monitoring
<if test="startTime != null">
where <![CDATA[ ts >= #{startTime} ]]>
</if>
<if test="endTime != null">
and <![CDATA[ ts < #{endTime} ]]>
</if>
</select>
</mapper> </mapper>
\ No newline at end of file
package com.taosdata.jdbc.springbootdemo.dao;
import com.taosdata.jdbc.springbootdemo.domain.TableMetadata;
public interface TableMapper {
boolean createSTable(TableMetadata tableMetadata);
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.taosdata.jdbc.springbootdemo.dao.TableMapper">
<update id="createSTable" parameterType="com.taosdata.jdbc.springbootdemo.domain.TableMetadata">
create table if not exists ${dbname}.${tablename}
<foreach collection="fields" item="field" index="index" open="(" close=")" separator=",">
${field.name} ${field.type}
</foreach>
TAGS
<foreach collection="tags" item="tag" index="index" open="(" close=")" separator=",">
${tag.name} ${tag.type}
</foreach>
</update>
<update id="dropTable" parameterType="java.lang.String">
drop ${tablename}
</update>
</mapper>
\ No newline at end of file
package com.taosdata.jdbc.springbootdemo.domain;
public class FieldMetadata {
private String name;
private String type;
public FieldMetadata(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
...@@ -6,10 +6,8 @@ import java.sql.Timestamp; ...@@ -6,10 +6,8 @@ import java.sql.Timestamp;
public class Rainfall { public class Rainfall {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS",timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
private Timestamp ts; private Timestamp ts;
private String name;
private String code;
private float rainfall; private float rainfall;
private String station_code; private String station_code;
private String station_name; private String station_name;
...@@ -22,22 +20,6 @@ public class Rainfall { ...@@ -22,22 +20,6 @@ public class Rainfall {
this.ts = ts; this.ts = ts;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public float getRainfall() { public float getRainfall() {
return rainfall; return rainfall;
} }
......
package com.taosdata.jdbc.springbootdemo.domain;
import java.util.List;
public class TableMetadata {
private String dbname;
private String tablename;
private List<FieldMetadata> fields;
private List<TagMetadata> tags;
public String getDbname() {
return dbname;
}
public void setDbname(String dbname) {
this.dbname = dbname;
}
public String getTablename() {
return tablename;
}
public void setTablename(String tablename) {
this.tablename = tablename;
}
public List<FieldMetadata> getFields() {
return fields;
}
public void setFields(List<FieldMetadata> fields) {
this.fields = fields;
}
public List<TagMetadata> getTags() {
return tags;
}
public void setTags(List<TagMetadata> tags) {
this.tags = tags;
}
}
package com.taosdata.jdbc.springbootdemo.domain;
public class TagMetadata {
private String name;
private String type;
public TagMetadata(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package com.taosdata.jdbc.springbootdemo.service; package com.taosdata.jdbc.springbootdemo.service;
import com.taosdata.jdbc.springbootdemo.dao.DatabaseMapper; import com.taosdata.jdbc.springbootdemo.dao.RainstationMapper;
import com.taosdata.jdbc.springbootdemo.dao.RainfallMapper;
import com.taosdata.jdbc.springbootdemo.dao.TableMapper;
import com.taosdata.jdbc.springbootdemo.domain.FieldMetadata;
import com.taosdata.jdbc.springbootdemo.domain.Rainfall; import com.taosdata.jdbc.springbootdemo.domain.Rainfall;
import com.taosdata.jdbc.springbootdemo.domain.TableMetadata;
import com.taosdata.jdbc.springbootdemo.domain.TagMetadata;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.sql.Timestamp;
import java.util.HashMap; import java.time.Duration;
import java.util.List; import java.time.Instant;
import java.util.Map; import java.util.*;
@Service @Service
public class RainStationService { public class RainStationService {
@Autowired @Autowired
private DatabaseMapper databaseMapper; private RainstationMapper rainstationMapper;
@Autowired
private TableMapper tableMapper;
@Autowired
private RainfallMapper rainfallMapper;
public boolean init() { private Random random = new Random(System.currentTimeMillis());
databaseMapper.dropDatabase("rainstation"); private String[] stationNames = {"北京", "上海", "深圳", "广州", "杭州", "苏州", "成都", "西安", "天津", "厦门", "福州"};
public boolean createDb(int keep, int days, int blocks) {
rainstationMapper.dropDatabase("rainstation");
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("dbname", "rainstation"); map.put("dbname", "rainstation");
map.put("keep", "36500"); map.put("keep", "" + keep);
map.put("days", "30"); map.put("days", "" + days);
map.put("blocks", "4"); map.put("blocks", "" + blocks);
databaseMapper.creatDatabaseWithParameters(map); rainstationMapper.creatDatabase(map);
rainstationMapper.useDatabase("rainstation");
return true;
}
databaseMapper.useDatabase("rainstation"); public boolean createSuperTable() {
try {
rainstationMapper.createSuperTable("rainstation", "monitoring");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true; return true;
} }
public boolean createTable() { // create table s_xxx using monitoring tags("s_xxxx","北京");
TableMetadata tableMetadata = new TableMetadata(); public void createSubTables(int stationCount) {
tableMetadata.setDbname("rainstation"); for (int i = 0; i < stationCount; i++) {
tableMetadata.setTablename("monitoring"); rainstationMapper.createSubTable("s_" + i, "s_" + i, stationNames[random.nextInt(stationNames.length)]);
}
}
List<FieldMetadata> fields = new ArrayList<>(); public boolean dropDatabase() {
fields.add(new FieldMetadata("ts", "timestamp")); try {
fields.add(new FieldMetadata("name", "NCHAR(10)")); rainstationMapper.dropDatabase("rainstation");
fields.add(new FieldMetadata("code", " BINARY(8)")); } catch (Exception e) {
fields.add(new FieldMetadata("rainfall", "float")); e.printStackTrace();
tableMetadata.setFields(fields); return false;
}
return true;
}
List<TagMetadata> tags = new ArrayList<>(); public long generate(int keep, int stationCount, long timeGapSec, int batchSize) {
tags.add(new TagMetadata("station_code", "BINARY(8)")); rainstationMapper.useDatabase("rainstation");
tags.add(new TagMetadata("station_name", "NCHAR(10)")); Instant end = Instant.now();
tableMetadata.setTags(tags); Instant start = end.minus(Duration.ofDays(keep - 1));
final long timeGap = 1000l * timeGapSec;
tableMapper.createSTable(tableMetadata); if (stationCount < 1000) {
return true; return insert(start.toEpochMilli(), end.toEpochMilli(), timeGap, stationCount);
} else {
return batchInsert(start.toEpochMilli(), end.toEpochMilli(), timeGap, batchSize, stationCount);
}
}
private long insert(long startTime, long endTime, long timeGap, int tableSize) {
long count = 0;
for (long ts = startTime; ts < endTime; ts += timeGap) {
for (int i = 0; i < tableSize; i++) {
Rainfall rainfall = new Rainfall();
rainfall.setTs(new Timestamp(ts));
rainfall.setRainfall(random.nextFloat() * 999);
rainfall.setStation_code("s_" + i);
count += rainstationMapper.insertOne(rainfall);
}
}
return count;
} }
public long batchInsert(long startTime, long endTime, long timeGap, int batchSize, int tableSize) {
long count = 0;
// for (long ts = startTime; ts < endTime; ts += (timeGap * batchSize)) {
// for (int i = 0; i < tableSize; i++) {
// List<Rainfall> rainfallList = new ArrayList<>(batchSize);
// for (int j = 0; j < batchSize && (ts + j * timeGap) < endTime; j++) {
// Rainfall rainfall = new Rainfall();
// rainfall.setTs(new Timestamp(ts + (j * timeGap)));
// rainfall.setRainfall(random.nextFloat() * 999);
// rainfallList.add(rainfall);
// }
// count += rainstationMapper.insertMany("s_" + i, rainfallList);
// }
// }
/***********************************/
for (int i = 0; i < tableSize; i++) {
for (long ts = startTime; ts < endTime; ts += (timeGap * batchSize)) {
List<Rainfall> rainfallList = new ArrayList<>(batchSize);
for (int j = 0; j < batchSize && (ts + j * timeGap) < endTime; j++) {
Rainfall rainfall = new Rainfall();
rainfall.setTs(new Timestamp(ts + (j * timeGap)));
rainfall.setRainfall(random.nextFloat() * 999);
rainfallList.add(rainfall);
}
count += rainstationMapper.insertMany("s_" + i, rainfallList);
}
}
public int insert(Rainfall rainfall) { return count;
}
public List<Rainfall> latestHour() {
Instant end = Instant.now();
Instant start = end.minus(Duration.ofHours(1));
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("dbname", "rainstation"); map.put("startTime", start.toEpochMilli());
map.put("table", "S_53646"); map.put("endTime", end.toEpochMilli());
map.put("stable", "monitoring"); return rainstationMapper.findAll(map);
map.put("values", rainfall);
return rainfallMapper.save(map);
} }
public List<Rainfall> latestDay() {
Instant end = Instant.now();
Instant start = end.minus(Duration.ofDays(1));
Map<String, Object> map = new HashMap<>();
map.put("startTime", start.toEpochMilli());
map.put("endTime", end.toEpochMilli());
return rainstationMapper.findAll(map);
}
public List<Rainfall> latestWeek() {
Instant end = Instant.now();
Instant start = end.minus(Duration.ofDays(7));
Map<String, Object> map = new HashMap<>();
map.put("startTime", start.toEpochMilli());
map.put("endTime", end.toEpochMilli());
return rainstationMapper.findAll(map);
}
public List<Rainfall> latestMonth() {
Instant end = Instant.now();
Instant start = end.minus(Duration.ofDays(30));
Map<String, Object> map = new HashMap<>();
map.put("startTime", start.toEpochMilli());
map.put("endTime", end.toEpochMilli());
return rainstationMapper.findAll(map);
}
public List<Rainfall> latestYear() {
Instant end = Instant.now();
Instant start = end.minus(Duration.ofDays(365));
Map<String, Object> map = new HashMap<>();
map.put("startTime", start.toEpochMilli());
map.put("endTime", end.toEpochMilli());
return rainstationMapper.findAll(map);
}
} }
\ No newline at end of file
# datasource config # datasource config
spring.datasource.driver-class-name=com.taosdata.jdbc.TSDBDriver spring.datasource.driver-class-name=com.taosdata.jdbc.TSDBDriver
spring.datasource.url=jdbc:TAOS://localhost:6030/log spring.datasource.url=jdbc:TAOS://192.168.1.59:6030/?&charset=UTF-8
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=taosdata spring.datasource.password=taosdata
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册