首页 - Java - 写

天谋科技 ·
package org.apache.iotdb;

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 org.apache.iotdb.tsfile.write.record.Tablet;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.List;

public class SessionExample {

  public static void main(String[] args)
          throws IoTDBConnectionException, StatementExecutionException {
    Session session = new Session.Builder()
            .host("127.0.0.1")
            .port(6667)
            .username("root")
            .password("root")
            .build();
    session.open(false);

    List<MeasurementSchema> schemaList = new ArrayList<>();
    schemaList.add(new MeasurementSchema("s1", TSDataType.FLOAT));
    schemaList.add(new MeasurementSchema("s2", TSDataType.FLOAT));
    schemaList.add(new MeasurementSchema("s3", TSDataType.FLOAT));
    Tablet tablet = new Tablet("root.db.d1", schemaList, 10);

    tablet.addTimestamp(0, 1);
    tablet.addValue("s1", 0, 1.23f);
    tablet.addValue("s2", 0, 1.23f);
    tablet.addValue("s3", 0, 1.23f);
    tablet.rowSize++;
    session.insertTablet(tablet);
    tablet.reset();
    session.close();
  }
}