本节为您介绍如何使用Connection
对象的方法管理实例上的进程和当前图集上的任务。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
Process
所有UQL查询(使用write()
方法执行的算法除外)都作为进程运行。进程完成后,其结果会立刻返回给客户端,但不会被存储。
Top()
获取实例中所有正在运行和正在停止的进程。
参数:
RequestConfig
(可选):配置请求。
返回值:
List<Process>
:实例中的全部进程列表。
// 获取实例中所有正在运行和正在终止的进程
RequestConfig requestConfig = new RequestConfig() { UseMaster = true };
var processList = await ultipa.Top(requestConfig);
Console.WriteLine(JsonConvert.SerializeObject(processList));
[{"Id":"a_7_33_2","Uql":"n().e().n().e().n().e().n() as p return count(p)","Duration":73,"Status":"RUNNING"}]
Kill()
终止实例中正在进行的进程。
参数:
string
:待终止的进程ID;设定为*
时终止全部进程。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 获取实例中所有正在运行和正在终止的进程并终止所有进程
RequestConfig requestConfig = new RequestConfig() { UseMaster = true };
var processList = await ultipa.Top(requestConfig);
foreach (var process in processList)
{
Console.WriteLine("Process ID: " + process.Id);
Console.WriteLine("UQL: " + process.Uql);
}
Thread.Sleep(1000);
var res = await ultipa.Kill("*", requestConfig);
if (res.Status.ErrorCode == 0)
{
Console.WriteLine("Operation succeeds");
}
Process ID: a_6_41_2
UQL: n().e().n().e().n().e().n() as p return count(p)
Operation succeeds
Task
使用write()
方法执行的算法作为任务运行。这些任务存储在算法运行的图集里,直至任务被删除。
ShowTask()
获取当前图集的任务。
参数:
string
orint
:要检索的算法名称(string
)或任务 ID(int
)。TaskStatus
(可选):任务状态。RequestConfig
(可选):配置请求。
返回值:
List<Task>
:图集中的全部任务列表。
RequestConfig requestConfig = new RequestConfig()
{
UseMaster = true,
Graph = "miniCircle",
};
// 在图集miniCircle上运行一个算法任务
var res = await ultipa.Uql(
"algo(degree).params({order: 'desc'}).write({file:{filename: 'degree_all'}})",
requestConfig
);
var taskID = Convert.ToInt32(res?.Alias("_task")?.AsTable()?.Rows[0][0]);
Thread.Sleep(5000);
// 获取以上任务并展示任务信息
var showT = await ultipa.ShowTask(taskID, requestConfig);
Console.WriteLine("Task ID: " + JsonConvert.SerializeObject(showT[0].Info.TaskId));
Console.WriteLine("Algo name: " + JsonConvert.SerializeObject(showT[0].Info.AlgoName));
Console.WriteLine("Task result: " + JsonConvert.SerializeObject(showT[0].result));
Task ID: "72122"
Algo name: "degree"
Task result: {"total_degree":"1390.000000","avarage_degree":"4.572368","result_files":"degree_all"}
ClearTask()
从当前图集清除(删除)任务。状态为TaskStatusComputing
和TaskStatusWriting
的任务无法被清除。
参数:
string
orint
:要检索的算法名称或任务ID。TaskStatus
:任务状态。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig()
{
UseMaster = true,
Graph = "miniCircle",
};
// 在图集miniCircle上运行一个算法任务
var res = await ultipa.Uql(
"algo(degree).params({order: 'desc'}).write({file:{filename: 'degree_all'}})",
requestConfig
);
var taskID = Convert.ToInt32(res?.Alias("_task")?.AsTable()?.Rows[0][0]);
Thread.Sleep(5000);
// 清除以上任务并打印错误码
var clearT = await ultipa.ClearTask(taskID, requestConfig);
Console.WriteLine("Task ID: " + taskID);
if (clearT.Status.ErrorCode == 0)
{
Console.WriteLine("Task cleared");
}
else
{
Console.WriteLine("Task not cleared");
}
Task ID: 72124
Task cleared
StopTask()
终止当前图集里状态为COMPUTING
的任务。
参数:
string
:待终止的任务ID;设定为*
时终止全部任务。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig()
{
UseMaster = true,
Graph = "miniCircle",
};
// 在图集miniCircle上运行一个算法任务
var res = await ultipa.Uql(
"algo(degree).params({order: 'desc'}).write({file:{filename: 'degree_all'}})",
requestConfig
);
var taskID = Convert.ToString(res?.Alias("_task")?.AsTable()?.Rows[0][0]);
// 终止以上任务并打印错误代码
var stopT = await ultipa.StopTask(taskID, requestConfig);
Console.WriteLine("Task ID: " + taskID);
Console.WriteLine("Task is stopped: " + stopT.Status.ErrorCode);
Task ID: 72126
Task is stopped: Success
完整示例
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上运行一个算法任务
var res = await ultipa.Uql(
"algo(degree).params({order: 'desc'}).write({file:{filename: 'degree_all'}})",
requestConfig
);
var taskID = Convert.ToInt32(res?.Alias("_task")?.AsTable()?.Rows[0][0]);
Thread.Sleep(5000);
// 获取以上任务并显示任务信息
var showT = await ultipa.ShowTask(taskID, requestConfig);
Console.WriteLine("Task ID: " + JsonConvert.SerializeObject(showT[0].Info.TaskId));
Console.WriteLine("Server ID: " + JsonConvert.SerializeObject(showT[0].Info.ServerId));
Console.WriteLine("Algo name: " + JsonConvert.SerializeObject(showT[0].Info.AlgoName));
Console.WriteLine("Task result: " + JsonConvert.SerializeObject(showT[0].result));
}
}