package com.muyu.event.util;

import org.apache.iotdb.isession.SessionDataSet;
import org.apache.iotdb.isession.util.Version;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;

import java.util.ArrayList;
import java.util.List;

public class Iotdb {
    public static void main(String[] args) throws IoTDBConnectionException, StatementExecutionException {

        System.out.println("测试数据库开始~~~~~");

        // 初始化与连接
        Session session = new Session.Builder()
                .host("47.116.173.119")
                .port(6667)
                .username("root")
                .password("root")
                .version(Version.V_1_0)
                .build();

        // 开启session Rpc不压缩
        session.open(false);

        // 写入数据
        ArrayList<Object> list = new ArrayList<>();
        list.add(100);
        insertRecord(session,list);

        //查询数据
        queryRecord(session);

        //关闭连接
        session.close();

    }


    private static void insertRecord(Session session, List<Object> values) throws IoTDBConnectionException, StatementExecutionException {
        ArrayList<String> measurements = new ArrayList<>();
        ArrayList<TSDataType> types = new ArrayList<>();

        measurements.add("status");
        types.add(TSDataType.INT32);
        session.insertRecord("root.four.test",System.currentTimeMillis(),measurements,types,values);
        System.out.println("————————————————写入数据成功————————————————");
    }

    private static void queryRecord(Session session) throws IoTDBConnectionException, StatementExecutionException {
        System.out.println("————————————————查询数据开始————————————————");
        try(SessionDataSet dataSet= session.executeQueryStatement("select * from root.four.test")){
            System.out.println(dataSet.getColumnNames());
            dataSet.setFetchSize(1024);
            while (dataSet.hasNext()){
                System.out.println(dataSet.next());
            }

        }
        System.out.println("————————————————查询数据结束————————————————");
    }
}
