通过JAVA使用聚合功能扩展Neo4j

时间:2018-07-11 04:21:47

标签: java neo4j cypher

这是我目前的密码查询

MATCH (sch:School)<-[:IN_SCHOOL]-(cla:Class1)<-[:IN_CLASS]-(stu:Student)
WHERE sch.schoolId=1
RETURN count(stu) as numberOfStudent
, AVG(stu.mathScore) AS avgScore
, MIN(stu.mathScore) AS minScore
, MAX(stu.mathScore) AS maxScore
, percentileDisc(stu.mathScore,0.1) AS per10thScore
, percentileDisc(stu.mathScore,0.1) AS medianScore
, percentileDisc(stu.mathScore,0.1) AS per90thScore

因此,如您所见,此密码将返回所有学生的摘要,其中将显示数学科目的平均分数以及最小,最大和百分位数

在某些情况下,查询变得非常复杂,我想完全控制它,因此我通过扩展Neo4j创建了过程,这是我用Java编写的函数

private static final RelationshipType IN_SCHOOL= RelationshipType.withName("IN_SCHOOL");
private static final RelationshipType IN_CLASS= RelationshipType.withName("IN_CLASS");
private static final Label School = Label.label("School");
private static final Label Class1 = Label.label("Class1");
private static final Label Student = Label.label("Student");

@Context
public GraphDatabaseService neo4j;

@UserFunction
@Description("Summary by School ID")
public List<Map<String, Object>> getSchoolSummary() {
    Transaction tx = neo4j.beginTx();
    List<Map<String, Object>> result = new ArrayList<>();
    try {
        Map<String, Object> conditions = new HashMap<String, Object>();
        conditions.put("schoolId", "1");
        ResourceIterator<Node> schools = neo4j.findNodes(School, conditions);

        while (schools.hasNext()) {
            Iterable<Relationship> inScholls = schools .next().getRelationships(IN_SCHOOL, Direction.INCOMING);
            while (inScholls.iterator().hasNext()) {
                Node class1 = inScholls.iterator().next().getEndNode();
                Iterable<Relationship> inClasses = class1.getRelationships(IN_CLASS, Direction.INCOMING);
                while (inClasses.iterator().hasNext()) {
                    Node student = inClasses.iterator().next().getEndNode();
                    //TODO AVG, MIN, MAX, PERCENTILE PLEASE HELP
                }
            }
        }

        tx.success();
    } finally {
        tx.close();
    }
}

所以我的问题是如何在扩展类中使用count,sum,avg和所有其他neo4j聚合函数

0 个答案:

没有答案