|
13 | 13 | "id": "b1358e7b",
|
14 | 14 | "metadata": {},
|
15 | 15 | "source": [
|
16 |
| - "First, we need to run Memgraph. The easiest way to do this, is to run Memgraph Platform with Docker.\n", |
| 16 | + "\n", |
| 17 | + "## Prerequisites <a name=\"prerequisites\"></a>\n", |
| 18 | + "\n", |
| 19 | + "For this tutorial, we need to install:\n", |
| 20 | + "\n", |
| 21 | + "- [Docker](https://docs.docker.com/get-docker/) - to run Memgraph, since Memgraph is a native Linux application and cannot be installed on Windows and macOS\n", |
| 22 | + "- [Memgraph Platform](https://memgraph.com/docs/memgraph/installation) - the complete streaming graph application platform; follow the instructions to install Memgraph Platform with Docker for your OS\n", |
| 23 | + "- [GQLAlchemy](https://pypi.org/project/gqlalchemy/)\n", |
| 24 | + "\n", |
| 25 | + "> ### Memgraph Platform installation using Docker\n", |
| 26 | + ">\n", |
| 27 | + "> After we install Docker, we can run the Memgraph Platform container by running:\n", |
| 28 | + ">\n", |
| 29 | + ">```\n", |
| 30 | + ">docker run -it -p 7687:7687 -p 7444:7444 -p 3000:3000 memgraph/memgraph-platform\n", |
| 31 | + ">```\n", |
| 32 | + ">\n", |
| 33 | + ">**Memgraph Platform** contains:\n", |
| 34 | + ">\n", |
| 35 | + ">- **MemgraphDB** - the database that holds your data\n", |
| 36 | + ">- **Memgraph Lab** - visual user interface for running queries and visualizing graph data (running at >`localhost:3000`)\n", |
| 37 | + ">- **mgconsole** - command-line interface for running queries\n", |
| 38 | + ">- **MAGE** - graph algorithms and modules library\n", |
| 39 | + "\n", |
| 40 | + "We will use the **GQLAlchemy**'s object graph mapper (OGM) to connect to Memgraph and quickly execute **Cypher** queries. GQLAlchemy also serves as a Python driver/client for Memgraph. We can install it using:\n", |
| 41 | + "\n", |
17 | 42 | "```\n",
|
18 |
| - "docker run -it -p 7687:7687 -p 7444:7444 -p 3000:3000 memgraph/memgraph-platform\n", |
19 |
| - "```" |
| 43 | + "pip install gqlalchemy\n", |
| 44 | + "```\n", |
| 45 | + "\n", |
| 46 | + "> You may need to install [CMake](https://cmake.org/download/) before installing GQLAlchemy." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "id": "9d6dfef6", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "## Connect to Memgraph with GQLAlchemy" |
20 | 55 | ]
|
21 | 56 | },
|
22 | 57 | {
|
|
160 | 195 | "id": "05ebf808",
|
161 | 196 | "metadata": {},
|
162 | 197 | "source": [
|
| 198 | + "## Load the dataset\n", |
| 199 | + "\n", |
163 | 200 | "The simplest way to load a dataset into Memgraph is by using **Memgraph Lab** which is currently running on `localhost:3000`. The first thing you're going to see on that address is the **Quick Connect**. You just have to click `Connect`!"
|
164 | 201 | ]
|
165 | 202 | },
|
|
299 | 336 | "id": "9a13b90e",
|
300 | 337 | "metadata": {},
|
301 | 338 | "source": [
|
| 339 | + "## Map nodes and relationships\n", |
| 340 | + "\n", |
302 | 341 | "We can create a schema for this model inside the Python code by defining the classes that inherit from the `Node` and `Relationship` classes."
|
303 | 342 | ]
|
304 | 343 | },
|
|
309 | 348 | "metadata": {},
|
310 | 349 | "outputs": [],
|
311 | 350 | "source": [
|
312 |
| - "from gqlalchemy import Node, Relationship, Field\n", |
| 351 | + "from gqlalchemy import Node, Relationship\n", |
313 | 352 | "\n",
|
314 | 353 | "class Character(Node):\n",
|
315 | 354 | " name: str\n",
|
|
561 | 600 | "name": "stdout",
|
562 | 601 | "output_type": "stream",
|
563 | 602 | "text": [
|
564 |
| - "['Lord Snow']\n" |
| 603 | + "{'Lord Snow'}\n" |
565 | 604 | ]
|
566 | 605 | }
|
567 | 606 | ],
|
568 | 607 | "source": [
|
| 608 | + "season_1 = {'Winter Is Coming ', 'The Kingsroad ', 'Lord Snow', \n", |
| 609 | + " 'Cripples Bastards and Broken Things ', 'The Wolf and the Lion ', \n", |
| 610 | + " 'A Golden Crown ', 'You Win or You Die', 'The Pointy End ', \n", |
| 611 | + " 'Baelor ', 'Fire and Blood'}\n", |
| 612 | + "\n", |
569 | 613 | "results = memgraph.execute_and_fetch(\n",
|
570 | 614 | " \"\"\"\n",
|
571 | 615 | " MATCH (e:Episode)-[p:PART_OF]->(s:Season {number: 1})\n",
|
572 | 616 | " RETURN e;\n",
|
573 | 617 | " \"\"\"\n",
|
574 | 618 | ")\n",
|
575 | 619 | "\n",
|
576 |
| - "season_1_deaths = []\n", |
| 620 | + "season_1_deaths = set()\n", |
577 | 621 | "\n",
|
578 | 622 | "for result in results:\n",
|
579 |
| - " season_1_deaths.append(result[\"e\"].name)\n", |
| 623 | + " season_1_deaths.add(result[\"e\"].name)\n", |
580 | 624 | "\n",
|
581 |
| - "set_season_1_deaths = set(season_1_deaths)\n", |
582 |
| - "missing = list(sorted(season_1 - set_season_1_deaths))\n", |
583 |
| - "print(missing)" |
| 625 | + "print(season_1 - season_1_deaths)" |
584 | 626 | ]
|
585 | 627 | },
|
586 | 628 | {
|
|
601 | 643 | "name": "stdout",
|
602 | 644 | "output_type": "stream",
|
603 | 645 | "text": [
|
604 |
| - "['The Prince of Winterfell']\n" |
| 646 | + "{'The Prince of Winterfell'}\n" |
605 | 647 | ]
|
606 | 648 | }
|
607 | 649 | ],
|
|
612 | 654 | " RETURN e;\n",
|
613 | 655 | " \"\"\"\n",
|
614 | 656 | ")\n",
|
615 |
| - "season_2_deaths = []\n", |
| 657 | + "season_2_deaths = set()\n", |
| 658 | + "\n", |
616 | 659 | "for result in results:\n",
|
617 |
| - " season_2_deaths.append(result[\"e\"].name)\n", |
| 660 | + " season_2_deaths.add(result[\"e\"].name)\n", |
618 | 661 | "\n",
|
619 |
| - "set_season_2_deaths = set(season_2_deaths)\n", |
620 |
| - "missing_2 = list(sorted(season_2 - set_season_2_deaths))\n", |
621 |
| - "print(missing_2)" |
| 662 | + "print(season_2 - season_2_deaths)" |
622 | 663 | ]
|
623 | 664 | },
|
624 | 665 | {
|
|
639 | 680 | "name": "stdout",
|
640 | 681 | "output_type": "stream",
|
641 | 682 | "text": [
|
642 |
| - "['The Bear and the Maiden Fair']\n" |
| 683 | + "{'The Bear and the Maiden Fair'}\n" |
643 | 684 | ]
|
644 | 685 | }
|
645 | 686 | ],
|
|
650 | 691 | " RETURN e;\n",
|
651 | 692 | " \"\"\"\n",
|
652 | 693 | ")\n",
|
653 |
| - "season_3_deaths = []\n", |
| 694 | + "season_3_deaths = set()\n", |
| 695 | + "\n", |
654 | 696 | "for result in results:\n",
|
655 |
| - " season_3_deaths.append(result[\"e\"].name)\n", |
| 697 | + " season_3_deaths.add(result[\"e\"].name)\n", |
656 | 698 | "\n",
|
657 |
| - "set_season_3_deaths = set(season_3_deaths)\n", |
658 |
| - "missing_3 = list(sorted(season_3 - set_season_3_deaths))\n", |
659 |
| - "print(missing_3)" |
| 699 | + "print(season_3 - season_3_deaths)" |
660 | 700 | ]
|
661 | 701 | },
|
662 | 702 | {
|
|
677 | 717 | "name": "stdout",
|
678 | 718 | "output_type": "stream",
|
679 | 719 | "text": [
|
680 |
| - "['Blood of My Blood']\n" |
| 720 | + "{'Blood of My Blood'}\n" |
681 | 721 | ]
|
682 | 722 | }
|
683 | 723 | ],
|
|
688 | 728 | " RETURN e;\n",
|
689 | 729 | " \"\"\"\n",
|
690 | 730 | ")\n",
|
691 |
| - "season_6_deaths = []\n", |
| 731 | + "season_6_deaths = set()\n", |
| 732 | + "\n", |
692 | 733 | "for result in results:\n",
|
693 |
| - " season_6_deaths.append(result[\"e\"].name)\n", |
| 734 | + " season_6_deaths.add(result[\"e\"].name)\n", |
694 | 735 | "\n",
|
695 |
| - "set_season_6_deaths = set(season_6_deaths)\n", |
696 |
| - "missing_6 = list(sorted(season_6 - set_season_6_deaths))\n", |
697 |
| - "print(missing_6)" |
| 736 | + "print(season_6 - season_6_deaths)" |
698 | 737 | ]
|
699 | 738 | },
|
700 | 739 | {
|
|
1371 | 1410 | "name": "stdout",
|
1372 | 1411 | "output_type": "stream",
|
1373 | 1412 | "text": [
|
1374 |
| - "Daenerys Targaryen | BC: 0.2641671685970773\n", |
1375 |
| - "Lannister soldier | BC: 0.11163138590238252\n", |
1376 |
| - "Sons of the Harpy agent | BC: 0.04326120746568804\n", |
1377 |
| - "Jon Snow | BC: 0.041527339730212685\n", |
1378 |
| - "Wildling | BC: 0.03782396045716543\n", |
1379 |
| - "Stark Soldier | BC: 0.028609456095599384\n", |
1380 |
| - "Baratheon of Dragonstone soldier | BC: 0.027533816622595353\n", |
1381 |
| - "Cersei Lannister | BC: 0.02644509282920999\n", |
1382 |
| - "Stark soldier | BC: 0.02432818459201448\n", |
1383 |
| - "Nights Watch brother | BC: 0.022719177729567427\n" |
| 1413 | + "Daenerys Targaryen | BC: 0.26416716859707734\n", |
| 1414 | + "Lannister soldier | BC: 0.11163138590238253\n", |
| 1415 | + "Sons of the Harpy agent | BC: 0.04326120746568793\n", |
| 1416 | + "Jon Snow | BC: 0.04152733973021257\n", |
| 1417 | + "Wildling | BC: 0.037823960457165504\n", |
| 1418 | + "Stark Soldier | BC: 0.02860945609559936\n", |
| 1419 | + "Baratheon of Dragonstone soldier | BC: 0.027533816622595367\n", |
| 1420 | + "Cersei Lannister | BC: 0.026445092829209968\n", |
| 1421 | + "Stark soldier | BC: 0.02432818459201446\n", |
| 1422 | + "Nights Watch brother | BC: 0.02271917772956741\n" |
1384 | 1423 | ]
|
1385 | 1424 | }
|
1386 | 1425 | ],
|
|
0 commit comments