<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Julien Genovese on Medium]]></title>
        <description><![CDATA[Stories by Julien Genovese on Medium]]></description>
        <link>https://medium.com/@juliengenovese?source=rss-b7caabd2f9d1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*TY2hGXpF7y_Sk12H</url>
            <title>Stories by Julien Genovese on Medium</title>
            <link>https://medium.com/@juliengenovese?source=rss-b7caabd2f9d1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 09:05:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@juliengenovese/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An introduction to Graph Neural Networks]]></title>
            <link>https://medium.com/data-reply-it-datatech/an-introduction-to-graph-neural-networks-b2d756c6b448?source=rss-b7caabd2f9d1------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2d756c6b448</guid>
            <dc:creator><![CDATA[Julien Genovese]]></dc:creator>
            <pubDate>Wed, 13 Apr 2022 13:50:52 GMT</pubDate>
            <atom:updated>2022-04-13T13:50:52.260Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BBBrX_qJoinqCjoQCsKNYw.jpeg" /></figure><p>Graph neural networks are an emerging trend in the last years. Despite a lot of materials to implement them is not always clear the reason why they are so important and why they are changing the way we are going to see connected data and so, graphs.</p><p>This article will try to explain the reason for this growth seeing the problems they try to solve.</p><p>It will be divided in this way:</p><ul><li>we will first see <strong>some limitations of classical graph machine learning</strong></li><li>we will understand <strong>why naive approaches don’t work</strong> to model a graph problem</li><li>we will do an <strong>introduction to graph neural networks</strong> understanding each step of the building blocks.</li></ul><h3>1. LIMITATIONS OF GRAPH MACHINE LEARNING</h3><p>Talking about classical graph machine learning, we think about algorithms like Page Rank centrality, Betweenness centrality, Label Propagation, Jaccard similarity, and so on.</p><p>We have a bunch of algorithms trying to do different things.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*ZJwYNmsi5BFwUGizW1nRBQ.png" /><figcaption><strong>Page Rank</strong></figcaption></figure><p>What they have in common is that <strong>they all study the topology of the graph for a certain goal they are designed to</strong>.</p><p><strong>All these algorithms can be used</strong>, for instance, <strong>to predict a label associated with a node or an edge</strong>: the label propagation predicts it directly; a big value of page rank can be associated with fraud in a banking network; a high Jaccard similarity between two nodes where one of them is a hustler can mean the other one is a hustler too.</p><p>The main issue is that <strong>they usually don’t take into account the features of each node or link.</strong></p><p>So, in general, a workaround for this problem is to come back to a classical machine learning environment and attach to the variables of each node some features extracted from the classical graph algorithms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cmZhK29EzGJvuAd59C-3Cg.png" /><figcaption><strong>Graph Topology and features table</strong></figcaption></figure><p>This has the clear problem that the <strong>information related to the node features doesn’t propagate through the network and only the connection information is captured.</strong></p><p>In addition, we don’t know what are the best topological features that represent the graph, and <strong>we have to try a lot of different algorithms that a supervised/unsupervised model will see later if are important.</strong></p><p>It’s quite clear that this approach is sometimes hard to apply.</p><p>The first possible solution to the feature graph exaction is proposed with the <strong>embeddings </strong>like the Deep Walk, Node2Vec, … They try to <strong>encode the graph</strong> and in a matrix that ideally has to <strong>learn some local graph features</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*oxF-ccs8z2ktiPdTZ_TE0w.png" /><figcaption><strong>Encoder nodes</strong></figcaption></figure><p>But this doesn’t solve the problem of feature propagation in the graph.</p><p>In addition,<strong> these embeddings don’t usually have separate training and validation phase</strong>. So we have to embed the whole graph once, and we have no chance to extend this embedding to new nodes without re-embed the entire graph.</p><p>And the last problem is that <strong>embedding usually captures only local features related to the graph but not global features.</strong></p><p>For this reason, we have to move on to something else.</p><h3>2. An understanding of naive approaches</h3><p>We can try to solve this problem using a <strong>Naive approach</strong>.</p><p>We could first take the <strong>adjacency matrix of the graph</strong>, where we have a row and a column for each node and a binary value if a link exists. We can <strong>add to each row the features associated with the node and try to use a classical MLP network.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ST93w2Ngl1pqsYMt8eNCVQ.png" /><figcaption><strong>Adjacency matrix joined with a feature matrix</strong></figcaption></figure><p>This seems to be fine to learn graph and node features and combine them.</p><p>But this doesn’t work for 3 reasons:</p><ul><li><strong>Parameter dimensionality</strong>: the resulting matrix has “<em>number of nodes + number of features” </em>parameters but we only have “<em>number of nodes”</em><strong><em> </em></strong>number of training samples.</li><li><strong>Changing topology</strong>: If we add a node, the adjacency matrix changes and so does the input matrix to the network. So it’s impossible to understand how to deal with such an input.</li><li><strong>Changing order of nodes</strong>: It’s sensitive to node ordering. If we change the way we give an id, the adjacency matrix changes and so does the input matrix, but the graph doesn’t change. It makes no sense.</li></ul><p>The naive approach clearly doesn’t fit well.</p><h3>3. An introduction to graph neural networks</h3><p>So let’s move on and introduce Graph neural networks.</p><p>To better understand why they are the solution to the previous problems we will construct them step by step.</p><p>First of all, each node needs a vector of d-dimension (where d has to be defined) defined as “<strong>embedding</strong>” as before<strong> but capturing both features and connections information</strong>. We denote it as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/26/0*vkr07OnAcpFCfLbZ" /><figcaption>embedding of a node <strong>v</strong></figcaption></figure><p>Let’s see how to construct it. We will do it iteratively.</p><p>In the first step we start with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/71/0*VwLT7Keu4gVWUR-i" /><figcaption><strong>embedding of a node v at the first step</strong></figcaption></figure><p>So we say that <strong>the embedding at the beginning is simply the feature vector related to the node.</strong></p><p>Now, to update this embedding to the next step we have to understand how to introduce the topology of the graph.</p><p>For doing it we need to note the<strong> similarity between the pixels in an image and the nodes in a graph.</strong></p><p>In fact, the graph can be seen as an unstructured grid of pixels, the nodes, connected to each other in a non-symmetric way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*V0-_6oUEqErO0Cr-SVJlhw.png" /></figure><p>With this in mind we can try to emulate the CNN from computer vision by taking the entire neighborhood of a node <em>v</em> and summing all over it using a unitary matrix as a filter:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/0*wXB3yLfmXPqKnOvj" /></figure><p>So what we are doing is to sum the features of the neighbors and update the embedding.</p><p>Let’s refine this updating step.</p><p>We note that it can be useful to divide by the number of neighbors to give a sense of the “average signal from neighbors”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/158/0*jd9NUw8UIECQcolO" /></figure><p>We see that using <strong>an average of the embeddings allows us to take the information from the neighbors regardless of the node id ordering in the adjacency matrix,</strong> solving the problem that emerged in the Naive solution.</p><p>After that, usually, we can introduce an MLP neural network to capture a more complex interaction:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/221/0*ezF54VfnXxb_8aNd" /></figure><p>using a weight matrix <strong><em>W </em></strong>and a <strong>nonlinearity sigma.</strong></p><p><strong>The matrix doesn’t depend on the adjacency matrix</strong>, but only on the dimension of the embedding, that we can decide.</p><p>In such a way we are collecting all the information from the neighborhood but not from the node itself.</p><p>Since its personal embedding at the step before can play an important role in the embedding of the node at the next step we can add it with a different weight matrix <strong><em>B</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/0*56Ze3tgstMDEUFXn" /></figure><p>This is all the information we need but there is something missing.</p><p>The fact is that <strong>we are taking only the information from the neighbors</strong>. But what if we want to take <strong>some information from neighbors of neighbors</strong>?</p><p>In this case, we introduce the concept of a <strong>layer.</strong></p><p>In each layer, we take the embedding of the previous layer, and update it with the information of the neighbors, summarizing them as explained before using the weight matrices.</p><p>We note that in each layer, we take the information from the neighbors, so one hop away. If we use k layers, we take the info k hop away.</p><p>The number of layers we have to take depends on the problem and how the nodes are connected.</p><p>Let’s note that the starting layer is the feature matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FIaNQVQl3IMFGxfY-6kSaQ.png" /><figcaption><strong>Two-layers architecture</strong></figcaption></figure><p>So we are ready to write the equation for the upgrading of the embedding:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/0*U1BUNP4fvyrsnFTW" /></figure><p>where</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/27/0*IqSXId8uxdYmCXzz" /></figure><p>is the embedding of the node <em>v </em>at layer <em>k.</em></p><p><strong>As we just saw this term capture all the features and interaction information.</strong></p><p>With these embeddings, we can feed a classification or regression algorithm or whatever we want.</p><p>In general, this is called <strong><em>message-passing architecture </em></strong>and it’s divided into these steps<strong><em>:</em></strong></p><blockquote><strong>1. Message computation</strong>. The neighbor u creates the message and send it to v:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/0*yzy87N062Kdf0y8X" /><figcaption><strong>Message computation</strong></figcaption></figure><blockquote>2. <strong>Aggregation. </strong>Node v takes all the messages and aggregates them:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/0*JzJs_YGWBCVe47bX" /><figcaption><strong>Aggregation step</strong></figcaption></figure><blockquote>3. <strong>Upgrade of the embedding</strong>: in this part, we take the previous calculations and pass this info through a nonlinearity:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/0*hw9Uo5fCqgOyFYdz" /><figcaption><strong>Upgrade with nonlinearity</strong></figcaption></figure><blockquote>The nonlinearity can be whatever you want: a sigmoid, a ReLU, and so on.</blockquote><p>We note that each node has a different structure in this equation due to the graph topology. So in the beginning, we could think that we need a different neural network for each node, a different matrix <strong><em>W_k </em></strong>and <strong><em>B_k</em></strong> for each node.</p><p>But the important point is that <strong>these parameters are shared across the nodes</strong>.</p><p>It’s quite clear that this is super useful <strong>for further generalizing to new nodes</strong>.</p><p>In fact, if we know the matrix <strong><em>W_k </em></strong>and <strong><em>B_k </em></strong>we can create the new embedding for the node.</p><p>And for learning them we can use the last layer of the embedding to do a certain task, create a loss, and optimize the entire embedding layers with backpropagation!</p><h3>CONCLUSION</h3><p>So, that’s all! We have seen why graph neural networks can solve all the problems we just saw and can be very useful for analyzing graphs.</p><p>Clearly, this is only an introduction and we need to understand how to stack multiple layers together and how to implement it.</p><p>We will see in the next article all these things.</p><p>For the moment I propose you some references:</p><ul><li>node2Vec embedding: <a href="https://arxiv.org/abs/1607.00653">https://arxiv.org/abs/1607.00653</a></li><li>graph neural network review: <a href="https://arxiv.org/ftp/arxiv/papers/1812/1812.08434.pdf">https://arxiv.org/ftp/arxiv/papers/1812/1812.08434.pdf</a></li><li>course on graph neural network: <a href="http://web.stanford.edu/class/cs224w/">http://web.stanford.edu/class/cs224w/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2d756c6b448" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-reply-it-datatech/an-introduction-to-graph-neural-networks-b2d756c6b448">An introduction to Graph Neural Networks</a> was originally published in <a href="https://medium.com/data-reply-it-datatech">Data Reply IT | DataTech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Graph Database in Neo4j: from zero to hero!]]></title>
            <link>https://medium.com/data-reply-it-datatech/graph-database-in-neo4j-from-zero-to-hero-dd6d82a47b95?source=rss-b7caabd2f9d1------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd6d82a47b95</guid>
            <dc:creator><![CDATA[Julien Genovese]]></dc:creator>
            <pubDate>Tue, 08 Feb 2022 09:33:13 GMT</pubDate>
            <atom:updated>2022-03-07T11:26:14.431Z</atom:updated>
            <content:encoded><![CDATA[<p>How to create your graph database in Neo4j from scratch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*msrSj1EKKXKDlu8royiVHg.jpeg" /><figcaption>Stonehenge, <a href="https://en.wikipedia.org/wiki/Wiltshire">Wiltshire</a></figcaption></figure><p>Today we are going to talk about<strong> how to create a graph database in </strong><a href="https://neo4j.com/"><strong>Neo4j</strong></a>, one of the main graph technology today, starting from zero.</p><p>We will see how to create indexes and constraints, model the graph, and understand how to use the easiest methods to load data in a database, using CSV files.</p><p>Although this is the starting point for all the graph analyses, sometimes is not easy to understand the entire path.</p><p>In practice we will see all these steps:</p><ol><li><strong>Initial setup</strong></li><li><strong>Model the Graph</strong></li><li><strong>Create the indexes and constraints</strong></li><li><strong>Load the data into the graph</strong></li><li><strong>Query the graph.</strong></li></ol><p>For the dataset, we will use a reduced version of the “Crime Investigation Graph example” that you can see <a href="https://github.com/JulienGenovese/JulienGenovese/tree/master/neo4j/crime_investigation_folder/data">here</a>. The whole dataset and documentation are <a href="https://github.com/neo4j-graph-examples/pole">here</a>. According to the Github description of the problem, we will “ <em>Explore connections in crime data using the POLE — Person, Object, Location, Event — model in a public dataset from Manchester, U.K.”</em></p><h3><strong>1. Initial setup</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*pTOccNfJqP3MwcjPasqM2A.jpeg" /><figcaption><a href="https://neo4j.com/">Neo4j Logo</a></figcaption></figure><p>First up we say that we will work locally using Neo4j Desktop that you can download <a href="https://neo4j.com/download/">here</a>.</p><p>After the installation, open it and <strong>create a new project</strong>, calling it as you want. I chose <em>“Crime_investigation_reduced”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*1cLBkzrwdmeDXM_f92-esg.png" /><figcaption>My folder projects</figcaption></figure><p>After that <strong>add a local DBMS</strong> clicking the “Add” button and choose a name and a password for your DBMS.</p><p>Now you have to <strong>start it</strong>. It will create a Neo4j instance, a process that contains 2 databases:</p><ul><li><strong>system</strong>: the system database stores metadata about the databases for the installation, as well as security configuration.</li><li><strong>neo4j</strong>: the default database is the user database where you typically implement your graph data model.</li></ul><p>In this case, we will use the neo4j database but in general, starting from the system database, you can create other ones.</p><h3><strong>2. Model the graph</strong></h3><p>After we have set up our database we have to model the graph.</p><p>In this case, we are using a reduced version of the original graph and we take only the person nodes and their relationships. An example of several relationships is in the picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*JU00XpUOKzVhOkdTTut8DA.png" /><figcaption>The reduced graph model, using arrows.app</figcaption></figure><p>In this case, the nodes are the people and the relationships are what the people know about other people.</p><p>From the technical point of view, each node has a “Person” label while each relationship can have different possible labels, associated with the type of relationship.</p><p>This is a very simple model but in this article, we are more interested in the loading part. In a future article, we will focus more on how to model a graph.</p><h3><strong>3. Create the indexes and constraints</strong></h3><p>Indexes and constraints creation is a very important step in modeling the graph to avoid duplications in nodes and to accelerate some queries.</p><p>Let’s see in detail what indexes and constraints are.</p><ul><li><strong>CONSTRAINTS</strong>: in your graph, you will want to provide <strong>uniqueness </strong>for some node properties. For example, in our case, we want that the <em>“id” </em>identifies univocally the node. <br>In addition, sometimes you may want to enforce the creation of <strong>mandatory properties</strong> for selected nodes. For instance, we may want a name and a surname for our people.</li></ul><p>The general code of the constraint for uniqueness is:</p><pre>CREATE CONSTRAINT &lt;name_of_constraint&gt; ON (&lt;name node&gt;: &lt;label&gt;) ASSERT &lt;name_node&gt;.&lt;property&gt; IS UNIQUE</pre><p>And for the mandatory property:</p><pre>CREATE CONSTRAINT &lt;name_of_constraint&gt; ON (&lt;name node&gt;: &lt;label&gt;) ASSERT exists(&lt;name_node&gt;.&lt;property&gt;)</pre><ul><li><strong>INDEXES</strong>: they are used to<strong> improve the initial node lookup performance</strong>, but they require additional storage in the graph to maintain and also add the cost of creating or modifying property values that are indexed. You use it to <strong>improve the engine performance</strong> on the property associated with it.</li></ul><p>The code for the index is:</p><pre>CREATE INDEX &lt;name_of_index&gt; FOR (&lt;name node&gt;:&lt;label&gt;) ON (&lt;name node&gt;.&lt;property&gt;)</pre><p>Note: <strong>a uniqueness constraint it’s always an index</strong>. So when we create a uniqueness constraint on a property we don’t need to create a related index.</p><p>Now, how to use constraints and indexes in our model?</p><p>First of all, start your database and open the Neo4j Browser, clicking “Open” from your DBMS in Neo4j Desktop.</p><p>In a cell, run this query:</p><pre>CREATE CONSTRAINT idPersonUnique ON (p:Person) ASSERT p.Id IS UNIQUE;<br>CREATE CONSTRAINT NamePersonExists ON (p:Person) ASSERT exists(p.name);<br>CREATE CONSTRAINT SurnamePersonExists ON (p:Person) ASSERT exists(p.surname);</pre><p>In this case, <strong>we want to identify a node using its Id and we want to be sure they have a name and a surname.</strong></p><p>To see your constraints you can use:</p><pre>SHOW CONSTRAINTS</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ayk5ziLxUiue18fPTkTWRA.png" /><figcaption>Result of the query to see the constraints</figcaption></figure><p>We can create an index for better searching the name and surname:</p><pre>CREATE INDEX nameIndex FOR (p:Person) ON (p.name);</pre><pre>CREATE INDEX SurnameIndex FOR (p:Person) ON (p.surname)</pre><p>We can see the results using</p><pre>SHOW INDEX</pre><p>And the result is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*duNsoB1vEmfaHIQmcvJAYA.png" /><figcaption>Result of the query to see the indexes</figcaption></figure><p>In this case, we see the two indexes and the uniqueness constraint.</p><p>Now we are ready to move on to the loading part.</p><h3><strong>4. LOAD DATA IN THE GRAPH</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E-rNQRJXdZetb5AsjCznpw.jpeg" /></figure><p>In this part, we will focus on<strong> loading data with LOAD CSV and the APOC library</strong>. We will use some CSV files because they are very common in RDMS and they are rather flexible. <br>These methods are easy to understand but can only be used for offline loading.</p><p><strong><em>4.1 LOAD CSV</em></strong></p><p>LOAD CSV is the easiest method to load data but the slowest as well.</p><p><strong>With the LOAD CSV method, we can load up to 100k rows.</strong></p><p>You can get your data in two ways:</p><ul><li>from <strong>URL: </strong>in this case, you have to write the URL where your data stays.</li><li>from the <strong>import folder: </strong>in this case, you have to come back to Neo4j Desktop and open the import folder, and put there your CSV files:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Es1nZ3kemeyGxUwe-JVMhw.png" /><figcaption><strong>How to find the import folder</strong></figcaption></figure><p>After that, the first step using the <a href="https://neo4j.com/docs/cypher-manual/current/clauses/load-csv/">LOAD CSV</a> is to analyze the file we want to load.</p><p>In this case, we have a file in the import folder so we use:</p><pre>LOAD CSV FROM “file:///nodes_people_reduced.csv” AS line</pre><pre>FIELDTERMINATOR &#39;,&#39;</pre><pre>RETURN line </pre><pre>LIMIT 10</pre><p>The <strong><em>FIELDTERMINATOR </em>field is used to understand what separator we need</strong> for the CSV. The default is the comma, so we can remove it in our case.</p><p>The output of the previous command is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/1*7CZXHheI06a9v74dZTTDAA.png" /><figcaption>Result of the first data lookup</figcaption></figure><p>It’s clear that:</p><ul><li>we need a header for our data.</li><li>the age column has a lot of missing values.</li><li>our data are duplicated.</li></ul><p>If we are interested in the total number of rows in our file we use:</p><pre>LOAD CSV WITH HEADERS FROM “file:///nodes_people_reduced.csv”</pre><pre>AS line</pre><pre>RETURN COUNT(line)</pre><p>and we can see that we have 2359 rows. Not a big dataset.</p><p>Let’s move on and create the nodes.</p><p>We can do it with the <strong>MERGE command</strong>. In contrast with the CREATE command, <strong>this one avoids duplicate data using a property to check the duplication</strong>. In our case, we will use the “id” property. In addition, we use ON CREATE SET to set some properties when the node is created.</p><p>NOTE: <strong>MERGE check is computationally intensive</strong>. The reason why we add the uniqueness constraint before is that being an index, it’s easier for the merging to check the property.</p><p>Now we can create the nodes with:</p><pre>LOAD CSV WITH HEADERS FROM “file:///nodes_people_reduced.csv”</pre><pre>AS row</pre><pre>WITH row</pre><pre>WHERE row._id IS NOT NULL</pre><pre>MERGE (p:Person {id: row._id})</pre><pre>ON CREATE SET</pre><pre>    p.name = row.name,<br>    p.surname = row.surnmame</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*QY_s_KeuI2BMGzBa7kd6vw.png" /></figure><p>In this way, we have loaded all the nodes at once.</p><p><strong>Sometimes the dataset is very big and the rows don’t fit in memory.</strong> In that case, we can use the <a href="https://neo4j.com/developer/neo4j-apoc/">APOC library</a> to commit a <strong>bunch of nodes.</strong> We will see how in the next chapter.</p><p>Let’s move on to the relationships:</p><pre>LOAD CSV FROM “file:///relationship_people_reduced.csv”</pre><pre>AS line </pre><pre>RETURN line</pre><pre>LIMIT 3</pre><p>and the result is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*d3X8UOW4hgwSmu_NcBJvzw.png" /></figure><p>In this case, we see that:</p><ul><li>we need a header.</li><li>we have a “_start” and a “_end” column to create the relationship.</li><li>the third column “_type” is the type of the relationship.</li></ul><p>To load the relationships <em>we can think to use a solution like that</em>:</p><pre>LOAD CSV WITH HEADERS</pre><pre>FROM &quot;file:///relationship_people_reduced.csv&quot; AS row</pre><pre>MATCH (starting_node:Person {id:row._start})</pre><pre>MATCH (ending_node:Person {id:row._end})</pre><pre>CREATE (starting_node)-[rel:row._type]-&gt;(ending_node)</pre><pre>RETURN starting_node, rel, ending_node</pre><p>but <strong>it doesn’t work!!!!</strong></p><p><strong>The reason is that the label is parametric and Neo4j doesn’t allow this.</strong></p><p>In this case, we have to move on to the APOC library to solve this problem.</p><p><strong><em>4.2 APOC LIBRARY</em></strong></p><p>Sometimes we need to do some <strong>more complicated stuff</strong>, or we need a <strong>faster tool</strong>, or to <strong>load more data than 100k rows</strong>. In this case, APOC comes to the rescue.</p><p>First, you have to <strong>install it as a plugin</strong>.</p><p>For doing it, come back to Neo4j Desktop and click on your DBMS (in my case “crime_inv_red”) and select plugins, and after that install APOC and restart your DBMS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OJaO-FxKCZ3Kd9HoVPXfwg.png" /><figcaption><strong>How to install the APOC library</strong></figcaption></figure><p>Now, you have to click on the 3 dots that you see on the right, click on “open folder” and “Configuration”. In the folder opened create an “apoc.conf” file with “apoc.import.file.enabled=true” inside. This is for enabling the local file import for APOC. Now restart your DBMS.</p><p>You’re finally ready to use this library!</p><p>If you have created the nodes in the other way you can try the APOC load deleting the previous ones using:</p><pre>MATCH (p)<br>DETACH DELETE p</pre><p>Now, use an APOC procedure to<strong> load the nodes</strong>:</p><pre>CALL apoc.periodic.iterate(</pre><pre>&quot;CALL apoc.load.csv(&#39;nodes_people_reduced.csv&#39;)</pre><pre>YIELD map as row</pre><pre>where row._id IS NOT NULL</pre><pre>RETURN row&quot;,</pre><pre>&quot;MERGE (p:Person {id: row._id})</pre><pre>ON CREATE SET</pre><pre>p.name = row.name,</pre><pre>p.surname = row.surname&quot;,</pre><pre>{batchSize:100}</pre><pre>)</pre><p>In this case, we have an apoc procedure to load the CSV (<strong>apoc.load.csv</strong>) and an APOC procedure for the batch commit (<strong>apoc.periodic.iterate</strong>).</p><p><strong>For the relationships</strong> we can use:</p><pre>CALL apoc.periodic.iterate(</pre><pre>&quot;CALL apoc.load.csv(&#39;relationship_people_reduced.csv&#39;)</pre><pre>YIELD map as row&quot;,</pre><pre>&quot;MATCH (starting_node:Person {id:row._start})</pre><pre>MATCH (ending_node:Person {id:row._end})</pre><pre>CALL apoc.create.relationship(starting_node, row._type,{}, ending_node)</pre><pre>YIELD rel</pre><pre>RETURN COUNT(*)&quot;,</pre><pre>{batchSize:100}</pre><pre>)</pre><p>Let’s note that we need to use <strong>apoc.create.relationship</strong> to introduce a parametric label.</p><p>So we have all the nodes and relationships in our graph. Let’s try to see what we have done!</p><h3><strong>5. QUERY THE GRAPH</strong></h3><p>In this last part, we want to see if all the previous procedures went right.</p><p>Let’s begin with a schema visualization of the graph:</p><pre>CALL db.schema.visualization()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*fPrkda1yTWZyZzrhyZ_pTw.png" /><figcaption><strong>Graph schema</strong></figcaption></figure><p>In this case, we see that we only have one type of node and five types of relationships, as expected.</p><p>And if we want to inspect a part of the graph we can use:</p><pre>MATCH (n)-[rel]-&gt;(n2)</pre><pre>RETURN n, rel, n2</pre><pre>LIMIT 10</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*4Ml8vDFTUSqNVyvEpe8Jiw.png" /><figcaption><strong>Subgraph</strong></figcaption></figure><h3><strong>CONCLUSIONS</strong></h3><p>In this article, we have seen how to create a graph database starting from zero using CSV files and loading with LOAD CSV and the APOC library.</p><p>If you want more details on that see the <a href="https://neo4j.com/developer/desktop-csv-import/">documentation</a>.</p><p>Sometimes we need faster computation and we have to use the Neo4j-admin import. In <a href="https://towardsdatascience.com/importing-csv-files-in-neo4j-f3553f1a76cf">this article</a>, we can see how to use it.</p><p>If you are interested in loading data online from an RDBMS you can use a <a href="https://neo4j.com/docs/api/python-driver/current/">Python driver</a> or the <a href="https://neo4j.com/labs/etl-tool/">Neo4j ETL tool</a>.</p><p>Now we have a complete overview of how to load the data, so the next step in other articles will be how to query and analyze a graph!</p><p>So, see you soon guys!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd6d82a47b95" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-reply-it-datatech/graph-database-in-neo4j-from-zero-to-hero-dd6d82a47b95">Graph Database in Neo4j: from zero to hero!</a> was originally published in <a href="https://medium.com/data-reply-it-datatech">Data Reply IT | DataTech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>