本节为您介绍如何使用Connection
对象的方法检查数据库服务器统计信息和驱动连接。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
stats()
获取数据库服务器的统计信息。
参数:
RequestConfig
(可选):配置请求。
返回值:
Stat
:获取到的服务器统计信息。
let resp = await conn.stats();
console.log("CPU usage:", resp.data.cpuUsage);
console.log("Memory usage:", resp.data.memUsage);
console.log("Expiration date:", resp.data.expiredDate);
console.log("CPU cores:", resp.data["cpuCores"]),
console.log("Company:", resp.data["company"]),
console.log("Server type:", resp.data["serverType"]),
console.log("Version:", resp.data["version"]);
CPU usage: 12.503961
Memory usage: 10356.265625
Expiration date: Thu Dec 26 23:59:59 2024
CPU cores: 80
Company: ultipa
Server type: CT
Version: htap_beta.4.5.5-b4.5.0-tv-ui
test()
测试驱动和数据库服务器连接。
参数:
RequestConfig
(可选):配置请求。
返回值:
boolean
:请求的结果。
let resp = await conn.test();
console.log(resp);
true
完整示例
import { ConnectionPool, ULTIPA } from "@ultipa-graph/ultipa-node-sdk";
import { GraphExra } from "@ultipa-graph/ultipa-node-sdk/dist/connection/extra/graph.extra";
import { getEdgesPrintInfo } from "@ultipa-graph/ultipa-node-sdk/dist/printers/edge";
import { RequestType } from "@ultipa-graph/ultipa-node-sdk/dist/types";
import { ListFormat } from "typescript";
let sdkUsage = async () => {
// 设置连接
//URI示例: hosts="mqj4zouys.us-east-1.cloud.ultipa.com:60010"
let hosts = [
"192.168.1.85:60061",
"192.168.1.86:60061",
"192.168.1.87:60061",
];
let username = "***";
let password = "***";
let connPool = new ConnectionPool(hosts, username, password);
// 建立与数据库的连接
let conn = await connPool.getActive();
let isSuccess = await conn.test();
console.log(isSuccess);
// 配置请求
let requestConfig = <RequestType.RequestConfig>{
useMaster: true,
};
// T测试连接
let resp = await conn.test();
console.log(resp);
};
sdkUsage().then(console.log).catch(console.log);