自定义Neo4j函数优化关系排序性能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/superman_xxx/article/details/81542927

一、IDEA新建Maven项目添加依赖

   <properties>
        <neo4j.version>3.4.1</neo4j.version>
    </properties>

    <dependencies>
        <dependency>
            <!-- This gives us the Procedure API our runtime code uses.
                 We have a `provided` scope on it, because when this is
                 deployed in a Neo4j Instance, the API will be provided
                 by Neo4j. If you add non-Neo4j dependencies to this
                 project, their scope should normally be `compile` -->
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>${neo4j.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Test Dependencies -->
        <dependency>
            <!-- This is used for a utility that lets us start Neo4j with
                 a specific Procedure, which is nice for writing tests. -->
            <groupId>org.neo4j.test</groupId>
            <artifactId>neo4j-harness</artifactId>
            <version>${neo4j.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <!-- Used to send cypher statements to our procedure. -->
            <groupId>org.neo4j.driver</groupId>
            <artifactId>neo4j-java-driver</artifactId>
            <version>1.4.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <!-- Neo4j Procedures require Java 8 -->
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- This generates a jar-file with our procedure code,
                     plus any dependencies marked as `compile` scope.
                     This should then be deployed in the `plugins` directory
                     of each Neo4j instance in your deployment.
                     After a restart, the procedure is available for calling. -->
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

二、需要排序的查询
需要排序的关系p,其中关系r上有属性eventTargetIds属性值是ids(由ID拼接成的string,使用逗号分隔)
eg.eventTargetIds:123123,123123,2131,12321,23424,123123,2331
zdr.apoc.getEventIdsSize是自定义的函数

RETURN zdr.apoc.getEventIdsSize("123123,123123,2131,12321,23424,123123,2331") as value; // 返回长度

match p=(n:Event)<-[r:HAS]-(m:VirId) where n.name='新闻_1432' and r.eventTargetIds IS NOT NULL return p ORDER BY zdr.apoc.getEventIdsSize(r.eventTargetIds) DESC limit 10 

上述排序也可以使用Neo4j自带的size()函数

match p=(n:Event)<-[r:HAS]-(m:VirId) where n.name='新闻_1432' and r.eventTargetIds IS NOT NULL return p ORDER BY size(r.eventTargetIds) DESC limit 10 

三、编写zdr.apoc.getEventIdsSize函数
自定义函数可以根据需求实现更多更实用的功能,除此之外还可以一定程度上优化查询速度,因此还是很强大的。

public class ZdrProcedures {
    /**
     * @param
     * @return
     * @Description: TODO(获取关系的属性ids的长度 , ids的值是用逗号分隔的id)
     */
        @UserFunction(name = "zdr.apoc.getEventIdsSize")// name是给函数设置别名
    public Number getEventIdsSize(@Name("ids") String ids) {
        String[] array = ids.split(",");
        int eventIdsSize = array.length;
        return eventIdsSize;
    }
}
// 测试函数
public class ZdrProceduresTest {
    @Rule
    public Neo4jRule neo4j = new Neo4jRule().withFunction(ZdrProcedures.class);
    @Test
    public void getEventIdsSize() throws Exception {
        GraphDatabaseService db = neo4j.getGraphDatabaseService();
        try (Transaction tx = db.beginTx()) {
            String eventIds = "123213,234324,4354353,1231231,2132131";
            Map<String, Object> params = new HashMap<>();
            params.put("eventIds", eventIds);
            Result result = db.execute("RETURN zdr.apoc.getEventIdsSize({eventIds}) as value", params);
            int eventIdsSize = (int) result.next().get("value");
            System.out.println(eventIdsSize);
        }
    }
}

四、测试完成之后打包安装到Neo4j就可以开始愉快的使用了!
Neo4j图数据库APOC存储过程基本安装使用参考链接

猜你喜欢

转载自blog.csdn.net/superman_xxx/article/details/81542927