Session And TsFile API

在使用Session、TsFIle API时,如果您调用的方法需要以字符串形式传入物理量(measurement)、设备(device)、数据库(database)、路径(path)等参数,请保证所传入字符串与使用 SQL 语句时的写法一致,下面是一些帮助您理解的例子。具体代码示例可以参考:example/session/src/main/java/org/apache/iotdb/SyntaxConventionRelatedExample.java

  1. 以创建时间序列 createTimeseries 为例:
  1. public void createTimeseries(
  2. String path,
  3. TSDataType dataType,
  4. TSEncoding encoding,
  5. CompressionType compressor)
  6. throws IoTDBConnectionException, StatementExecutionException;

如果您希望创建时间序列 root.sg.a,root.sgSession And TsFile API - 图1open in new window.`a.``“b`,root.sgSession And TsFile API - 图2open in new window.`111`,您使用的 SQL 语句应该如下所示:

  1. create timeseries root.sg.a with datatype=FLOAT,encoding=PLAIN,compressor=SNAPPY;
  2. # 路径结点名中包含特殊字符,时间序列各结点为["root","sg","a.`\"b"]
  3. create timeseries root.sg.`a."b` with datatype=FLOAT,encoding=PLAIN,compressor=SNAPPY;
  4. # 路径结点名为实数
  5. create timeseries root.sg.`111` with datatype=FLOAT,encoding=PLAIN,compressor=SNAPPY;

您在调用 createTimeseries 方法时,应该按照如下方法赋值 path 字符串,保证 path 字符串内容与使用 SQL 时一致:

  1. // 时间序列 root.sg.a
  2. String path = "root.sg.a";
  3. // 时间序列 root.sg.`a``"b`
  4. String path = "root.sg.`a``\"b`";
  5. // 时间序列 root.sg.`111`
  6. String path = "root.sg.`111`";
  1. 以插入数据 insertRecord 为例:
  1. public void insertRecord(
  2. String deviceId,
  3. long time,
  4. List<String> measurements,
  5. List<TSDataType> types,
  6. Object... values)
  7. throws IoTDBConnectionException, StatementExecutionException;

如果您希望向时间序列 root.sg.a,root.sgSession And TsFile API - 图3open in new window.`a.``“b`,root.sgSession And TsFile API - 图4open in new window.`111`中插入数据,您使用的 SQL 语句应该如下所示:

  1. insert into root.sg(timestamp, a, `a."b`, `111`) values (1, 2, 2, 2);

您在调用 insertRecord 方法时,应该按照如下方法赋值 deviceId 和 measurements:

  1. // deviceId 为 root.sg
  2. String deviceId = "root.sg";
  3. // measurements
  4. String[] measurements = new String[]{"a", "`a.\"b`", "`111`"};
  5. List<String> measurementList = Arrays.asList(measurements);
  1. 以查询数据 executeRawDataQuery 为例:
  1. public SessionDataSet executeRawDataQuery(
  2. List<String> paths,
  3. long startTime,
  4. long endTime)
  5. throws StatementExecutionException, IoTDBConnectionException;

如果您希望查询时间序列 root.sg.a,root.sgSession And TsFile API - 图5open in new window.`a.``“b`,root.sgSession And TsFile API - 图6open in new window.`111`的数据,您使用的 SQL 语句应该如下所示:

  1. select a from root.sg
  2. # 路径结点名中包含特殊字符
  3. select `a."b` from root.sg;
  4. # 路径结点名为实数
  5. select `111` from root.sg

您在调用 executeRawDataQuery 方法时,应该按照如下方法赋值 paths:

  1. // paths
  2. String[] paths = new String[]{"root.sg.a", "root.sg.`a.\"b`", "root.sg.`111`"};
  3. List<String> pathList = Arrays.asList(paths);