本节为您介绍如何使用Connection
对象的方法下载算法结果文件,从图集导出点和边。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
downloadAlgoResultFile()
下载当前图集一个算法任务中的一个结果文件。
参数:
string
:文件名称。string
:生成结果文件的算法任务ID。DownloadFileResultListener
:下载进程的监听器。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
let requestConfig = <RequestType.RequestConfig>{
useMaster: true,
graphSetName: "miniCircle",
};
// 在图集miniCircle上运行鲁汶算法并打印任务ID
let resp = await conn.uql(
"algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})",
requestConfig
);
let myTask = await conn.showTask(
"louvain",
RequestType.TASK_STATUS.TASK_DONE,
requestConfig
);
let myTaskID = myTask.data.map((item) => item.task_info.task_id)[0];
console.log("taskID = ", myTaskID);
let myDownload = await conn.downloadAlgoResultFile(
"communityID",
myTaskID,
function (chunkData) {
fsPromises
.writeFile(`E:/NodeJs/Algo/communityID`, chunkData)
.then(() => {
console.log("Download write success");
})
.catch((err) => {
console.error("Download write error", err);
});
},
{
graphSetName: "miniCircle",
stream: {
onData(data) {
console.log("Success");
},
onEnd() {},
onError(error) {
console.error("Download write error");
},
},
}
);
taskID = 60085
Success
Download write success
downloadAllAlgoResultFile()
下载当前图集一个算法任务中的全部结果文件。
参数:
string
:生成结果文件的算法任务ID。DownloadFileResultListener
:下载进程的监听器。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
let requestConfig = <RequestType.RequestConfig>{
useMaster: true,
graphSetName: "miniCircle",
};
// 在图集miniCircle上运行鲁汶算法并打印任务ID
let resp = await conn.uql(
"algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})",
requestConfig
);
let myTask = await conn.showTask(
"louvain",
RequestType.TASK_STATUS.TASK_DONE,
requestConfig
);
let myTaskID = myTask.data.map((item) => item.task_info.task_id)[0];
console.log("taskID = ", myTaskID);
let myDownload = await conn.downloadAllAlgoResultFile(
myTaskID,
function (chunkData) {
fsPromises
.writeFile(`E:/NodeJs/Algo/LovainResult`, chunkData)
.then(() => {
console.log("Download write success");
})
.catch((err) => {
console.error("Download write error", err);
});
},
{
graphSetName: "miniCircle",
stream: {
onData(data) {},
onEnd() {},
onError(error) {
console.error("Download write error");
},
},
}
);
taskID = 60088
Download write success
Download write success
Download write success
export()
从当前图集导出点和边。
参数:
RequestType.ExportRequest
:导出请求的配置信息,包括dbType:ULTIPA.DBType
,schemaName:string
,limit:number
和selectPropertiesName:string[]
。ExportListener
:导出进程的监听器。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 从图集miniCircle名为account的schema导出10个点,并打印点的信息
let requestConfig = <RequestType.RequestConfig>{
useMaster: true,
graphSetName: "miniCircle",
};
let resp = await conn.export(
{
dbType: ULTIPA.DBType.DBNODE,
schemaName: "account",
limit: 10,
selectPropertiesName: ["_id", "_uuid", "name", "year"],
},
async (n, e) => {
console.log(n);
},
{
graphSetName: "miniCircle",
stream: {
onData(data) {},
onEnd() {},
onError(error) {
console.error("Download write error");
},
},
}
);
[
Node {
id: 'ULTIPA8000000000000001',
uuid: '1',
schema: 'account',
values: { name: 'Yu78', year: 1978 }
},
Node {
id: 'ULTIPA8000000000000002',
uuid: '2',
schema: 'account',
values: { name: 'jibber-jabber', year: 1989 }
},
Node {
id: 'ULTIPA8000000000000003',
uuid: '3',
schema: 'account',
values: { name: 'mochaeach', year: 1982 }
},
Node {
id: 'ULTIPA8000000000000004',
uuid: '4',
schema: 'account',
values: { name: 'Win-win0', year: 2007 }
},
Node {
id: 'ULTIPA8000000000000005',
uuid: '5',
schema: 'account',
values: { name: 'kevinh', year: 1973 }
},
Node {
id: 'ULTIPA8000000000000006',
uuid: '6',
schema: 'account',
values: { name: 'alexyhel', year: 1974 }
},
Node {
id: 'ULTIPA8000000000000007',
uuid: '7',
schema: 'account',
values: { name: 'hooj', year: 1986 }
},
Node {
id: 'ULTIPA8000000000000008',
uuid: '8',
schema: 'account',
values: { name: 'vv67', year: 1990 }
},
Node {
id: 'ULTIPA8000000000000009',
uuid: '9',
schema: 'account',
values: { name: '95smith', year: 1988 }
},
Node {
id: 'ULTIPA800000000000000A',
uuid: '10',
schema: 'account',
values: { name: 'jo', year: 1992 }
}
]
完整示例
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>{
graphSetName: "miniCircle",
useMaster: true,
};
// 运行鲁汶算法并打印任务ID
let resp = await conn.uql(
"algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})",
requestConfig
);
let myTask = await conn.showTask(
"louvain",
RequestType.TASK_STATUS.TASK_DONE,
requestConfig
);
let myTaskID = myTask.data.map((item) => item.task_info.task_id)[0];
console.log("taskID = ", myTaskID);
// 下载以上算法任务生成的全部文件,打印每个文件的内容和下载响应信息
let myDownload = await conn.downloadAllAlgoResultFile(
myTaskID,
function (chunkData) {
fsPromises
.writeFile(`C:/NodeJs/tw/LovainResult`, chunkData)
.then(() => {
console.log("Download write success");
})
.catch((err) => {
console.error("Download write error", err);
});
},
{
graphSetName: "miniCircle",
stream: {
onData(data) {},
onEnd() {},
onError(error) {
console.error("Download write error");
},
},
}
);
};
sdkUsage().then(console.log).catch(console.log);