Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic-session.ts
More file actions
Latest commit
123 lines (107 loc) · 3.8 KB
/
Copy pathbasic-session.ts
File metadata and controls
123 lines (107 loc) · 3.8 KB
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
/**
* Basic Session Example
*
* This example demonstrates how to use a single Session to connect to IoTDB,
* execute queries, and insert data. Shows both traditional and Builder pattern approaches.
*/
import{Session,ConfigBuilder,TSDataType,TreeTablet}from"../src";
asyncfunctionmain(){
console.log("=== Basic Session Example ===\n");
// Method 1: Traditional constructor (backward compatible)
console.log("Method 1: Traditional constructor");
constsession1=newSession({
host: "localhost",
port: 6667,
username: "root",
password: "root",
});
// Method 2: Using Builder pattern (recommended)
console.log("Method 2: Using Builder pattern");
constsession2=newSession(
newConfigBuilder()
.host("localhost")
.port(6667)
.username("root")
.password("root")
.fetchSize(1024)
.build(),
);
// For demo purposes, we'll use session1
constsession=session1;
try{
// Open the session
console.log("\nOpening session...");
awaitsession.open();
console.log("Session opened successfully");
// Create a database
console.log("\nCreating database...");
awaitsession.executeNonQueryStatement("CREATE DATABASE root.example");
console.log("Database created");
// Create timeseries
console.log("\nCreating timeseries...");
awaitsession.executeNonQueryStatement(
"CREATE TIMESERIES root.example.device1.temperature WITH DATATYPE=FLOAT, ENCODING=RLE",
);
awaitsession.executeNonQueryStatement(
"CREATE TIMESERIES root.example.device1.humidity WITH DATATYPE=FLOAT, ENCODING=RLE",
);
console.log("Timeseries created");
// Insert tablet data using TreeTablet class with addRow method
console.log("\nInserting tree tablet data using addRow...");
consttablet=newTreeTablet(
"root.example.device1",
["temperature","humidity"],
[TSDataType.FLOAT,TSDataType.FLOAT]
);
// Add rows one at a time - convenient for streaming data
tablet.addRow(Date.now(),[25.5,60.0]);
tablet.addRow(Date.now()+1000,[26.0,61.5]);
tablet.addRow(Date.now()+2000,[26.5,62.0]);
awaitsession.insertTablet(tablet);
console.log("Tree tablet data inserted");
// Alternative: Use plain object interface (still supported for backward compatibility)
console.log("\nInserting using plain object interface...");
awaitsession.insertTablet({
deviceId: "root.example.device1",
measurements: ["temperature","humidity"],
dataTypes: [TSDataType.FLOAT,TSDataType.FLOAT],
timestamps: [Date.now()+3000,Date.now()+4000],
values: [
[27.0,63.0],
[27.5,64.0],
],
});
console.log("Plain object data inserted");
// Query data using SessionDataSet iterator pattern
console.log("\nQuerying data...");
constdataSet=awaitsession.executeQueryStatement(
"SELECT * FROM root.example.device1",
);
console.log("Columns:",dataSet.getColumnNames());
letrowCount=0;
while(awaitdataSet.hasNext()){
constrow=dataSet.next();
rowCount++;
console.log(`Row ${rowCount}:`,row.getTimestamp(),row.getFields());
}
awaitdataSet.close();
console.log("Total rows:",rowCount);
// Show databases
console.log("\nShowing databases...");
constdbDataSet=awaitsession.executeQueryStatement("SHOW DATABASES");
constdatabases=[];
while(awaitdbDataSet.hasNext()){
databases.push(dbDataSet.next().getFields());
}
awaitdbDataSet.close();
console.log("Databases:",databases);
}catch(error){
console.error("Error:",error);
}finally{
// Close the session
console.log("\nClosing session...");
awaitsession.close();
console.log("Session closed");
}
}
main().catch(console.error);