/
SessionExample.java
187 lines (166 loc) · 6.48 KB
/
SessionExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.iotdb;
import org.apache.iotdb.rpc.BatchExecutionException;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.session.SessionDataSet;
import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
import org.apache.iotdb.tsfile.write.record.RowBatch;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import org.apache.iotdb.tsfile.write.schema.Schema;
import java.util.ArrayList;
import java.util.List;
public class SessionExample {
private static Session session;
public static void main(String[] args)
throws IoTDBConnectionException, StatementExecutionException, BatchExecutionException {
session = new Session("127.0.0.1", 6667, "root", "root");
session.open();
session.setStorageGroup("root.sg1");
if (session.checkTimeseriesExists("root.sg1.d1.s1")) {
session.createTimeseries("root.sg1.d1.s1", TSDataType.INT64, TSEncoding.RLE, CompressionType.SNAPPY);
}
if (session.checkTimeseriesExists("root.sg1.d1.s2")) {
session.createTimeseries("root.sg1.d1.s2", TSDataType.INT64, TSEncoding.RLE, CompressionType.SNAPPY);
}
if (session.checkTimeseriesExists("root.sg1.d1.s3")) {
session.createTimeseries("root.sg1.d1.s3", TSDataType.INT64, TSEncoding.RLE, CompressionType.SNAPPY);
}
insert();
insertInBatch();
insertRowBatch();
nonQuery();
query();
deleteData();
deleteTimeseries();
session.close();
}
private static void insert() throws IoTDBConnectionException, StatementExecutionException {
String deviceId = "root.sg1.d1";
List<String> measurements = new ArrayList<>();
measurements.add("s1");
measurements.add("s2");
measurements.add("s3");
for (long time = 0; time < 100; time++) {
List<String> values = new ArrayList<>();
values.add("1");
values.add("2");
values.add("3");
session.insert(deviceId, time, measurements, values);
}
}
private static void insertInBatch() throws IoTDBConnectionException, BatchExecutionException {
String deviceId = "root.sg1.d1";
List<String> measurements = new ArrayList<>();
measurements.add("s1");
measurements.add("s2");
measurements.add("s3");
List<String> deviceIds = new ArrayList<>();
List<List<String>> measurementsList = new ArrayList<>();
List<List<String>> valuesList = new ArrayList<>();
List<Long> timestamps = new ArrayList<>();
for (long time = 0; time < 500; time++) {
List<String> values = new ArrayList<>();
values.add("1");
values.add("2");
values.add("3");
deviceIds.add(deviceId);
measurementsList.add(measurements);
valuesList.add(values);
timestamps.add(time);
if (time != 0 && time % 100 == 0) {
session.insertInBatch(deviceIds, timestamps, measurementsList, valuesList);
deviceIds.clear();
measurementsList.clear();
valuesList.clear();
timestamps.clear();
}
}
session.insertInBatch(deviceIds, timestamps, measurementsList, valuesList);
}
/**
* insert a batch data of one device, each batch contains multiple timestamps with values of sensors
*
* a RowBatch example:
*
* device1
* time s1, s2, s3
* 1, 1, 1, 1
* 2, 2, 2, 2
* 3, 3, 3, 3
*
* Users need to control the count of RowBatch and write a batch when it reaches the maxBatchSize
*
*/
private static void insertRowBatch() throws IoTDBConnectionException, BatchExecutionException {
// The schema of sensors of one device
Schema schema = new Schema();
schema.registerMeasurement(new MeasurementSchema("s1", TSDataType.INT64, TSEncoding.RLE));
schema.registerMeasurement(new MeasurementSchema("s2", TSDataType.INT64, TSEncoding.RLE));
schema.registerMeasurement(new MeasurementSchema("s3", TSDataType.INT64, TSEncoding.RLE));
RowBatch rowBatch = schema.createRowBatch("root.sg1.d1", 100);
long[] timestamps = rowBatch.timestamps;
Object[] values = rowBatch.values;
for (long time = 0; time < 100; time++) {
int row = rowBatch.batchSize++;
timestamps[row] = time;
for (int i = 0; i < 3; i++) {
long[] sensor = (long[]) values[i];
sensor[row] = i;
}
if (rowBatch.batchSize == rowBatch.getMaxBatchSize()) {
session.insertBatch(rowBatch);
rowBatch.reset();
}
}
if (rowBatch.batchSize != 0) {
session.insertBatch(rowBatch);
rowBatch.reset();
}
}
private static void deleteData() throws IoTDBConnectionException, StatementExecutionException {
String path = "root.sg1.d1.s1";
long deleteTime = 99;
session.deleteData(path, deleteTime);
}
private static void deleteTimeseries()
throws IoTDBConnectionException, StatementExecutionException {
List<String> paths = new ArrayList<>();
paths.add("root.sg1.d1.s1");
paths.add("root.sg1.d1.s2");
paths.add("root.sg1.d1.s3");
session.deleteTimeseries(paths);
}
private static void query() throws IoTDBConnectionException, StatementExecutionException {
SessionDataSet dataSet;
dataSet = session.executeQueryStatement("select * from root.sg1.d1");
dataSet.setBatchSize(1024); // default is 512
while (dataSet.hasNext()){
System.out.println(dataSet.next());
}
dataSet.closeOperationHandle();
}
private static void nonQuery() throws IoTDBConnectionException, StatementExecutionException {
session.executeNonQueryStatement("insert into root.sg1.d1(timestamp,s1) values(200, 1);");
}
}