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

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

上级 635583e6
......@@ -63,7 +63,7 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.4</version>
<version>2.0.8</version>
</dependency>
<dependency>
......@@ -71,8 +71,6 @@
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
</dependencies>
<build>
......
......@@ -4,7 +4,7 @@
```properties
# datasource config
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.password=taosdata
......
package com.taosdata.jdbc.springbootdemo.controller;
import com.taosdata.jdbc.springbootdemo.domain.Rainfall;
import com.taosdata.jdbc.springbootdemo.service.RainStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/rainstation")
public class RainStationController {
@Autowired
private RainStationService service;
private int keep = 7665;
private int days = 120;
private int blocks = 30;
@GetMapping("/init")
public boolean init() {
service.init();
service.createTable();
public boolean init(@RequestParam("stationCount") int stationCount) {
// create database rainstation keep 7665 days 120 blocks 30
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;
}
@PostMapping("/insert")
public int insert(@RequestBody Rainfall rainfall){
return service.insert(rainfall);
@GetMapping("/generate")
public long generate(@RequestParam("stationCount") int stationCount, @RequestParam("timeGap") int timeGap, @RequestParam("batchSize") int batchSize) {
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 {
*/
@PostMapping("/batch")
public int batchSaveWeather(@RequestBody List<Weather> 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"?>
<!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">
<update id="createDatabase" parameterType="java.lang.String">
create database if not exists ${dbname}
</update>
<mapper namespace="com.taosdata.jdbc.springbootdemo.dao.RainstationMapper">
<update id="dropDatabase" parameterType="java.lang.String">
DROP database if exists ${dbname}
</update>
<update id="creatDatabaseWithParameters" parameterType="map">
<update id="creatDatabase" parameterType="map">
CREATE database if not EXISTS ${dbname}
<if test="keep != null">
KEEP ${keep}
......@@ -41,4 +36,36 @@
use ${dbname}
</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>
\ 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;
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 String name;
private String code;
private float rainfall;
private String station_code;
private String station_name;
......@@ -22,22 +20,6 @@ public class Rainfall {
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() {
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;
import com.taosdata.jdbc.springbootdemo.dao.DatabaseMapper;
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.dao.RainstationMapper;
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.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
@Service
public class RainStationService {
@Autowired
private DatabaseMapper databaseMapper;
@Autowired
private TableMapper tableMapper;
@Autowired
private RainfallMapper rainfallMapper;
private RainstationMapper rainstationMapper;
public boolean init() {
databaseMapper.dropDatabase("rainstation");
private Random random = new Random(System.currentTimeMillis());
private String[] stationNames = {"北京", "上海", "深圳", "广州", "杭州", "苏州", "成都", "西安", "天津", "厦门", "福州"};
public boolean createDb(int keep, int days, int blocks) {
rainstationMapper.dropDatabase("rainstation");
Map<String, String> map = new HashMap<>();
map.put("dbname", "rainstation");
map.put("keep", "36500");
map.put("days", "30");
map.put("blocks", "4");
databaseMapper.creatDatabaseWithParameters(map);
map.put("keep", "" + keep);
map.put("days", "" + days);
map.put("blocks", "" + blocks);
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;
}
public boolean createTable() {
TableMetadata tableMetadata = new TableMetadata();
tableMetadata.setDbname("rainstation");
tableMetadata.setTablename("monitoring");
// create table s_xxx using monitoring tags("s_xxxx","北京");
public void createSubTables(int stationCount) {
for (int i = 0; i < stationCount; i++) {
rainstationMapper.createSubTable("s_" + i, "s_" + i, stationNames[random.nextInt(stationNames.length)]);
}
}
List<FieldMetadata> fields = new ArrayList<>();
fields.add(new FieldMetadata("ts", "timestamp"));
fields.add(new FieldMetadata("name", "NCHAR(10)"));
fields.add(new FieldMetadata("code", " BINARY(8)"));
fields.add(new FieldMetadata("rainfall", "float"));
tableMetadata.setFields(fields);
public boolean dropDatabase() {
try {
rainstationMapper.dropDatabase("rainstation");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
List<TagMetadata> tags = new ArrayList<>();
tags.add(new TagMetadata("station_code", "BINARY(8)"));
tags.add(new TagMetadata("station_name", "NCHAR(10)"));
tableMetadata.setTags(tags);
public long generate(int keep, int stationCount, long timeGapSec, int batchSize) {
rainstationMapper.useDatabase("rainstation");
Instant end = Instant.now();
Instant start = end.minus(Duration.ofDays(keep - 1));
final long timeGap = 1000l * timeGapSec;
tableMapper.createSTable(tableMetadata);
return true;
if (stationCount < 1000) {
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.put("dbname", "rainstation");
map.put("table", "S_53646");
map.put("stable", "monitoring");
map.put("values", rainfall);
return rainfallMapper.save(map);
map.put("startTime", start.toEpochMilli());
map.put("endTime", end.toEpochMilli());
return rainstationMapper.findAll(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
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.password=taosdata
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册