在开始之前,先打开官方开发手册和Java doc ,地址:
PAR API使用文档(示例 与使用说明) : www.galaxybase.com/document?file=dev&docid=74
Graphdbapi-par java doc (类 方法): galaxybase.com/public/v3.4.1/par-doc_20220721170804/com/graphdbapi/package-summary.html
package com.learn.algorithm;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import com.graphdbapi.GraphSchema;
import com.graphdbapi.Graph;
import com.graphdbapi.GraphLogger;
import com.graphdbapi.Edge;
import com.graphdbapi.Vertex;
import com.graphdbapi.procedure.Context;
import com.graphdbapi.procedure.Description;
import com.graphdbapi.procedure.Name;
import com.graphdbapi.procedure.Procedure;
import com.graphdbapi.values.util.ConvertUtil;
import com.graphdbapi.values.virtual.CypherNode;
/**
* 根据给定的节点外部唯一标识做N度检索
* 代码参数N 验证接收,未实现N度检索
* PK 查询不同类型未过滤
* 获取节点会存在重复放入
*
* 主要是为了验证主流程,代码逻辑可自行调整
*/
public class LimitQueryWithPK {
@Context
public Graph graph;
@Context
public GraphLogger log;
@Procedure("com.ddwang.algorithm.limitQuery")
@Description("根据给定的节点外部唯一标识做N度检索")
// 注意这里要看官方文档对类型的要求,比如不能使用int 必须用long
public Stream<ResultTree> limitQuery(@Name("pk")String pk,@Name("numberEdge") long n) {
GraphSchema graphSchema = graph.meta();
ResultTree resultTree = new ResultTree();
log.info("查询参数 pk:"+pk+" 几度:"+n);
Set<Long> ids = graph.getVertexIdByPk(pk);
ids.forEach(id->{
resultTree.setCypherNode(ConvertUtil.nodeValue(graphSchema,graph.retrieveVertex(id)));
List<Edge> edges = graph.retrieveEdgeByVertexId(id);
log.info("Node ID :"+id +" 查询边:"+edges.size());
List<CypherNode> cypherNodes = new ArrayList<>();
edges.forEach(edge -> {
Long toId = edge.getToId();
Vertex toVertex = graph.retrieveVertex(toId);
cypherNodes.add(ConvertUtil.nodeValue(graphSchema,toVertex));
});
});
return Stream.of(resultTree);
}
}
注意:点、边必须要做下转换才可以返回
package com.learn.algorithm;
import com.graphdbapi.values.virtual.CypherNode;
import com.graphdbapi.values.virtual.CypherRelationship;
import java.util.List;
public class ResultTree {
public List<CypherRelationship> relationships;
public List<CypherNode> nodes;
public List<CypherRelationship> getRelationships() {
return relationships;
}
public void setRelationships(List<CypherRelationship> relationships) {
this.relationships = relationships;
}
public List<CypherNode> getNodes() {
return nodes;
}
public void setNodes(List<CypherNode> nodes) {
this.nodes = nodes;
}
}
ResultTree和LimitQueryWithPK 可直接放在文件加下一块打包。必须是public可访问的,不能放在一个文件中。
# 过程调用方式
CALL com.learn.algorithm.limitQuery('3',1)
如何查看日志:
一个是通过可视化界面
第二个方法是通过控制台:
tail -f /data/galaxybase/bin/home/graph/logs/par/par.log
注意:
par包点击上线后,会长时间无法执行,提示procedure不存在,试着换下查询条件看看,不知道什么问题
可以点击编辑,在线修改代码