本节为您介绍如何使用Connection
对象的方法下载算法结果文件,从图集导出点和边。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
DownloadAlgoResultFile()
下载当前图集一个算法任务中的一个结果文件。
参数:
int
:生成结果文件的算法任务ID。string
:文件名称。AlgoAPI.OnData
:接收请求结果的函数RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig()
{
UseMaster = true,
Graph = "miniCircle",
};
// 在图集miniCircle上运行鲁汶算法并打印任务ID
var res = await ultipa.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
);
var task = res.Alias("_task").AsTable();
var taskId = int.Parse((string)task.Rows[0][0]);
var filename = "communityID";
Thread.Sleep(2000);
var stream = new MemoryStream();
Console.WriteLine("TaskId :" + taskId);
Console.WriteLine("Content of the file '" + filename + "'");
await ultipa.DownloadAlgoResultFile(
taskId,
filename,
(reply, filename) =>
{
stream.Write(reply.Chunk.ToByteArray());
}
);
var bs = stream.ToArray();
Console.Write(Encoding.UTF8.GetString(bs));
TaskId :77218
Content of the file 'communityID'
DownloadAllAlgoResultFile()
下载当前图集一个算法任务中的全部结果文件。
参数:
int
:生成结果文件的算法任务ID。AlgoAPI.OnData
:接收请求结果的函数RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 在图集miniCircle上运行鲁汶算法并打印任务ID
RequestConfig requestConfig = new RequestConfig() { UseMaster = true, Graph = "miniCircle" };
{
var res = await ultipa.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
);
var task = res.Alias("_task").AsTable();
var taskId = int.Parse((string)task.Rows[0][0]);
Thread.Sleep(2000);
Console.WriteLine("TaskId :" + taskId);
var streams = new Dictionary<string, MemoryStream>();
await ultipa.DownloadAlgoAllResultFile(
taskId,
(reply, filename) =>
{
if (!streams.ContainsKey(filename))
{
streams[filename] = new MemoryStream();
}
streams[filename].Write(reply.Chunk.ToByteArray());
}
);
foreach (var kv in streams)
{
var bs = kv.Value.ToArray();
Console.WriteLine("Content of the file'" + kv.Key + "'");
Console.WriteLine("Download complete");
}
}
TaskId :79667
Content of the file 'communityID'
Download complete
Content of the file 'ids'
Download complete
Content of the file 'num'
Download complete
Export()
从当前图集导出点和边。
参数:
ExportRequest
:导出请求的配置信息,包括DbType:ULTIPA.DBType
,Schema:string
,Limit:number
和selectProperties:List<string>
。ExportAPI.OnData
:接收请求结果的函数RequestConfig
(可选):配置请求。
返回值:
ExportReply
:请求的结果。
// 从图集miniCircle名为account的schema中导出10个点,并打印其选定属性的信息。
RequestConfig requestConfig = new RequestConfig()
{
UseMaster = true,
Graph = "miniCircle",
};
ExportRequest exportConfig = new ExportRequest()
{
Schema = "account",
Limit = 10,
DbType = DBType.Dbnode,
SelectProperties = { "name", "year" },
};
await ultipa.Export(
exportConfig,
(nodes, edges) =>
{
foreach (var node in nodes)
{
Console.WriteLine(JsonConvert.SerializeObject(node));
}
},
requestConfig
);
{"Uuid":1,"Id":"ULTIPA8000000000000001","Schema":"account","Values":{"name":"Yu78","year":1978}}
{"Uuid":2,"Id":"ULTIPA8000000000000002","Schema":"account","Values":{"name":"jibber-jabber","year":1989}}
{"Uuid":3,"Id":"ULTIPA8000000000000003","Schema":"account","Values":{"name":"mochaeach","year":1982}}
{"Uuid":4,"Id":"ULTIPA8000000000000004","Schema":"account","Values":{"name":"Win-win0","year":2007}}
{"Uuid":5,"Id":"ULTIPA8000000000000005","Schema":"account","Values":{"name":"kevinh","year":1973}}
{"Uuid":6,"Id":"ULTIPA8000000000000006","Schema":"account","Values":{"name":"alexyhel","year":1974}}
{"Uuid":7,"Id":"ULTIPA8000000000000007","Schema":"account","Values":{"name":"hooj","year":1986}}
{"Uuid":8,"Id":"ULTIPA8000000000000008","Schema":"account","Values":{"name":"vv67","year":1990}}
{"Uuid":9,"Id":"ULTIPA8000000000000009","Schema":"account","Values":{"name":"95smith","year":1988}}
{"Uuid":10,"Id":"ULTIPA800000000000000A","Schema":"account","Values":{"name":"jo","year":1992}}
完整示例
using System.Data;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using UltipaService;
using UltipaSharp;
using UltipaSharp.api;
using UltipaSharp.configuration;
using UltipaSharp.connection;
using UltipaSharp.exceptions;
using UltipaSharp.structs;
using UltipaSharp.utils;
using Logger = UltipaSharp.utils.Logger;
using Property = UltipaSharp.structs.Property;
using Schema = UltipaSharp.structs.Schema;
class Program
{
static async Task Main(string[] args)
{
// 设置连接
//URI 示例: Hosts=new[]{"mqj4zouys.us-east-1.cloud.ultipa.com:60010"}
var myconfig = new UltipaConfig()
{
Hosts = new[] { "192.168.1.85:60061", "192.168.1.86:60061", "192.168.1.87:60061" },
Username = "***",
Password = "***",
};
// 建立与数据库的连接
var ultipa = new Ultipa(myconfig);
var isSuccess = ultipa.Test();
Console.WriteLine(isSuccess);
// 配置请求
RequestConfig requestConfig = new RequestConfig()
{
UseMaster = true,
Graph = "miniCircle",
};
// 导出图集 'miniCircle' 中名为 'account'的schema的10个点及其选定属性,并打印结果。
ExportRequest exportConfig = new ExportRequest();
exportConfig.Schema = "account";
exportConfig.Limit = 10;
exportConfig.DbType = DBType.Dbnode;
exportConfig.SelectProperties = ["_id", "_uuid", "name", "year"];
await ultipa.Export(
exportConfig,
(nodes, edges) =>
{
foreach (var node in nodes)
{
Console.WriteLine(JsonConvert.SerializeObject(node));
}
},
requestConfig
);
}
}