|
12 | 12 | }, |
13 | 13 | { |
14 | 14 | "cell_type": "markdown", |
15 | | - "id": "14bf5bb2", |
| 15 | + "id": "21ad21b2", |
16 | 16 | "metadata": {}, |
17 | 17 | "source": [ |
18 | | - "## Downloading data from Neo4j \n", |
| 18 | + "## Connecting to Neo4J \n", |
19 | 19 | "\n", |
20 | | - "Here is an example of using the Neo4JDownloader to retrieve all nodes from the graph. \n", |
21 | | - "Adapt the Neo4JDownloader class as you see fit for retrieving information. " |
| 20 | + "Here are some settings that allow to connect to Neo4J. Do not edit unless you know what you are doing. " |
22 | 21 | ] |
23 | 22 | }, |
24 | 23 | { |
25 | 24 | "cell_type": "code", |
26 | 25 | "execution_count": null, |
27 | | - "id": "ea28c628", |
| 26 | + "id": "4545c948", |
28 | 27 | "metadata": {}, |
29 | 28 | "outputs": [], |
30 | 29 | "source": [ |
|
53 | 52 | { |
54 | 53 | "cell_type": "code", |
55 | 54 | "execution_count": null, |
56 | | - "id": "1a349b96", |
| 55 | + "id": "d799a464", |
57 | 56 | "metadata": {}, |
58 | 57 | "outputs": [], |
59 | 58 | "source": [ |
|
71 | 70 | "\n", |
72 | 71 | " return Neo4JDownloader(NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD, NEO4J_DATABASE)\n", |
73 | 72 | "\n", |
74 | | - "def extract_data(nodes, relationships):\n", |
| 73 | + "def connect_neo4j(): \n", |
75 | 74 | " downloader = get_downloader()\n", |
| 75 | + " return downloader" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "id": "d3707369", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "## Neo4J Querrying: Examples of CYPHER Querries\n", |
| 84 | + "\n", |
| 85 | + "Here is an example of using the Neo4J Downloader to run some custom querries on the graph." |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "id": "02f8b4a2", |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "downloader = connect_neo4j()\n", |
| 96 | + "\n", |
| 97 | + "# QUERY: Get all nodes that have the EPFL string in their name (whether they are user, repository or org)\n", |
| 98 | + "\n", |
| 99 | + "epfl_query =\"\"\"\n", |
| 100 | + "MATCH (n)\n", |
| 101 | + "WHERE toLower(n.name) CONTAINS 'epfl'\n", |
| 102 | + "RETURN n.name AS name, labels(n) AS node_type;\n", |
| 103 | + "\"\"\"\n", |
| 104 | + "\n", |
| 105 | + "epfl_nodes = downloader.run_custom_query(epfl_query)\n", |
| 106 | + "print(\"RESULTS: all nodes that match the epfl string: \",epfl_nodes)\n", |
| 107 | + "\n", |
| 108 | + "# QUERY: Get all organizations\n", |
| 109 | + "\n", |
| 110 | + "orgs_query = \"\"\"\n", |
| 111 | + "MATCH (o:org)\n", |
| 112 | + "RETURN o.name AS organization;\n", |
| 113 | + "\"\"\"\n", |
| 114 | + "organizations = downloader.run_custom_query(orgs_query)\n", |
| 115 | + "print(\"RESULTS: all organizations in the graph: \",organizations)\n", |
| 116 | + "\n", |
| 117 | + "# QUERY : Get all users for an organization \n", |
| 118 | + "\n", |
| 119 | + "users_of_org_query = \"\"\"\n", |
| 120 | + "MATCH (u:user)-[:member_of]->(o:org)\n", |
| 121 | + "WHERE o.name = $org_name\n", |
| 122 | + "RETURN u.name AS user\n", |
| 123 | + "ORDER BY user;\n", |
| 124 | + "\"\"\"\n", |
| 125 | + "parameters = {\n", |
| 126 | + " \"org_name\": \"SwissDataScienceCenter\"\n", |
| 127 | + "}\n", |
| 128 | + "users_in_org = downloader.run_custom_query(users_of_org_query, parameters)\n", |
| 129 | + "print(\"RESULTS: users inside the organization: \", users_in_org)\n", |
| 130 | + "\n", |
| 131 | + "# QUERY: For a list of users get all their repositories\n", |
| 132 | + "\n", |
| 133 | + "# Here we base the query on the Contributor of edge.\n", |
| 134 | + "repos_of_users_query= \"\"\"\n", |
| 135 | + "MATCH (u:user)-[:contributor_of]->(repo:repo)\n", |
| 136 | + "WHERE u.name IN $user_list\n", |
| 137 | + "RETURN u.name AS user,\n", |
| 138 | + " repo.name AS repository\n", |
| 139 | + "ORDER BY user, repository;\n", |
| 140 | + "\"\"\"\n", |
| 141 | + "parameters = {\n", |
| 142 | + " \"user_list\": [\"yousra-elbachir\", \"Victor2175\", \"williamaeberhard\"]\n", |
| 143 | + "}\n", |
| 144 | + "users_and_their_repos = downloader.run_custom_query(repos_of_users_query, parameters)\n", |
| 145 | + "print(\"RESULTS: users and their repositories: \", users_and_their_repos)" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "markdown", |
| 150 | + "id": "14bf5bb2", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "## Downloading data from Neo4j \n", |
| 154 | + "\n", |
| 155 | + "Here is an example of using the Neo4JDownloader to retrieve all nodes from the graph. \n", |
| 156 | + "Adapt the Neo4JDownloader class as you see fit for retrieving information. " |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "id": "1a349b96", |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "def extract_data(nodes, relationships):\n", |
| 167 | + " downloader = connect_neo4j()\n", |
76 | 168 | "\n", |
77 | 169 | " try:\n", |
78 | 170 | " nodes_ids, nodes_features = downloader.retrieve_nodes(nodes)\n", |
|
0 commit comments