After creating the spring boot project, add dependencies. Note that the dependency version is consistent with your IotDB version
<!--iotdb dependency--> <dependency> <groupId>org.apache.iotdb</groupId> <artifactId>iotdb-session</artifactId> <version>0.13.2</version> </dependency> <dependency> <groupId>org.apache.iotdb</groupId> <artifactId>iotdb-jdbc</artifactId> <version>0.13.2</version> </dependency> <!--mybatis dependency--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
Option 1: Integrate using mybatis
Add to configuration file
server: port: 8080 #iotdb spring: datasource: username: root password: root driver-class-name: org.apache.iotdb.jdbc.IoTDBDriver url: jdbc:iotdb://110.110.110.xxx:6667/ #mybatis mybatis: mapper-locations: classpath*:/mappers/*.xml
Create controller, mapper, service (impl) and other packages by yourself
controller
@RestController public class OrderController { @Autowired OrderService orderService; @GetMapping("/test") public void test(){ Integer test = orderService.test(); System.out.println(test); } }
mapper
@Mapper public interface OrderMapper { Integer test(); }
service
public interface OrderService { Integer test (); }
serviceImpl
@Service public class OrderServiceImpl implements OrderService { @Autowired OrderMapper orderMapper; @Override public Integer test() { return orderMapper.test(); } }
Mapping files under mappers. Remember to modify the statements.
<?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.huangke.iotdbtest.mapper.OrderMapper"> <select id="test" resultType="Integer"> select count(*) from root.ln.wf01.wt01 </select> </mapper>
The general structure is as follows
Next, run the access to get the result
Option 2: Use IotSession
Note: To use this method, first comment out the dependencies of mybatis, otherwise an error will be reported if the database is not injected.
The configuration file is as follows
#iotdb spring: iotdb: IP: 192.168.110.10 port: 6667 user: root password: root fetchSize: 10000 maxActive: 10 server: port: 8080
Create a configuration file under the config package
@Configuration public class IotConfigSession { @Value("${spring.iotdb.ip}") private String ip; @Value("${spring.iotdb.port}") private int port; @Value("${spring.iotdb.user}") private String user; @Value("${spring.iotdb.password}") private String password; @Value("${spring.iotdb.fetchSize}") private int fetchSize; @Bean public Session iotSession(){ Session session = new Session(ip, port, user, password, fetchSize); try { session.open(); } catch (IoTDBConnectionException e) { throw new RuntimeException(e); } return session; } }
Create controller
Friends can also write some simple test cases by themselves. Just use session to “.”
It does not need to be used in the following methods
Session session = iotConfigSession.iotSession();
You can do it directly with iotConfigSession. I do this just because I want to implement different session connections to test the pressure.
@RestController public class TestController { @Autowired private IotConfigSession iotConfigSession; //Batch insert The first parameter is the number of inserts each time The second parameter is the total amount inserted @GetMapping("insert/{BATCH_INSERT_SIZE}/{TOTAL_INSERT_ROW_COUNT}") public String insertTablet(@PathVariable int BATCH_INSERT_SIZE,@PathVariable int TOTAL_INSERT_ROW_COUNT) throws IoTDBConnectionException, StatementExecutionException { //Set the device name, the sensor name under the device, and the type of each sensor Session session = iotConfigSession.iotSession(); List<MeasurementSchema> schemaList = new ArrayList<>(); //Specify the physical quantity (field is also called attribute) sensor and set the type schemaList.add(new MeasurementSchema("status", TSDataType.BOOLEAN)); schemaList.add(new MeasurementSchema("temperature", TSDataType.DOUBLE)); schemaList.add(new MeasurementSchema("speed", TSDataType.INT64)); /* The default first two levels are storage group maxRowNumber, how many rows are inserted at a time*/ Tablet tablet = new Tablet("root.ln.wf01.wt01", schemaList, BATCH_INSERT_SIZE); // Use the current timestamp as the starting timestamp for insertion long timestamp = System.currentTimeMillis(); long beginTime = System.currentTimeMillis(); for (long row = 0; row < TOTAL_INSERT_ROW_COUNT; row + + ) { int rowIndex = tablet.rowSize + + ; tablet.addTimestamp(rowIndex, timestamp);//The timestamp of batch insertion is an array, rowIndex is the subscript // Randomly generate data and values corresponding to physical quantities tablet.addValue("status", rowIndex, (row & amp; 1) == 0); tablet.addValue("temperature", rowIndex, (double) row); tablet.addValue("speed", rowIndex, row); if (tablet.rowSize == tablet.getMaxRowNumber()) {//Execute once after splicing to BATCH_INSERT_SIZE long bg = System.currentTimeMillis(); session.insertTablet(tablet); tablet.reset(); System.out.println("Already inserted:" + (row + 1) + "Row data" + "Time consuming:" + (System.currentTimeMillis()-bg)); } timestamp + + ; } //Insert the remaining data that is insufficient for BATCH_INSERT_SIZE and insert it again. For example, if 20 rows are allowed to be inserted at a time and there are 25 records, insert it 2 times (20 + 5) if (tablet.rowSize != 0) { session.insertTablet(tablet); System.out.println("Last inserted" + tablet.rowSize); tablet.reset(); } long endTime =System.currentTimeMillis(); System.out.println("Insert" + TOTAL_INSERT_ROW_COUNT + "Records, each insert" + BATCH_INSERT_SIZE + "Total time taken" + (endTime-beginTime) + "ms"); // session.close(); return "Insert" + TOTAL_INSERT_ROW_COUNT + "Records, each insert" + BATCH_INSERT_SIZE + "Total time taken" + (endTime-beginTime) + "ms" + session; } //delete storage group @GetMapping("deleteStorageGroup") public String deleteStorageGroup() throws IoTDBConnectionException, StatementExecutionException { Session session = iotConfigSession.iotSession(); session.deleteStorageGroup("root.ln"); // session.close(); return "Delete storage group root.ln"; } //Batch insert num is the quantity @GetMapping("insertRecords/{num}") public String insertRecords(@PathVariable("num") int num) throws IoTDBConnectionException, StatementExecutionException { Session session = iotConfigSession.iotSession(); //device/entity String deviceId="root.ln"; //Attribute physical quantity List<String> schemaList=new ArrayList<>(); schemaList.add("capacity"); schemaList.add("price"); schemaList.add("status"); List<TSDataType> types=new ArrayList<>(); types.add(TSDataType.TEXT); types.add(TSDataType.DOUBLE); types.add(TSDataType.BOOLEAN); List<Object> value=new ArrayList<>(); value.add("1000"); value.add(88.5); value.add(false); List<String> deviceIds=new ArrayList<>(); List<Long> times=new ArrayList<>(); List<List<String>> measurementsList=new ArrayList<>(); List<List<TSDataType>> typesList=new ArrayList<>(); List<List<Object>> valueList=new ArrayList<>(); for (int i=1;i<=num;i + + ){ deviceIds.add(deviceId + ".F" + i + "." + "k" + (i )); times.add((long) i 0 + 1); measurementsList.add(schemaList); typesList.add(types); valueList.add(value); } long begin = System.currentTimeMillis(); //List<String> deviceIds, List<Long> times, List<List<String>> measurementsList, List<List<TSDataType>> typesList, List<List<Object>> values session.insertRecords(deviceIds,times,measurementsList,typesList,valueList); long end = System.currentTimeMillis(); session.close(); return "Insertion" + num + "Time taken to insert records:" + (end-begin) + "ms"; } //Inquire @GetMapping("select") public String select() throws IoTDBConnectionException, StatementExecutionException { Session session = iotConfigSession.iotSession(); long l = System.currentTimeMillis(); String sql="select * from root.ln.wf01.wt01 limit 100"; SessionDataSet sessionDataSet = session.executeQueryStatement(sql); while(sessionDataSet.hasNext()){ RowRecord rowRecord = sessionDataSet.next(); System.out.println("timeStamp:" + rowRecord.getTimestamp()); List<Field> fields = rowRecord.getFields(); for (Field field:fields){ System.out.println(field); } System.out.println("--------------------------------------------- ----------"); } long l1 = System.currentTimeMillis(); session.close(); return "Query time taken:" + (l1-l) + session; } }
test
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16,655 people are learning the system