APOC (Awesome Procedures On Cypher)

repository·dev·Indexed 42 days ago

https://github.com/neo4j/apoc

A library of over 400 user-defined procedures and functions for Neo4j that extends Cypher's capabilities in data integration, graph algorithms, and data conversion.

Tokens
1.2K
Snippets
7
Records
9
Agent score
3%

What's inside APOC

  1. Understand APOC procedure signatures

    dev

    A procedure signature consists of the procedure name, its parameters (name and type), and its return columns (name and type). You can find these via apoc.help() or in error messages when a procedure is called incorrectly.

    Example signature format for apoc.algo.dijkstra: apoc.algo.dijkstra (startNode :: NODE?, endNode :: NODE?, relationshipTypesAndDirections :: STRING?, weightPropertyName :: STRING?) :: (path :: PATH?, weight :: FLOAT?)

    CALL apoc.help("dijkstra")
  2. Install APOC manually via JAR download

    dev

    APOC requires a version that matches your Neo4j installation. The first two version numbers of Neo4j and APOC must match (e.g., Neo4j 5.4.x requires APOC 5.4.x).

    1. Go to the APOC releases page.
    2. Download the matching binary .jar file.
    3. Place the .jar file into your $NEO4J_HOME/plugins folder.
  3. Run Neo4j with APOC in Docker (Production)

    dev

    In production, you should download the specific APOC version matching your Neo4j version, place it in a local plugins directory, and mount that directory as a volume to /plugins in the container.

    mkdir plugins
    pushd plugins
    wget https://github.com/neo4j/apoc/releases/download/{apoc-release}/apoc-{apoc-release}-core.jar
    popd
    docker run --rm -e NEO4J_AUTH=none -p 7474:7474 -v $PWD/plugins:/plugins -p 7687:7687 neo4j:{neo4j-version}
  4. Install APOC with Neo4j Desktop

    dev

    To install APOC using Neo4j Desktop:

    1. Open Neo4j Desktop and select your database.
    2. Go to the Manage screen.
    3. Navigate to the Plugins tab.
    4. Click Install in the APOC box.
    5. Wait for the green check mark to appear next to "APOC".
  5. Build APOC from source

    dev

    To build the current development branch from source using Gradle:

    1. Clone the repository.
    2. Run ./gradlew shadow to build the shadow JAR.
    3. Copy the resulting JAR from build/extended/libs/ to your $NEO4J_HOME/plugins folder.
    4. Restart Neo4j.
    git clone https://github.com/neo4j/apoc
    cd apoc
    ./gradlew shadow
    cp build/extended/libs/apoc-<version>.jar $NEO4J_HOME/plugins/
    $NEO4J_HOME/bin/neo4j restart
  6. Run Neo4j with APOC in Docker (Development)

    dev

    For development environments, you can use the NEO4J_PLUGINS environment variable to automatically install APOC.

    Note: This method is intended for development and is not recommended for production.

    docker run \
        -p 7474:7474 -p 7687:7687 \
        --name neo4j-apoc \
        -e NEO4J_apoc_export_file_enabled=true \
        -e NEO4J_apoc_import_file_enabled=true \
        -e NEO4J_apoc_import_file_use__neo4j__config=true \
        -e NEO4J_PLUGINS=["apoc"] \
        neo4j:{neo4j-version}
  7. Use APOC Functions and Procedures in Cypher

    dev

    APOC extends Cypher with two types of capabilities:

    • Functions: Can be used in any expression or predicate, similar to built-in Cypher functions.
    • Procedures: Called stand-alone using the CALL clause, or integrated into Cypher statements using YIELD to pass data to subsequent clauses.

    Example of integrating a procedure into a Cypher statement to load JSON data:

    WITH 'https://raw.githubusercontent.com/neo4j/apoc/{branch}/core/src/test/resources/person.json' AS url
    
    CALL apoc.load.json(url) YIELD value as person
    
    MERGE (p:Person {name:person.name})
       ON CREATE SET p.age = person.age, p.children = size(person.children)
  8. Discover APOC procedures using apoc.help()

    dev

    You can use the built-in apoc.help() function to find information about procedures and functions. Passing a keyword to the function will list names, descriptions, signatures, and required roles based on that keyword.

    CALL apoc.help("dijkstra")