本节为您介绍如何使用Connection
对象的方法管理实例上的进程和当前图集上的任务。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
进程
所有UQL查询(使用write()
方法执行的算法除外)都作为进程运行。进程完成后,其结果会立刻返回给客户端,但不会被存储。
top()
获取实例中所有正在运行和正在停止的进程。
参数:
RequestConfig
(可选):配置请求。
返回值:
List<Process>
:实例中的全部进程列表。
// 获取实例中所有正在运行和正在终止的进程
List<Process> processList = client.top();
for (Process process : processList) {
System.out.println(process.getProcessId() + " " + process.getProcessUql());
}
a_0_539_2 n({_uuid > 300}).e()[:3].n() as p RETURN p{*} LIMIT 500
kill()
终止实例中正在进行的进程。
参数:
String
:待终止的进程ID;设定为*
时终止全部进程。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 获取实例中所有正在运行和正在终止的进程并终止所有进程
List<Process> processList = client.top();
for (Process process : processList) {
System.out.println(process.getProcessId() + " " + process.getProcessUql());
}
Response response = client.kill("*");
System.out.println(response.getStatus().getErrorCode());
a_0_540_2 n({_uuid > 300}).e()[:3].n() as p RETURN p{*} LIMIT 500
SUCCESS
任务
使用write()
方法执行的算法作为任务运行。这些任务存储在算法运行的图集里,直至任务被删除。
showTask()
获取当前图集的任务。
参数:
ShowTask
(可选):配置如何获取任务,包括id:String
,name:String
和status:TaskStatus
(若同时设置了id
和name
,将优先使用id
获取任务);如果忽略,将获取全部任务。RequestConfig
(可选):配置请求。
返回值:
List<Task>
:图集中的全部任务列表。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("miniCircle");
// 在图集miniCircle上运行一个算法任务
Response response = client.uql("algo(degree).params({order: 'desc'}).write({file:{filename: 'degree_all'}})", requestConfig);
String taskID = (String) response.alias("_task").asTable().getRows().get(0).get(0);
Thread.sleep(3000);
// 获取以上任务
ShowTask showTask = new ShowTask();
showTask.setId(taskID);
//showTask.setName("degree");
showTask.setStatus(TaskStatus.DONE);
List<Task> taskList = client.showTask(showTask, requestConfig);
for (Task task : taskList) {
System.out.println("Task ID: " + task.getTaskInfo().getTaskId());
System.out.println("Server ID: " + task.getTaskInfo().getServerId());
System.out.println("Algo Name: " + task.getTaskInfo().getAlgoName());
System.out.println("Task Params: " + task.getParam().toString());
System.out.println("Task Result: " + task.getResult().toString());
}
Task ID: 54240
Server ID: 3
Algo Name: degree
Task Params: {order=desc}
Task Result: {total_degree=590.000000, avarage_degree=1.940789, result_files=degree_all}
clearTask()
从当前图集清除(删除)任务。状态为COMPUTING
和WRITING
的任务无法被清除。
参数:
ClearTask
(可选):配置清除任务,包括id:Integer
,name:String
和status:TaskStatus
(若同时设置了id
和name
,将优先使用id
清除任务);如果忽略,将清除全部任务。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("miniCircle");
// 在图集miniCircle上运行一个算法任务
Response response = client.uql("algo(degree).params({order: 'desc'}).write({file:{filename: 'degree_all'}})", requestConfig);
String id = (String) response.alias("_task").asTable().getRows().get(0).get(0);
Integer taskID = Integer.parseInt(id);
Thread.sleep(3000);
// 清除以上任务
ClearTask clearTask = new ClearTask();
clearTask.setId(taskID);
//clearTask.setName("degree");
clearTask.setStatus(TaskStatus.DONE);
Response response1 = client.clearTask(clearTask, requestConfig);
System.out.println("Task " + taskID + " cleared: " + response1.getStatus().getErrorCode());
Task 54242 cleared: SUCCESS
stopTask()
终止当前图集里状态为COMPUTING
的任务。
参数:
String
:待终止的任务ID;设定为*
时终止全部任务。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("Ad_Click");
// 在图集Ad_Click上运行一个算法任务
Response response = client.uql("algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID'}})", requestConfig);
String taskId = (String) response.get(0).asTable().getRows().get(0).get(0);
// 终止以上任务并打印错误代码
Response response1 = client.stopTask(taskId, requestConfig);
System.out.println("Task " + taskId + " stopped: " + response1.getStatus().getErrorCode());
// 获取以上终止的任务
ShowTask showTask = new ShowTask();
showTask.setId(taskId);
showTask.setStatus(TaskStatus.STOP);
Thread.sleep(3000);
List<Task> taskList = client.showTask(showTask, requestConfig);
for (Task task : taskList) {
System.out.println("Task ID: " + task.getTaskInfo().getTaskId());
System.out.println("Server ID: " + task.getTaskInfo().getServerId());
System.out.println("Algo Name: " + task.getTaskInfo().getAlgoName());
System.out.println("Task Params: " + task.getParam().toString());
System.out.println("Task Result: " + task.getResult().toString());
}
Task 54248 stopped: SUCCESS
Task ID: 54248
Server ID: 3
Algo Name: louvain
Task Params: {phase1_loop_num=20, min_modularity_increase=0.001}
Task Result: {community_count=1228589, modularity=0.647601}
完整示例
package com.ultipa.www.sdk.api;
import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.RequestConfig;
import com.ultipa.sdk.connect.conf.UltipaConfiguration;
import com.ultipa.sdk.connect.driver.UltipaClientDriver;
import com.ultipa.sdk.connect.request.ShowTask;
import com.ultipa.sdk.operate.entity.*;
import com.ultipa.sdk.operate.enums.TaskStatus;
import com.ultipa.sdk.operate.response.Response;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置连接
UltipaConfiguration myConfig = UltipaConfiguration.config()
// URI示例: .hosts("mqj4zouys.us-east-1.cloud.ultipa.com:60010")
.hosts("192.168.1.85:60611,192.168.1.87:60611,192.168.1.88:60611")
.username("<username>")
.password("<password>");
UltipaClientDriver driver = null;
try {
// 建立与数据库的连接
driver = new UltipaClientDriver(myConfig);
Connection client = driver.getConnection();
Thread.sleep(3000);
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("miniCircle");
// 在图集miniCircle上运行一个算法任务
Response response = client.uql("algo(degree).params({order: 'desc'}).write({file:{filename: 'degree_all'}})", requestConfig);
String taskID = (String) response.alias("_task").asTable().getRows().get(0).get(0);
Thread.sleep(3000);
// 获取以上任务
ShowTask showTask = new ShowTask();
showTask.setId(taskID);
//showTask.setName("degree");
showTask.setStatus(TaskStatus.DONE);
List<Task> taskList = client.showTask(showTask, requestConfig);
for (Task task : taskList) {
System.out.println("Task ID: " + task.getTaskInfo().getTaskId());
System.out.println("Server ID: " + task.getTaskInfo().getServerId());
System.out.println("Algo Name: " + task.getTaskInfo().getAlgoName());
System.out.println("Task Params: " + task.getParam().toString());
System.out.println("Task Result: " + task.getResult().toString());
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (driver != null) {
driver.close();
}
}
}
}