From 0e391451b0f7f605be1b46842e8aa939b07567f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=A8=E5=93=80?= <2076029107@qq.com> Date: Fri, 4 Oct 2024 10:08:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:()=20=E6=96=B0=E5=A2=9Eiotdb=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6,iotdb=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../muyu/processing/config/IotDBConfig.java | 107 ++++++++++++++++++ .../controller/IotDbController.java | 59 ++++++++++ 2 files changed, 166 insertions(+) create mode 100644 cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/config/IotDBConfig.java create mode 100644 cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/controller/IotDbController.java diff --git a/cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/config/IotDBConfig.java b/cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/config/IotDBConfig.java new file mode 100644 index 0000000..b92d68d --- /dev/null +++ b/cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/config/IotDBConfig.java @@ -0,0 +1,107 @@ +package com.muyu.processing.config; + +import lombok.extern.log4j.Log4j2; +import org.apache.iotdb.isession.SessionDataSet; +import org.apache.iotdb.rpc.IoTDBConnectionException; +import org.apache.iotdb.rpc.StatementExecutionException; +import org.apache.iotdb.session.Session; +import org.apache.iotdb.tsfile.read.common.Field; +import org.apache.iotdb.tsfile.read.common.RowRecord; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * iotdb配置类 + * @Author:杨鹏 + * @Package:com.muyu.processing.config + * @Project:car-cloud-server + * @name:IotDBConfig + * @Date:2024/9/30 15:13 + */ +@Log4j2 +@Component +@Configuration +public class IotDBConfig { + + /** + * 创建session + */ + @Bean + public static Session session(){ + Session session = null; + try { + session = new Session( + "47.101.49.53", + 6667, + "root", + "root" + ); + session.open(false); + session.setFetchSize(100); + } catch (Exception e) { + throw new RuntimeException(e); + } + return session; + } + + /** + * 新增数据 + */ + public void execute(String deviceId, Long time, List measurement, List values){ + if (!CollectionUtils.isEmpty(measurement) && !CollectionUtils.isEmpty(values)){ + try { + session().insertAlignedRecord(deviceId,time,measurement,values); + } catch (IoTDBConnectionException e) { + throw new RuntimeException(e); + } catch (StatementExecutionException e) { + throw new RuntimeException(e); + } + } + } + + /** + * 查询数据 + */ + public List> executeQuery(String sql){ + log.info("sql:{}",sql); + ArrayList> list = new ArrayList<>(); + + try { + SessionDataSet sessionDataSet = session().executeQueryStatement(sql); + int fetchSize = sessionDataSet.getFetchSize(); + List columnNames = sessionDataSet.getColumnNames(); + List columnTypes = sessionDataSet.getColumnTypes(); + System.out.println(columnNames); + System.out.println(columnTypes); + if (fetchSize > 0){ + while (sessionDataSet.hasNext()){ + HashMap map = new HashMap<>(); + RowRecord next = sessionDataSet.next(); + List fields = next.getFields(); + // 查询结果第一个为时间戳 + long timestamp = next.getTimestamp(); + for (int i = 0; i < fields.size(); i++) { + Field field = fields.get(i); + String key = field.getStringValue(); + // 这里的需要按照类型获取 + Object value = field.getObjectValue(field.getDataType()); + map.put(key, value); + } + list.add(map); + } + } + sessionDataSet.closeOperationHandle(); + } catch (Exception e) { + throw new RuntimeException(e); + } + return list; + } + + +} diff --git a/cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/controller/IotDbController.java b/cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/controller/IotDbController.java new file mode 100644 index 0000000..a8ce170 --- /dev/null +++ b/cloud-modules/cloud-modules-processing/src/main/java/com/muyu/processing/controller/IotDbController.java @@ -0,0 +1,59 @@ +package com.muyu.processing.controller; + +import com.muyu.processing.config.IotDBConfig; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * iotdb测试 + * @Author:杨鹏 + * @Package:com.muyu.processing.controller + * @Project:car-cloud-server + * @name:IotdbController + * @Date:2024/10/2 9:39 + */ +@RestController +@RequestMapping("iotdb") +public class IotDbController { + + @Resource + private IotDBConfig iotDBConfig; + + private String json = "{\n" + " \"carVin\": \"VIN123456\",\n" + + " \"carName\": \"宝马\",\n" + "}"; + + /** + * 添加数据 + */ + @GetMapping("add") + public void add(){ +// Map map = JSON.parseObject(json, Map.class); +// Set set = map.keySet(); + ArrayList key = new ArrayList<>(); + ArrayList value = new ArrayList<>(); + key.add("car_vin"); + key.add("car_name"); + value.add("VIN123456"); + value.add("宝马"); + System.out.println(key); + System.out.println(value); + long l = System.currentTimeMillis(); + iotDBConfig.execute("root.vehicle", l, key, value); + } + + /** + * 查询列表 + */ + @GetMapping("findList") + public void findList(){ + String sql = "select * from root.vehicle"; + List> list = iotDBConfig.executeQuery(sql); + System.out.println(list); + } +}