This article is participating in Python Theme Month. See the link to the event for more details

Let’s see if the gold digger is paying attention to you…

Introduction to the

Neo4j

Neo is a network – a network oriented database – that is, it is an embedded, disk-based Java persistence engine with full transactional features, but it stores structured data on the network rather than in tables. A network (mathematically called a graph) is a flexible data structure that allows for more agile and rapid development patterns.

  • Open source, ACID support, with high availability clusters for enterprise deployment.
  • Comes with Web-based administration tools, including full transaction support and a visual node link graph browser;
  • The built-in REST Web API interface is accessible from most programming languages, as well as the proprietary Bolt protocol using official drivers;

Secondary installation

Docker Hub installation address is recommended.

The advantages of a Docker installation are:

  • It does not depend on any locale of the host, just need to install Docker.
  • Easy to deploy, a single command to implement deployment.
  • The configuration is simple. You do not need to configure environment variables and resource parameters.
  • Restart at any time to recover, encounter problems can be quickly restored.
# Boot directly from a computer with a Docker environment
docker run -p 7474:7474 -p 7687:7687 -v /d/docker/neo4j:/data --restart=always neo4j
Copy the code

After the installation is successful, the browser visits http://localhost:7474/ for initial Settings:

  • Enter the default user name and passwordneo4j/neo4j
  • Change the password toadmin
  • Log in to the system again

Cypher query

Cypher is similar to Mysql database with query language SQL and Neo4j with CQL as the query language.

Only simple statements are shown here. For details, see the Neo4j documentation

Create an object

Create a Person object named Zhang SAN
CREATE (:Person {name:"Zhang"});

Build a relationship between Zhang SAN and Li Si
MATCH (a:Person {name: "Zhang"}) MATCH (b:Person {name: "Bill"}) MERGE (a)-[:Follower]->(b);
Copy the code

The query object

Return all Person objects
MATCH (p:Person) RETURN p;

Select * from Person where name=" three"
MATCH (p:Person) where p.name="Zhang" RETURN p;

# Query all Person objects whose relationship between two nodes is Follower
MATCH(p1:Person) - [:Follower]->(p2:Person)  return p1,p2;
Copy the code

Delete the object

Delete all Person objects
MATCH(p:Person) DETACH DELETE p;
Copy the code

Modify the object

Add the age attribute to the Person object and replace the name attribute
MATCH (p:Person{name:"Zhang"}) set p={name:"Zhang"} return p;

# add age attribute to Person object
MATCH (p:Person{name:"Zhang"}) set p+={name:"Zhang"} return p;
Copy the code

Create indexes

Create index for the name property of the Person object
CREATE INDEX ON :Person(name);
Copy the code

Python build data

Data preparation

The data used here is the nugget’s concern data, so first encapsulate the nugget’s interface to get the followers

import requests

class JueJin(object) :
    followers_url = "https://api.juejin.cn/user_api/v1/follow/followers"
    followees_url = "https://api.juejin.cn/user_api/v1/follow/followees"

    def __init__(self) :
        self.session = requests.session()

    def get_followers(self, user_id, cursor, limit) :
        params = {
            "user_id": user_id,
            "cursor": cursor,
            "limit": limit
        }
        return self.session.request("get", self.followers_url, params=params).json()

    def get_followees(self, user_id, cursor, limit) :
        params = {
            "user_id": user_id,
            "cursor": cursor,
            "limit": limit
        }
        return self.session.request("get", self.followees_url, params=params).json()
Copy the code

Encapsulate a query statement

This encapsulates the basic query language for simplicity:


from neo4j import GraphDatabase

class App(object) :

    def __init__(self, uri, username, password) :
        self.driver = GraphDatabase.driver(uri, auth=(username, password))

    def close(self) :
        self.driver.close()

    @classmethod
    def create_person(cls, tx, name) :
        tx.run("CREATE (:Person {name: $name})", name=name)

    @classmethod
    def create_friendship(cls, tx, name_a, name_b) :
        tx.run("MATCH (a:Person {name: $name_a}) "
               "MATCH (b:Person {name: $name_b}) "
               "MERGE (a)-[:Follower]->(b)",
               name_a=name_a, name_b=name_b)

    @classmethod
    def delete_person(cls, tx) :
        tx.run("MATCH(p:Person) DETACH DELETE p;".)    @staticmethod
    def _find_and_return_person(tx, name) :
        query = (
            "MATCH (p:Person) "
            "WHERE p.name = name "
            "RETURN p.name AS name"
        )
        result = tx.run(query, name=name)
        return [record["name"] for record in result]
Copy the code

Creating Nodes in Batches

def main() :
    # Connecting to Aura, use the "neo4j+s" URI scheme
    scheme = "neo4j"
    host_name = "deepin"
    port = 7687
    url = "{scheme}://{host_name}:{port}".format(scheme=scheme, host_name=host_name, port=port)
    user = "neo4j"
    Enter your password here
    password = "admin"
    app = App(url, user, password)

    juejin = JueJin()
    # Replace the information of the person you want to see with that of the person you want to see
    user_id, user_name = "993614678985085"."Fried rice with tomatoes and Eggs."
    cursor, limit = 0.20
    followees, followers = [], []

    has_more = True
    while has_more:
        result = juejin.get_followees(user_id, cursor, limit)
        data = result['data'] ['data']
        followees += data
        has_more = result['data'] ['hasMore']
        cursor = result['data'] ['cursor']

    cursor, limit = 0.20
    has_more = True
    while has_more:
        result = juejin.get_followers(user_id, cursor, limit)
        data = result['data'] ['data']
        followers += data
        has_more = result['data'] ['hasMore']
        cursor = result['data'] ['cursor']

    names = {f["user_name"] for f in followees + followers}

    with app.driver.session() as session:

        session.write_transaction(app.delete_person)

        session.write_transaction(
            app.create_person, user_name)

        for name in names:
            session.write_transaction(
                app.create_person, name)

        for person in followees:
            session.write_transaction(
                app.create_friendship, user_name, person['user_name'])

        for person in followers:
            session.write_transaction(
                app.create_friendship, person['user_name'], user_name)

    app.close()
Copy the code

View the results

MATCH(p:Person) return p;

MATCH(p1:Person) – [:Follower]->(p2:Person) – [:Follower]->(p1:Person) return p1,p2;

The last

This is just a simple example of how to use Neo4j. There are actually many uses for Neo4j that are well worth exploring. Let me share some whimsical ideas:

    1. Build a map of your mining users and see how many friends are between you and me.
    1. It can be used for search recommendations, to recommend articles that your friends follow and your friends like.
    1. You can visualize who you’re following who you’re following, and you can follow who you’re following.

Of course, these ideas are based on huge amounts of data about digging friends, and it’s a physical job. Emm will be fine.

If you think my project is helpful to you, please click ❤️❤️❤️. See the project code GitHub And welcome to Star Fork.