Last active
November 3, 2023 19:59
-
-
Save tomasonjo/aac988e46d8fbc9de125446e9d0d570a to your computer and use it in GitHub Desktop.
Marvel.cql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://sandbox.neo4j.com/?usecase=graph-data-science-blank-sandbox | |
// constraints | |
CREATE CONSTRAINT IF NOT EXISTS FOR (c:Character) REQUIRE c.id IS UNIQUE; | |
CREATE CONSTRAINT IF NOT EXISTS FOR (c:Comic) REQUIRE c.id IS UNIQUE; | |
// import | |
:auto LOAD CSV WITH HEADERS FROM | |
"https://raw.githubusercontent.com/tomasonjo/neo4j-marvel/master/data/edges.csv" as row | |
CALL { | |
WITH row | |
MERGE (h:Character{id:row.hero}) | |
MERGE (c:Comic{id:row.comic}) | |
MERGE (h)-[:APPEARED_IN]->(c) | |
} | |
IN TRANSACTIONS OF 5000 ROWS; | |
// inspect | |
MATCH p=(c:Character)-[r:APPEARED_IN]->() | |
WHERE c.id CONTAINS "SPIDER" RETURN p LIMIT 25; | |
// monopartite projection | |
:auto CALL { | |
MATCH (p1:Character)-->(:Comic)<--(p2:Character) where id(p1) < id(p2) | |
WITH p1, p2, count(*) AS commonComics | |
WHERE commonComics > 100 | |
MERGE (p1)-[k:KNOWS]-(p2) | |
SET k.score = commonComics | |
} IN TRANSACTIONS OF 10000 ROWS; | |
MATCH (c:Character) | |
WHERE EXISTS {(c)-[:KNOWS]-()} | |
SET c:Hero; | |
// graph loading | |
CALL gds.graph.project( | |
"marvel", | |
"Hero", | |
{KNOWS: {orientation:"UNDIRECTED"}} | |
); | |
// finding islands | |
CALL gds.wcc.stats("marvel"); | |
// PageRank | |
CALL gds.pageRank.write("marvel", | |
{writeProperty:"pagerank"}); | |
MATCH (c:Hero) | |
RETURN c.id AS name, c.pagerank AS score | |
ORDER BY score DESC | |
LIMIT 10; | |
// Louvain | |
CALL gds.louvain.write("marvel", | |
{writeProperty:"louvain"}); | |
MATCH (c:Hero) | |
RETURN c.louvain AS component, count(*) AS count | |
ORDER BY count DESC | |
LIMIT 10; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment