Neo4j's APOC installation and usage example

1 Introduction to APOC

APOC stands for Awesome Procedures on Cypher, which is the largest and most widely used extension library for Neo4j and a standard utility library for Neo4j procedures and functions. It includes more than 450 standard programs, providing utilities, transformations, graphics updates, and more. They are well supported and easy to run as standalone functions or included in Cypher queries.

2 Precautions

Since APOC depends on Neo4j's internal API, you need to use a matching version of APOC for your Neo4j installation. Make sure the first two version numbers match between Neo4j and APOC. The following is the APOC and Neo4j version matching table

apoc version neo4j version
4.4.0.1 4.4.0 (4.3.x)
4.3.0.4 4.3.7 (4.3.x)
4.2.0.9 4.2.11 (4.2.x)
4.1.0.10 4.1.11 (4.1.x)
4.0.0.18 4.0.12 (4.0.x)
3.5.0.15 3.5.30 (3.5.x)
3.4.0.8 3.4.18 (3.4.x)
3.3.0.4 3.3.9 (3.3.x)
3.2.3.6 3.2.14 (3.2.x)
3.1.3.9 3.1.9 (3.1.x)
3.0.8.6 3.0.12 (3.0.x)
3.5.0.0 3.5.0-beta01
3.4.0.2 3.4.5
3.3.0.3 3.3.5
3.2.3.5 3.2.3
3.1.3.8 3.1.5

3 installation process

  1. Download the binary jar and put it in the local folder $NEO4J_HOME$/plugins

    image-20230516113738678

    image-20230516113758133

  2. Add the following to the $NEO4J_HOME$/conf/neo4j.conf file:

    1. For security reasons, Neo4j disables use of internal APIs by default. They can be enabled by specifying configuration in $NEO4J_HOME/conf/neo4j.conf)

      dbms.security.procedures.unrestricted=apoc.*
      
    2. Whitelist the procedures and functions that are generally loaded in apoc (in the list of programs that are allowed to run)

      dbms.security.procedures.whitelist=apoc.coll.*,apoc.load.*
      

      image-20230516113945423

  3. Restart Neo4j to confirm whether the installation is successful. If the APOC version number is returned, the installation is successful

    RETURN apoc.version() as version
    

    image-20230516114102459

4 Use apoc sample

Union function in cypher apoc

In the APOC library, apoc.coll.unionfunctions can be used to perform the union operation of sets. apoc.coll.unionfunction to merge two or more collections into a new collection containing all elements.

Here is an example of using apoc.coll.uniona function to perform a set union operation:

cypherCopy codeMATCH (p1:Person {name: 'John'})
MATCH (p2:Person {name: 'Alice'})
RETURN apoc.coll.union(p1.hobbies, p2.hobbies) AS mergedHobbies

In the above example, we are using the MATCH clause to match the "Person" nodes named "John" and "Alice" respectively. Then, in the RETURN clause, we use apoc.coll.uniona function to merge the "hobbies" collection properties of these two nodes into a new collection and name the result "mergedHobbies".

The query results will return a list containing the merged hobbies, which is the union of the "hobbies" collections of the two "Person" nodes.

Intersection function in cypher apoc

In Cypher, INTERSECTIONkeywords can be used to perform the intersection operation of sets. INTERSECTIONKeywords are used to find common elements between two or more collections.

Here is INTERSECTIONan example of using keywords to perform set intersection operations:

cypherCopy codeMATCH (p1:Person {name: 'John'})
MATCH (p2:Person {name: 'Alice'})
RETURN apoc.coll.intersection(p1.hobbies, p2.hobbies) AS commonHobbies

In the above example, we are using the MATCH clause to match the "Person" nodes named "John" and "Alice" respectively. Then, in the RETURN clause, we use apoc.coll.intersectiona function to find the common elements between the "hobbies" collection properties of these two nodes, and name the result "commonHobbies".

The result of the query will return a list of common interests, which is the intersection of the "hobbies" collections of the two "Person" nodes.

Guess you like

Origin blog.csdn.net/twi_twi/article/details/130703688