package com.muyu.processing.utils;


import com.muyu.common.kafka.config.KafkaProducerConfig;
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;

/**
 * IotDb测试
 * @Author：杨鹏
 * @Package：com.muyu.processing.utils
 * @Project：car-cloud-server
 * @name：lotdb
 * @Date：2024/9/28 19:12
 */
public class IotDb {

    public static void main(String[] args) throws IoTDBConnectionException , StatementExecutionException {
        System.out.println("开始测试IoTDB数据库连接");

        // 初始化与连接
        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> values = new ArrayList<>();
        values.add(88);
        insertRecord(session, values);

        //查询数据
        queryRecord(session);

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

    }

    private static void insertRecord(Session session, List<Object> value) 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, value);
        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("————————————————查询数据结束————————————————");
    }

}
