本节介绍用于导出点、边的方法。
export()
导出图中的点或边。
参数
exportRequest: ExportRequest
:导出请求的配置,包括字段dbType
、schema
、selectProperties
、graph
和limit
;设置limit
为-1
时导出全部。listener: RequestType.ExportListener
:导出数据时执行的回调函数。
返回值
- Void
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import type { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types/index.js";
import pkg from '@ultipa-graph/ultipa-driver/src/proto/ultipa_pb.js';
const { ExportRequest, DBType } = pkg;
import * as fs from "fs";
import { parse } from "json2csv";
let sdkUsage = async () => {
const ultipaConfig: ULTIPA.UltipaConfig = {
// URI example: hosts: ["xxxx.us-east-1.cloud.ultipa.com:60010"]
hosts: ["10.xx.xx.xx:60010"],
username: "<username>",
password: "<password>"
};
const driver = new UltipaDriver(ultipaConfig);
// Exports 'account' nodes in the graph 'miniCircle'
const exportRequest = new ExportRequest();
exportRequest.setDbType(DBType.DBNODE);
exportRequest.setSchema("account");
exportRequest.setGraph("miniCircle");
exportRequest.addSelectProperties("_id");
exportRequest.addSelectProperties("name");
exportRequest.addSelectProperties("year");
exportRequest.setLimit(-1);
const allNodes: any[] = [];
await driver.export(exportRequest, {
stream: {
onStart: () => {
console.log("Export started");
},
onData: async (data) => {
allNodes.push(...data);
},
onEnd: () => {
console.log("Export completed. Total nodes:", allNodes.length);
if (allNodes.length === 0) {
console.log("No data received.");
return;
}
try {
const csv = parse(allNodes);
const filePath = "./account_nodes.csv";
fs.writeFileSync(filePath, csv, "utf8");
console.log(`CSV export completed: ${filePath}`);
} catch (err) {
console.error("Failed to write a row to CSV:", err);
}
},
},
});
};
sdkUsage().catch(console.error);
Export started
Export finished. Total nodes: 111
CSV export completed: ./account_nodes.csv
导出文件account_nodes.csv
将保存到与执行文件相同的目录中。