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 pathmulti-node.ts
More file actions
Latest commit
109 lines (96 loc) · 3.02 KB
/
Copy pathmulti-node.ts
File metadata and controls
109 lines (96 loc) · 3.02 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
/**
* Multi-Node Example
*
* This example demonstrates how to configure SessionPool to work with
* multiple IoTDB nodes for load balancing using the new nodeUrls API.
*/
import{SessionPool,PoolConfigBuilder}from'../src';
asyncfunctionmain(){
console.log('=== Multi-Node Example ===\n');
// Method 1: Using nodeUrls with string array format (RECOMMENDED)
console.log('Method 1: Using nodeUrls in string array format (host:port)');
constpool1=newSessionPool({
nodeUrls: [
'node1.example.com:6667',
'node2.example.com:6668',
'node3.example.com:6669',
],
username: 'root',
password: 'root',
maxPoolSize: 15,// 5 connections per node
minPoolSize: 3,// 1 connection per node initially
});
// Method 2: Using Builder pattern with string array format (RECOMMENDED)
console.log('Method 2: Using Builder pattern with string array');
constpool2=newSessionPool(
newPoolConfigBuilder()
.nodeUrls([
'node1.example.com:6667',
'node2.example.com:6668',
'node3.example.com:6669',
])
.username('root')
.password('root')
.maxPoolSize(15)
.minPoolSize(3)
.build()
);
// Method 3: Using nodeUrls with object format (also supported)
console.log('Method 3: Using nodeUrls in object format');
constpool3=newSessionPool({
nodeUrls: [
{host: 'node1.example.com',port: 6667},
{host: 'node2.example.com',port: 6668},
{host: 'node3.example.com',port: 6669},
],
username: 'root',
password: 'root',
maxPoolSize: 15,
minPoolSize: 3,
});
// Method 4: Backward compatible - same port for all hosts
console.log('Method 4: Backward compatible (same port for all hosts)');
constpool4=newSessionPool(
[
'node1.example.com',
'node2.example.com',
'node3.example.com',
],
6667,
{
username: 'root',
password: 'root',
maxPoolSize: 15,
minPoolSize: 3,
}
);
// For demo purposes, we'll use pool1
constpool=pool1;
try{
console.log('\nInitializing multi-node session pool...');
awaitpool.init();
console.log('Pool initialized with',pool.getPoolSize(),'connections across 3 nodes');
// Execute operations
// Connections will be distributed across all nodes using round-robin
console.log('\nExecuting operations across nodes...');
for(leti=0;i<10;i++){
constdataSet=awaitpool.executeQueryStatement('SHOW DATABASES');
letcount=0;
while(awaitdataSet.hasNext()){
dataSet.next();
count++;
}
awaitdataSet.close();
console.log(`Query ${i+1} executed (result count: ${count})`);
}
console.log('\nAll operations completed');
console.log('Final pool size:',pool.getPoolSize());
console.log('Available connections:',pool.getAvailableSize());
}catch(error){
console.error('Error:',error);
}finally{
awaitpool.close();
console.log('Pool closed');
}
}
main().catch(console.error);