Browse Source

时序数据库iotdb

Tom-shushu 2 năm trước cách đây
mục cha
commit
0b572a4ed2

+ 31 - 0
iotdb-demo/.gitignore

@@ -0,0 +1,31 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**
+!**/src/test/**
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+
+### VS Code ###
+.vscode/

+ 6 - 0
iotdb-demo/README.md

@@ -0,0 +1,6 @@
+# 工程简介
+
+
+
+# 延伸阅读
+

+ 104 - 0
iotdb-demo/pom.xml

@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.zhouhong</groupId>
+    <artifactId>iotdb-demo</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>iotdb-demo</name>
+    <description>iotdb测试</description>
+
+    <properties>
+        <java.version>1.8</java.version>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
+    </properties>
+
+    <dependencies>
+        <!-- https://mvnrepository.com/artifact/org.apache.iotdb/iotdb-session -->
+        <dependency>
+            <groupId>org.apache.iotdb</groupId>
+            <artifactId>iotdb-session</artifactId>
+            <version>0.14.0-preview1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.6.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.83</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.junit.vintage</groupId>
+                    <artifactId>junit-vintage-engine</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+    </dependencies>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring-boot.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.8.1</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>2.3.7.RELEASE</version>
+                <configuration>
+                    <mainClass>com.zhouhong.iotdbdemo.IotdbDemoApplication</mainClass>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>repackage</id>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 13 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/IotdbDemoApplication.java

@@ -0,0 +1,13 @@
+package com.zhouhong.iotdbdemo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class IotdbDemoApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(IotdbDemoApplication.class, args);
+    }
+
+}

+ 186 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/config/IotDBSessionConfig.java

@@ -0,0 +1,186 @@
+package com.zhouhong.iotdbdemo.config;
+
+import lombok.extern.log4j.Log4j2;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.session.util.Version;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.write.record.Tablet;
+import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+
+import java.rmi.ServerException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * description: iotdb 配置工具类(常用部分,如需要可以自行扩展)
+ * 注意:可以不需要创建分组,插入时默认前两个节点名称为分组名称 比如: root.a1eaKSRpRty.CA3013A303A25467 或者
+ * root.a1eaKSRpRty.CA3013A303A25467.heart  他们的分组都为 root.a1eaKSRpRty
+ * author: zhouhong
+ */
+@Log4j2
+@Component
+@Configuration
+public class IotDBSessionConfig {
+
+    private static Session session;
+    private static final String LOCAL_HOST = "XXX.XX.XXX.XX";
+    @Bean
+    public Session getSession() throws IoTDBConnectionException, StatementExecutionException {
+        if (session == null) {
+            log.info("正在连接iotdb.......");
+            session = new Session.Builder().host(LOCAL_HOST).port(6667).username("root").password("root").version(Version.V_0_13).build();
+            session.open(false);
+            session.setFetchSize(100);
+            log.info("iotdb连接成功~");
+            // 设置时区
+            session.setTimeZone("+08:00");
+        }
+        return session;
+    }
+
+    /**
+     * description: 带有数据类型的添加操作 - insertRecord没有指定类型
+     * author: zhouhong
+     * @param  * @param deviceId:节点路径如:root.a1eaKSRpRty.CA3013A303A25467
+     *                  time:时间戳
+     *                  measurementsList:物理量 即:属性
+     *                  type:数据类型: BOOLEAN((byte)0), INT32((byte)1),INT64((byte)2),FLOAT((byte)3),DOUBLE((byte)4),TEXT((byte)5),VECTOR((byte)6);
+     *                  valuesList:属性值 --- 属性必须与属性值一一对应
+     * @return
+     */
+    public void insertRecordType(String deviceId, Long time,List<String>  measurementsList, TSDataType type,List<Object> valuesList) throws StatementExecutionException, IoTDBConnectionException, ServerException {
+        if (measurementsList.size() != valuesList.size()) {
+            throw new ServerException("measurementsList 与 valuesList 值不对应");
+        }
+        List<TSDataType> types = new ArrayList<>();
+        measurementsList.forEach(item -> {
+            types.add(type);
+        });
+        session.insertRecord(deviceId, time, measurementsList, types, valuesList);
+    }
+    /**
+     * description: 带有数据类型的添加操作 - insertRecord没有指定类型
+     * author: zhouhong
+     * @param  deviceId:节点路径如:root.a1eaKSRpRty.CA3013A303A25467
+     * @param  time:时间戳
+     * @param  measurementsList:物理量 即:属性
+     * @param  valuesList:属性值 --- 属性必须与属性值一一对应
+     * @return
+     */
+    public void insertRecord(String deviceId, Long time,List<String>  measurementsList, List<String> valuesList) throws StatementExecutionException, IoTDBConnectionException, ServerException {
+        if (measurementsList.size() == valuesList.size()) {
+            session.insertRecord(deviceId, time, measurementsList, valuesList);
+        } else {
+            log.error("measurementsList 与 valuesList 值不对应");
+        }
+    }
+    /**
+     * description: 批量插入
+     * author: zhouhong
+     */
+    public void insertRecords(List<String> deviceIdList, List<Long> timeList, List<List<String>> measurementsList, List<List<String>> valuesList) throws StatementExecutionException, IoTDBConnectionException, ServerException {
+        if (measurementsList.size() == valuesList.size()) {
+            session.insertRecords(deviceIdList, timeList, measurementsList, valuesList);
+        } else {
+            log.error("measurementsList 与 valuesList 值不对应");
+        }
+    }
+
+    /**
+     * description: 插入操作
+     * author: zhouhong
+     * @param  deviceId:节点路径如:root.a1eaKSRpRty.CA3013A303A25467
+     *  @param  time:时间戳
+     *  @param  schemaList: 属性值 + 数据类型 例子: List<MeasurementSchema> schemaList = new ArrayList<>();  schemaList.add(new MeasurementSchema("breath", TSDataType.INT64));
+     *  @param  maxRowNumber:
+     * @return
+     */
+    public void insertTablet(String deviceId,  Long time,List<MeasurementSchema> schemaList, List<Object> valueList,int maxRowNumber) throws StatementExecutionException, IoTDBConnectionException {
+
+        Tablet tablet = new Tablet(deviceId, schemaList, maxRowNumber);
+        // 向iotdb里面添加数据
+        int rowIndex = tablet.rowSize++;
+        tablet.addTimestamp(rowIndex, time);
+        for (int i = 0; i < valueList.size(); i++) {
+            tablet.addValue(schemaList.get(i).getMeasurementId(), rowIndex, valueList.get(i));
+        }
+        if (tablet.rowSize == tablet.getMaxRowNumber()) {
+            session.insertTablet(tablet, true);
+            tablet.reset();
+        }
+        if (tablet.rowSize != 0) {
+            session.insertTablet(tablet);
+            tablet.reset();
+        }
+    }
+
+    /**
+     * description: 根据SQL查询
+     * author: zhouhong
+     */
+    public SessionDataSet query(String sql) throws StatementExecutionException, IoTDBConnectionException {
+        return session.executeQueryStatement(sql);
+    }
+
+    /**
+     * description: 删除分组 如 root.a1eaKSRpRty
+     * author: zhouhong
+     * @param  groupName:分组名称
+     * @return
+     */
+    public void deleteStorageGroup(String groupName) throws StatementExecutionException, IoTDBConnectionException {
+        session.deleteStorageGroup(groupName);
+    }
+
+    /**
+     * description: 根据Timeseries删除  如:root.a1eaKSRpRty.CA3013A303A25467.breath  (个人理解:为具体的物理量)
+     * author: zhouhong
+     */
+    public void deleteTimeseries(String timeseries) throws StatementExecutionException, IoTDBConnectionException {
+        session.deleteTimeseries(timeseries);
+    }
+    /**
+     * description: 根据Timeseries批量删除
+     * author: zhouhong
+     */
+    public void deleteTimeserieList(List<String> timeseriesList) throws StatementExecutionException, IoTDBConnectionException {
+        session.deleteTimeseries(timeseriesList);
+    }
+
+    /**
+     * description: 根据分组批量删除
+     * author: zhouhong
+     */
+    public void deleteStorageGroupList(List<String> storageGroupList) throws StatementExecutionException, IoTDBConnectionException {
+        session.deleteStorageGroups(storageGroupList);
+    }
+
+    /**
+     * description: 根据路径和结束时间删除 结束时间之前的所有数据
+     * author: zhouhong
+     */
+    public void deleteDataByPathAndEndTime(String path, Long endTime) throws StatementExecutionException, IoTDBConnectionException {
+        session.deleteData(path, endTime);
+    }
+    /**
+     * description: 根据路径集合和结束时间批量删除 结束时间之前的所有数据
+     * author: zhouhong
+     */
+    public void deleteDataByPathListAndEndTime(List<String> pathList, Long endTime) throws StatementExecutionException, IoTDBConnectionException {
+        session.deleteData(pathList, endTime);
+    }
+    /**
+     * description: 根据路径集合和时间段批量删除
+     * author: zhouhong
+     */
+    public void deleteDataByPathListAndTime(List<String> pathList, Long startTime,Long endTime) throws StatementExecutionException, IoTDBConnectionException {
+        session.deleteData(pathList, startTime, endTime);
+    }
+
+}

+ 59 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/controller/IotDbController.java

@@ -0,0 +1,59 @@
+package com.zhouhong.iotdbdemo.controller;
+
+import com.zhouhong.iotdbdemo.config.IotDBSessionConfig;
+import com.zhouhong.iotdbdemo.model.param.IotDbParam;
+import com.zhouhong.iotdbdemo.response.ResponseData;
+import com.zhouhong.iotdbdemo.server.IotDbServer;
+import lombok.extern.log4j.Log4j2;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.rmi.ServerException;
+
+/**
+ * description: iotdb 控制层
+ * date: 2022/8/15 21:50
+ * author: zhouhong
+ */
+@Log4j2
+@RestController
+public class IotDbController {
+
+    @Resource
+    private IotDbServer iotDbServer;
+    @Resource
+    private IotDBSessionConfig iotDBSessionConfig;
+
+    /**
+     * 插入数据
+     * @param iotDbParam
+     */
+    @PostMapping("/api/device/insert")
+    public ResponseData insert(@RequestBody IotDbParam iotDbParam) throws StatementExecutionException, ServerException, IoTDBConnectionException {
+        iotDbServer.insertData(iotDbParam);
+        return ResponseData.success();
+    }
+
+    /**
+     * 插入数据
+     * @param iotDbParam
+     */
+    @PostMapping("/api/device/queryData")
+    public ResponseData queryDataFromIotDb(@RequestBody IotDbParam iotDbParam) throws Exception {
+        return ResponseData.success(iotDbServer.queryDataFromIotDb(iotDbParam));
+    }
+
+    /**
+     * 删除分组
+     * @return
+     */
+    @PostMapping("/api/device/deleteGroup")
+    public ResponseData deleteGroup() throws StatementExecutionException, IoTDBConnectionException {
+        iotDBSessionConfig.deleteStorageGroup("root.a1eaKSRpRty");
+        iotDBSessionConfig.deleteStorageGroup("root.smartretirement");
+        return ResponseData.success();
+    }
+
+}

+ 40 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/model/param/IotDbParam.java

@@ -0,0 +1,40 @@
+package com.zhouhong.iotdbdemo.model.param;
+
+import lombok.Data;
+/**
+ * description: 入参
+ * date: 2022/8/15 21:53
+ * author: zhouhong
+ */
+@Data
+public class IotDbParam {
+    /***
+     * 产品PK
+     */
+    private  String  pk;
+    /***
+     * 设备号
+     */
+    private  String  sn;
+    /***
+     * 时间
+     */
+    private Long time;
+    /***
+     * 实时呼吸
+     */
+    private String breath;
+    /***
+     * 实时心率
+     */
+    private String heart;
+    /***
+     * 查询开始时间
+     */
+    private String startTime;
+    /***
+     * 查询结束时间
+     */
+    private String endTime;
+
+}

+ 33 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/model/result/IotDbResult.java

@@ -0,0 +1,33 @@
+package com.zhouhong.iotdbdemo.model.result;
+
+import lombok.Data;
+
+/**
+ * description: 返回结果
+ * date: 2022/8/15 21:56
+ * author: zhouhong
+ */
+@Data
+public class IotDbResult {
+    /***
+     * 时间
+     */
+    private String time;
+    /***
+     * 产品PK
+     */
+    private  String  pk;
+    /***
+     * 设备号
+     */
+    private  String  sn;
+    /***
+     * 实时呼吸
+     */
+    private String breath;
+    /***
+     * 实时心率
+     */
+    private String heart;
+
+}

+ 77 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/response/ErrorResponseData.java

@@ -0,0 +1,77 @@
+package com.zhouhong.iotdbdemo.response;
+
+/**
+ * description: 错误返回封装
+ * date: 2022/8/15 21:30
+ * author: zhouhong
+ */
+public class ErrorResponseData extends ResponseData {
+    private String exceptionClazz;
+
+    ErrorResponseData(String message) {
+        super(false, DEFAULT_ERROR_CODE, message, message, (Object)null);
+    }
+
+    public ErrorResponseData(Integer code, String message) {
+        super(false, code, message, message, (Object)null);
+    }
+
+    ErrorResponseData(Integer code, String message, Object object) {
+        super(false, code, message, object);
+    }
+
+    ErrorResponseData(Integer code, String message, String localizedMsg, Object object) {
+        super(false, code, message, localizedMsg, object);
+    }
+
+    public boolean equals(final Object o) {
+        if (o == this) {
+            return true;
+        } else if (!(o instanceof ErrorResponseData)) {
+            return false;
+        } else {
+            ErrorResponseData other = (ErrorResponseData)o;
+            if (!other.canEqual(this)) {
+                return false;
+            } else if (!super.equals(o)) {
+                return false;
+            } else {
+                Object this$exceptionClazz = this.getExceptionClazz();
+                Object other$exceptionClazz = other.getExceptionClazz();
+                if (this$exceptionClazz == null) {
+                    if (other$exceptionClazz != null) {
+                        return false;
+                    }
+                } else if (!this$exceptionClazz.equals(other$exceptionClazz)) {
+                    return false;
+                }
+
+                return true;
+            }
+        }
+    }
+
+    protected boolean canEqual(final Object other) {
+        return other instanceof ErrorResponseData;
+    }
+
+    public int hashCode() {
+        int result = super.hashCode();
+        Object $exceptionClazz = this.getExceptionClazz();
+        result = result * 59 + ($exceptionClazz == null ? 43 : $exceptionClazz.hashCode());
+        return result;
+    }
+
+    public String getExceptionClazz() {
+        return this.exceptionClazz;
+    }
+
+    public void setExceptionClazz(final String exceptionClazz) {
+        this.exceptionClazz = exceptionClazz;
+    }
+
+    public String toString() {
+        return "ErrorResponseData(exceptionClazz=" + this.getExceptionClazz() + ")";
+    }
+}
+

+ 214 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/response/ResponseData.java

@@ -0,0 +1,214 @@
+package com.zhouhong.iotdbdemo.response;
+
+/**
+ * description: 返回结果封装
+ * date: 2022/8/15 21:32
+ * author: zhouhong
+ */
+public class ResponseData {
+    public static final String DEFAULT_SUCCESS_MESSAGE = "请求成功";
+    public static final String DEFAULT_ERROR_MESSAGE = "网络异常";
+    public static final Integer DEFAULT_SUCCESS_CODE = 200;
+    public static final Integer DEFAULT_ERROR_CODE = 500;
+    private Boolean success;
+    private Integer code;
+    private String message;
+    private String localizedMsg;
+    private Object data;
+
+    public ResponseData() {
+    }
+
+    public ResponseData(Boolean success, Integer code, String message, Object data) {
+        this.success = success;
+        this.code = code;
+        this.message = message;
+        this.data = data;
+    }
+
+    public ResponseData(Boolean success, Integer code, String message, String localizedMsg, Object data) {
+        this.success = success;
+        this.code = code;
+        this.message = message;
+        this.localizedMsg = localizedMsg;
+        this.data = data;
+    }
+
+    public ResponseData(Boolean success, Integer code, String message) {
+        this.success = success;
+        this.code = code;
+        this.message = message;
+    }
+
+    public static SuccessResponseData success() {
+        return new SuccessResponseData();
+    }
+
+    public static SuccessResponseData success(Object object) {
+        return new SuccessResponseData(object);
+    }
+
+    public static SuccessResponseData success(Integer code, String message, Object object) {
+        return new SuccessResponseData(code, message, object);
+    }
+
+    public static SuccessResponseData success(Integer code, String message) {
+        return new SuccessResponseData(code, message);
+    }
+
+    public static SuccessResponseData success(Integer code, String message, String localizedMsg, Object object) {
+        return new SuccessResponseData(code, message, localizedMsg, object);
+    }
+
+    public static ErrorResponseData error(String message) {
+        return new ErrorResponseData(message);
+    }
+
+    public static ErrorResponseData error(Integer code, String message) {
+        return new ErrorResponseData(code, message);
+    }
+
+    public static ErrorResponseData error(Integer code, String message, Object object) {
+        return new ErrorResponseData(code, message, object);
+    }
+
+    public static ErrorResponseData error(Integer code, String message, String localizedMsg, Object object) {
+        return new ErrorResponseData(code, message, localizedMsg, object);
+    }
+
+    public Boolean getSuccess() {
+        return this.success;
+    }
+
+    public Integer getCode() {
+        return this.code;
+    }
+
+    public String getMessage() {
+        return this.message;
+    }
+
+    public String getLocalizedMsg() {
+        return this.localizedMsg;
+    }
+
+    public Object getData() {
+        return this.data;
+    }
+
+    public void setSuccess(final Boolean success) {
+        this.success = success;
+    }
+
+    public void setCode(final Integer code) {
+        this.code = code;
+    }
+
+    public void setMessage(final String message) {
+        this.message = message;
+    }
+
+    public void setLocalizedMsg(final String localizedMsg) {
+        this.localizedMsg = localizedMsg;
+    }
+
+    public void setData(final Object data) {
+        this.data = data;
+    }
+
+    public boolean equals(final Object o) {
+        if (o == this) {
+            return true;
+        } else if (!(o instanceof ResponseData)) {
+            return false;
+        } else {
+            ResponseData other = (ResponseData)o;
+            if (!other.canEqual(this)) {
+                return false;
+            } else {
+                label71: {
+                    Object this$success = this.getSuccess();
+                    Object other$success = other.getSuccess();
+                    if (this$success == null) {
+                        if (other$success == null) {
+                            break label71;
+                        }
+                    } else if (this$success.equals(other$success)) {
+                        break label71;
+                    }
+
+                    return false;
+                }
+
+                Object this$code = this.getCode();
+                Object other$code = other.getCode();
+                if (this$code == null) {
+                    if (other$code != null) {
+                        return false;
+                    }
+                } else if (!this$code.equals(other$code)) {
+                    return false;
+                }
+
+                label57: {
+                    Object this$message = this.getMessage();
+                    Object other$message = other.getMessage();
+                    if (this$message == null) {
+                        if (other$message == null) {
+                            break label57;
+                        }
+                    } else if (this$message.equals(other$message)) {
+                        break label57;
+                    }
+
+                    return false;
+                }
+
+                Object this$localizedMsg = this.getLocalizedMsg();
+                Object other$localizedMsg = other.getLocalizedMsg();
+                if (this$localizedMsg == null) {
+                    if (other$localizedMsg != null) {
+                        return false;
+                    }
+                } else if (!this$localizedMsg.equals(other$localizedMsg)) {
+                    return false;
+                }
+
+                Object this$data = this.getData();
+                Object other$data = other.getData();
+                if (this$data == null) {
+                    if (other$data == null) {
+                        return true;
+                    }
+                } else if (this$data.equals(other$data)) {
+                    return true;
+                }
+
+                return false;
+            }
+        }
+    }
+
+    protected boolean canEqual(final Object other) {
+        return other instanceof ResponseData;
+    }
+
+    public int hashCode() {
+        int result1 = 1;
+        Object $success = this.getSuccess();
+        int result = result1 * 59 + ($success == null ? 43 : $success.hashCode());
+        Object $code = this.getCode();
+        result = result * 59 + ($code == null ? 43 : $code.hashCode());
+        Object $message = this.getMessage();
+        result = result * 59 + ($message == null ? 43 : $message.hashCode());
+        Object $localizedMsg = this.getLocalizedMsg();
+        result = result * 59 + ($localizedMsg == null ? 43 : $localizedMsg.hashCode());
+        Object $data = this.getData();
+        result = result * 59 + ($data == null ? 43 : $data.hashCode());
+        return result;
+    }
+
+    public String toString() {
+        return "ResponseData(success=" + this.getSuccess() + ", code=" + this.getCode() + ", message=" + this.getMessage() + ", localizedMsg=" + this.getLocalizedMsg() + ", data=" + this.getData() + ")";
+    }
+}

+ 28 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/response/SuccessResponseData.java

@@ -0,0 +1,28 @@
+package com.zhouhong.iotdbdemo.response;
+/**
+ * description: 正确返回结果封装
+ * date: 2022/8/15 21:40
+ * author: zhouhong
+ */
+
+public class SuccessResponseData extends ResponseData {
+    public SuccessResponseData() {
+        super(true, DEFAULT_SUCCESS_CODE, "请求成功", "请求成功", (Object)null);
+    }
+
+    public SuccessResponseData(Object object) {
+        super(true, DEFAULT_SUCCESS_CODE, "请求成功", "请求成功", object);
+    }
+
+    public SuccessResponseData(Integer code, String message, Object object) {
+        super(true, code, message, message, object);
+    }
+
+    public SuccessResponseData(Integer code, String message, String localizedMsg, Object object) {
+        super(true, code, message, localizedMsg, object);
+    }
+
+    public SuccessResponseData(Integer code, String message) {
+        super(true, code, message);
+    }
+}

+ 25 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/server/IotDbServer.java

@@ -0,0 +1,25 @@
+package com.zhouhong.iotdbdemo.server;
+
+import com.zhouhong.iotdbdemo.model.param.IotDbParam;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+
+import java.rmi.ServerException;
+
+/**
+ * description: iot服务类
+ * date: 2022/8/15 21:41
+ * author: zhouhong
+ */
+
+public interface IotDbServer {
+    /**
+     * 添加数据
+     */
+    void insertData(IotDbParam iotDbParam) throws StatementExecutionException, ServerException, IoTDBConnectionException;
+
+    /**
+     * 查询数据
+     */
+    Object queryDataFromIotDb(IotDbParam iotDbParam) throws Exception;
+}

+ 107 - 0
iotdb-demo/src/main/java/com/zhouhong/iotdbdemo/server/impl/IotDbServerImpl.java

@@ -0,0 +1,107 @@
+package com.zhouhong.iotdbdemo.server.impl;
+
+import com.zhouhong.iotdbdemo.config.IotDBSessionConfig;
+import com.zhouhong.iotdbdemo.model.param.IotDbParam;
+import com.zhouhong.iotdbdemo.model.result.IotDbResult;
+import com.zhouhong.iotdbdemo.server.IotDbServer;
+import lombok.extern.log4j.Log4j2;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.rmi.ServerException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * description: iot服务实现类
+ * date: 2022/8/15 9:43
+ * author: zhouhong
+ */
+
+@Log4j2
+@Service
+public class IotDbServerImpl implements IotDbServer {
+
+    @Resource
+    private IotDBSessionConfig iotDBSessionConfig;
+
+    @Override
+    public void insertData(IotDbParam iotDbParam) throws StatementExecutionException, ServerException, IoTDBConnectionException {
+        // iotDbParam: 模拟设备上报消息
+        // bizkey: 业务唯一key  PK :产品唯一编码   SN:设备唯一编码
+        String deviceId = "root.bizkey."+ iotDbParam.getPk() + "." + iotDbParam.getSn();
+        // 将设备上报的数据存入数据库(时序数据库)
+        List<String> measurementsList = new ArrayList<>();
+        measurementsList.add("heart");
+        measurementsList.add("breath");
+        List<String> valuesList = new ArrayList<>();
+        valuesList.add(String.valueOf(iotDbParam.getHeart()));
+        valuesList.add(String.valueOf(iotDbParam.getBreath()));
+        iotDBSessionConfig.insertRecord(deviceId, iotDbParam.getTime(), measurementsList, valuesList);
+    }
+
+    @Override
+    public List<IotDbResult> queryDataFromIotDb(IotDbParam iotDbParam) throws Exception {
+        List<IotDbResult> iotDbResultList = new ArrayList<>();
+
+        if (null != iotDbParam.getPk() && null != iotDbParam.getSn()) {
+            String sql = "select * from root.bizkey."+ iotDbParam.getPk() +"." + iotDbParam.getSn() + " where time >= "
+                    + iotDbParam.getStartTime() + " and time < " + iotDbParam.getEndTime();
+            SessionDataSet sessionDataSet = iotDBSessionConfig.query(sql);
+            List<String> columnNames = sessionDataSet.getColumnNames();
+            List<String> titleList = new ArrayList<>();
+            // 排除Time字段 -- 方便后面后面拼装数据
+            for (int i = 1; i < columnNames.size(); i++) {
+                String[] temp = columnNames.get(i).split("\\.");
+                titleList.add(temp[temp.length - 1]);
+            }
+            // 封装处理数据
+            packagingData(iotDbParam, iotDbResultList, sessionDataSet, titleList);
+        } else {
+            log.info("PK或者SN不能为空!!");
+        }
+        return iotDbResultList;
+    }
+    /**
+     * 封装处理数据
+     * @param iotDbParam
+     * @param iotDbResultList
+     * @param sessionDataSet
+     * @param titleList
+     * @throws StatementExecutionException
+     * @throws IoTDBConnectionException
+     */
+    private void packagingData(IotDbParam iotDbParam, List<IotDbResult> iotDbResultList, SessionDataSet sessionDataSet, List<String> titleList)
+            throws StatementExecutionException, IoTDBConnectionException {
+        int fetchSize = sessionDataSet.getFetchSize();
+        if (fetchSize > 0) {
+            while (sessionDataSet.hasNext()) {
+                IotDbResult iotDbResult = new IotDbResult();
+                RowRecord next = sessionDataSet.next();
+                List<Field> fields = next.getFields();
+                String timeString = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(next.getTimestamp());
+                iotDbResult.setTime(timeString);
+                Map<String, String> map = new HashMap<>();
+
+                for (int i = 0; i < fields.size(); i++) {
+                    Field field = fields.get(i);
+                    // 这里的需要按照类型获取
+                    map.put(titleList.get(i), field.getObjectValue(field.getDataType()).toString());
+                }
+                iotDbResult.setTime(timeString);
+                iotDbResult.setPk(iotDbParam.getPk());
+                iotDbResult.setSn(iotDbParam.getSn());
+                iotDbResult.setHeart(map.get("heart"));
+                iotDbResult.setBreath(map.get("breath"));
+                iotDbResultList.add(iotDbResult);
+            }
+        }
+    }
+}

+ 0 - 0
iotdb-demo/src/main/resources/application.properties


+ 12 - 0
iotdb-demo/src/main/resources/application.yml

@@ -0,0 +1,12 @@
+server:
+  port: 8080
+
+spring:
+  application:
+    name: iot-db
+  iotdb:
+    username: root
+    password: root
+    ip: 101.35.241.59
+    port: 6667
+    fetchSize: 10000

+ 13 - 0
iotdb-demo/src/test/java/com/zhouhong/iotdbdemo/IotdbDemoApplicationTests.java

@@ -0,0 +1,13 @@
+package com.zhouhong.iotdbdemo;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class IotdbDemoApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}