{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interagindo com Neo4j usando Cypher" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Usando uma magic cell para fazer consultas\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%reload_ext neo4j_cypher_query" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Consultas com visualização do grafo" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "CREATE (n1:Pessoa {nome:'Horácio', apelido:'Hora'})-[r:GOSTOU_DE]->(n2:Filme {titulo:'Django', ano:2012})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "CREATE (n1:Pessoa {nome:'Zeca', apelido:'Baleiro', nome_do_pai: 'Ivo'})-[r:GOSTOU_DE]->(n2:Filme {titulo:'Bastardos Inglórios', ano:2009})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "CREATE (cidade1:Cidade {nome:'Ilhéus'})-[r:FICA_EM]->(estado:Estado {titulo:'Bahia', populacao:150000})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "display" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "MATCH (irineuzinho:Pessoa {nome:'Horácio'}),\n", " (zequinha:Pessoa {nome:'Zeca'})\n", " \n", "CREATE (irineuzinho)-[relacionamento:CONHECE]->(zequinha)\n", "\n", "return irineuzinho, zequinha, relacionamento" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "display" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "CREATE (n1:Pessoa {nome_completo:'Asdrubal França', apelido:'Dubs'})-[r1:GOSTOU_DE]->(movie1:Filme {titulo:'Her', ano:2015})\n", "RETURN n1,r1,movie1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "display" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "\n", "match (a)-[r]->(b)\n", "match (c)\n", "\n", "return a, b, c, r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Criando nós e visualizando resultado" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "\n", "CREATE (node1:TipoA {propriedade1:'valor1', propriedade2:'valor2', label:'meu primeiro nó'}) \n", "return node1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "\n", "CREATE (node1:Pessoa {nome:'Fulano Silva', profissao:'Professor', label:'Prof. Fulano'}) \n", "return node1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "\n", "CREATE (node1:Pessoa {nome:'Cicrano Junior', profissao:'Engenheiro de dados', label:'Eng. Cicrano'}) \n", "return node1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Criando relacionamentos (Arestas)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "\n", "CREATE (person1:Pessoa {nome:'Matheus Mota', label:'Matheus'})-[r:DEU_LIKE]->(movie1:Filme {titulo:'Django', ano:2009, label:'Django'})\n", "return person1, movie1, r" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "display" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "\n", "CREATE (person1:Pessoa {nome:'Laura Lero', label:'Laura'})-[rel:DEU_DESLIKE]->(movie1:Filme {titulo:'The Matrix', ano:1999, label:'Matrix'})\n", "return person1, movie1, rel" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show full graph\n", "display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ligando dois nós já existentes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher show returned graph\n", "\n", "MATCH (node_pessoa:Pessoa), (node_filme:Filme)\n", "WHERE node_pessoa.nome = 'Laura Lero' AND node_filme.titulo = 'Her'\n", "CREATE (node_pessoa)-[r:DEU_LIKE]->(node_filme)\n", "RETURN node_pessoa, node_filme, r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Querying" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher\n", "\n", "MATCH (node_pessoa:Pessoa)-[:DEU_LIKE]->(node_filme:Filme)\n", "RETURN node_pessoa.nome, node_filme.titulo" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%cypher\n", "MATCH (node_pessoa:Pessoa)-[:DEU_LIKE]->(node_filme:Filme)\n", "WHERE node_filme.titulo = 'Django'\n", "RETURN node_pessoa.nome as nome_usuario, node_filme.titulo" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# %%cypher\n", "# match (n)-[r]->(m)\n", "# match (x)\n", "# delete r, n, m, x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 4 }