-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtable-session-pool.ts
More file actions
223 lines (196 loc) · 7.52 KB
/
Copy pathtable-session-pool.ts
File metadata and controls
223 lines (196 loc) · 7.52 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* TableSessionPool Example
*
* This example demonstrates how to use TableSessionPool for table model
* operations in IoTDB, including explicit session management, nodeUrls,
* and concurrent batch operations for high-throughput scenarios.
*/
import { TableSessionPool, PoolConfigBuilder, TSDataType, ColumnCategory, TableTablet } from "../src";
async function main() {
console.log("=== TableSessionPool Example ===\n");
// Method 1: Traditional constructor (backward compatible)
console.log("Method 1: Traditional constructor");
const pool1 = new TableSessionPool("localhost", 6667, {
username: "root",
password: "root",
database: "my_database", // Set default database
maxPoolSize: 10,
minPoolSize: 2,
});
// Method 2: Using Builder pattern (recommended)
console.log("Method 2: Using Builder pattern");
const pool2 = new TableSessionPool(
new PoolConfigBuilder()
.host("localhost")
.port(6667)
.username("root")
.password("root")
.database("my_database")
.maxPoolSize(10)
.minPoolSize(2)
.build(),
);
// Method 3: Using nodeUrls with string format (for multi-node)
console.log("Method 3: Using nodeUrls in string format");
const pool3 = new TableSessionPool({
nodeUrls: ["node1:6667", "node2:6668", "node3:6669"],
username: "root",
password: "root",
database: "my_database",
maxPoolSize: 10,
minPoolSize: 2,
});
// Method 4: Using Builder with nodeUrls
console.log("Method 4: Using Builder with nodeUrls");
const pool4 = new TableSessionPool(
new PoolConfigBuilder()
.nodeUrls(["node1:6667", "node2:6668", "node3:6669"])
.username("root")
.password("root")
.database("my_database")
.maxPoolSize(10)
.minPoolSize(2)
.build(),
);
// For demo purposes, we'll use pool1
const pool = pool1;
try {
// Initialize the pool
console.log("\nInitializing table session pool...");
await pool.init();
console.log(
"Table pool initialized with",
pool.getPoolSize(),
"connections",
);
// Create database if not exists
console.log("\nSetting up database...");
await pool.executeNonQueryStatement(
"CREATE DATABASE IF NOT EXISTS root.table_example",
);
// Approach 1: Using pool methods directly (automatic session management)
console.log("\n--- Approach 1: Automatic session management ---");
console.log("Executing table queries...");
const dataSet = await pool.executeQueryStatement("SHOW DATABASES");
let dbCount = 0;
while (await dataSet.hasNext()) {
dataSet.next();
dbCount++;
}
await dataSet.close();
console.log("Databases found:", dbCount);
// Insert data using TableTablet class with addRow method
console.log("Inserting table data using addRow...");
const tablet = new TableTablet(
"table1",
["device_id", "column1", "column2"],
[TSDataType.TEXT, TSDataType.INT32, TSDataType.FLOAT],
[
ColumnCategory.TAG, // device_id - indexed tag for filtering
ColumnCategory.FIELD, // column1 - measurement value
ColumnCategory.FIELD, // column2 - measurement value
]
);
// Add rows one at a time - convenient for streaming data
tablet.addRow(Date.now(), ["device_001", 100, 25.5]);
tablet.addRow(Date.now() + 1000, ["device_002", 200, 30.5]);
await pool.insertTablet(tablet);
console.log("Table data inserted");
// Approach 2: Explicit session management
console.log("\n--- Approach 2: Explicit session management ---");
console.log("Getting a session from the pool...");
const session = await pool.getSession();
try {
console.log("Executing operations with explicit session...");
// Query with explicit session
const queryDataSet = await session.executeQueryStatement("SHOW DATABASES");
let rowCount = 0;
while (await queryDataSet.hasNext()) {
queryDataSet.next();
rowCount++;
}
await queryDataSet.close();
console.log("Query result:", rowCount, "rows");
// Insert with explicit session using TableTablet class with addRow
const sessionTablet = new TableTablet(
"table1",
["device_id", "column1", "column2"],
[TSDataType.TEXT, TSDataType.INT32, TSDataType.FLOAT],
[
ColumnCategory.TAG, // device_id - indexed tag
ColumnCategory.FIELD, // column1 - measurement
ColumnCategory.FIELD, // column2 - measurement
]
);
sessionTablet.addRow(Date.now() + 2000, ["device_003", 300, 35.5]);
await session.insertTablet(sessionTablet);
console.log("Table data inserted via explicit session");
} finally {
// Always release the session back to the pool
pool.releaseSession(session);
console.log("Session released back to the pool");
}
// Approach 3: Concurrent batch insertion (NEW API - insertTabletsParallel)
console.log("\n--- Approach 3: Concurrent batch insertion (insertTabletsParallel) ---");
const tablets = [];
const batchTime = Date.now();
for (let i = 0; i < 20; i++) {
tablets.push({
tableName: "batch_table",
columnNames: ["device_id", "temperature"],
columnTypes: [TSDataType.STRING, TSDataType.FLOAT], // Use STRING for TAG columns
columnCategories: [ColumnCategory.TAG, ColumnCategory.FIELD],
timestamps: [batchTime + i * 1000],
values: [[`batch_device_${i}`, 20.0 + i]],
});
}
console.log(`Inserting ${tablets.length} table tablets concurrently...`);
const startTime = Date.now();
await pool.insertTabletsParallel(tablets, { concurrency: 5 });
const elapsed = Date.now() - startTime;
console.log(`Inserted ${tablets.length} tablets in ${elapsed}ms (${(tablets.length * 1000 / elapsed).toFixed(2)} tablets/sec)`);
// Approach 4: Generic parallel execution (NEW API - executeParallel)
console.log("\n--- Approach 4: Generic parallel execution (executeParallel) ---");
const tableNames = ["parallel_t1", "parallel_t2", "parallel_t3", "parallel_t4"];
console.log(`Creating ${tableNames.length} tables in parallel...`);
const results = await pool.executeParallel(
tableNames,
async (session, tableName) => {
try {
await session.executeNonQueryStatement(
`CREATE TABLE IF NOT EXISTS ${tableName}(device_id TEXT TAG, value FLOAT FIELD)`
);
} catch {
// Ignore if already exists
}
return `Created ${tableName}`;
},
{ concurrency: 4 }
);
console.log("Results:", results);
// Pool statistics
console.log("\nTable pool statistics:");
console.log("Total connections:", pool.getPoolSize());
console.log("Available connections:", pool.getAvailableSize());
console.log("In-use connections:", pool.getInUseSize());
// Enhanced metrics
console.log("\nEnhanced metrics:");
console.log("Total (new API):", pool.totalCount);
console.log("Idle (new API):", pool.idleCount);
console.log("Active (new API):", pool.activeCount);
console.log("Waiting requests:", pool.waitingCount);
// Get comprehensive stats
const stats = pool.getPoolStats();
console.log("\nComprehensive stats:", stats);
} catch (error) {
console.error("Error:", error);
} finally {
console.log("\nClosing table session pool...");
await pool.close();
await pool2.close();
await pool3.close();
await pool4.close();
console.log("Table pool closed");
}
}
main().catch(console.error);