1. 程式人生 > 其它 >IoTDB原生session操作demo

IoTDB原生session操作demo

技術標籤:DB物聯網session操作IoTDB

JDBC方式操作IoTDB

原生session操作比jdbc方式更高效,這種方式專門為IoTDB設計,更加貼近IoTDB底層

所需jar包下載

maven方式

<dependencies>
    <dependency>
      <groupId>org.apache.iotdb</groupId>
      <artifactId>iotdb-session</artifactId>
      <version>0.10.1</version>
    </dependency>
</dependencies>

插入

    public static void main(String[] args) throws Exception{
        Session session = new Session("127.0.0.1",6667,"root","root");
        session.open();
        session.setStorageGroup("root.sestest");
        session.createTimeseries("root.sestest.wf01.wt01.s0", TSDataType.INT32, TSEncoding.RLE, CompressionType.SNAPPY);
        session.createTimeseries("root.sestest.wf01.wt01.s1", TSDataType.INT32, TSEncoding.RLE, CompressionType.SNAPPY);
        session.createTimeseries("root.sestest.wf01.wt01.s2", TSDataType.INT32, TSEncoding.RLE, CompressionType.SNAPPY);

        String deviceId = "root.sestest.wf01.wt01";
        List<String> measurements = new ArrayList<>(16);
        measurements.add("s0");
        measurements.add("s1");
        measurements.add("s2");


        for(long i = 1000 ;i <5000 ;i++){
            List<Object> values = new ArrayList<>(3);
            List<TSDataType> types = new ArrayList<>(3);
            values.add(1);
            types.add(TSDataType.INT32);
            values.add(2);
            types.add(TSDataType.INT32);
            values.add(3);
            types.add(TSDataType.INT32);

            session.insertRecord(deviceId,i,measurements,types,values);
        }
        session.close();

    }

查詢

    public static void query()throws Exception{
        Session session = new Session("127.0.0.1",6667,"root","root");
        session.open();
        SessionDataSet dataSet = session.executeQueryStatement("select * from root.sestest.wf01.wt01");
        List<String> list = dataSet.getColumnNames();
        for (String s : list) {
            System.out.printf("%-35s",s );
        }
        System.out.println();

        dataSet.setFetchSize(1024);
        while (dataSet.hasNext()){
            RowRecord record = dataSet.next();
            System.out.printf("%-35s",record.getTimestamp());
            for (Field field : record.getFields()) {
                System.out.printf("%-35s",field.getIntV());
            }

            System.out.println();
        }
        dataSet.closeOperationHandle();
        session.close();
    }