-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic-session.ts
More file actions
124 lines (108 loc) · 3.83 KB
/
Copy pathbasic-session.ts
File metadata and controls
124 lines (108 loc) · 3.83 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
124
/**
* 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";
async function main() {
console.log("=== Basic Session Example ===\n");
// Method 1: Traditional constructor (backward compatible)
console.log("Method 1: Traditional constructor");
const session1 = new Session({
host: "localhost",
port: 6667,
username: "root",
password: "root",
});
// Method 2: Using Builder pattern (recommended)
console.log("Method 2: Using Builder pattern");
const session2 = new Session(
new ConfigBuilder()
.host("localhost")
.port(6667)
.username("root")
.password("root")
.fetchSize(1024)
.build(),
);
// For demo purposes, we'll use session1
const session = session1;
try {
// Open the session
console.log("\nOpening session...");
await session.open();
console.log("Session opened successfully");
// Create a database
console.log("\nCreating database...");
await session.executeNonQueryStatement("CREATE DATABASE root.example");
console.log("Database created");
// Create timeseries
console.log("\nCreating timeseries...");
await session.executeNonQueryStatement(
"CREATE TIMESERIES root.example.device1.temperature WITH DATATYPE=FLOAT, ENCODING=RLE",
);
await session.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...");
const tablet = new TreeTablet(
"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]);
await session.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...");
await session.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...");
const dataSet = await session.executeQueryStatement(
"SELECT * FROM root.example.device1",
);
console.log("Columns:", dataSet.getColumnNames());
let rowCount = 0;
while (await dataSet.hasNext()) {
const row = dataSet.next();
rowCount++;
console.log(`Row ${rowCount}:`, row.getTimestamp(), row.getFields());
}
await dataSet.close();
console.log("Total rows:", rowCount);
// Show databases
console.log("\nShowing databases...");
const dbDataSet = await session.executeQueryStatement("SHOW DATABASES");
const databases = [];
while (await dbDataSet.hasNext()) {
databases.push(dbDataSet.next().getFields());
}
await dbDataSet.close();
console.log("Databases:", databases);
} catch (error) {
console.error("Error:", error);
} finally {
// Close the session
console.log("\nClosing session...");
await session.close();
await session2.close();
console.log("Session closed");
}
}
main().catch(console.error);