0% found this document useful (0 votes)
40 views5 pages

Neo4j Interview4

The document discusses how to import data into Neo4j from CSV files by using Cypher LOAD CSV commands. It explains how to create nodes from CSV data, create relationships between nodes, and use MERGE to insert or match nodes and relationships. It also covers Neo4j configuration files and settings.

Uploaded by

mnpaliwaldse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views5 pages

Neo4j Interview4

The document discusses how to import data into Neo4j from CSV files by using Cypher LOAD CSV commands. It explains how to create nodes from CSV data, create relationships between nodes, and use MERGE to insert or match nodes and relationships. It also covers Neo4j configuration files and settings.

Uploaded by

mnpaliwaldse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#Importing data to Neo4j

1)Cypher can be used to create graphs or include data in your existing graphs from
common data formats such as CSV. Cypher uses the LOAD CSV command to parse
CSV data into the form that can be incorporated in a Neo4j graph

2)We have three CSV files:

one contains players,


the second has a list of games, and
the third has a list of which of these players played in each game.

You can access the CSV files by keeping them on the Neo4j server and using file://,
or by using FTP, HTTP, or HTTPS for remote access to the data.

3.1)Let's consider sample data about cricketers (players) and the matches (games)
that were played by them.

Your CSV file would look like this:


id,name
1,Adam Gilchrist
2,Sachin Tendulkar
3,Jonty Rhodes
4,Don Bradman
5,Brian Lara

3.2)You can now load the CSV data into Neo4j and create nodes out of them using the
following commands, where the headers are treated as the labels of the nodes and
the data from every line is treated as nodes

LOAD CSV WITH HEADERS FROM "http://192.168.0.1/data/players.csv" AS


LineOfCsv
CREATE (p:Person { id: toInt(LineOfCsv.id), name: LineOfCsv.name })

4.1)Now, let's load the games.csv file. The format of the game data will be in the
following format where each line would have the ID, the name of the game, the
country it was played in, and the year of the game:

id,game,nation,year
1,Ashes,Australia,1987
2,Asia Cup,India,1999
3,World Cup,London,2000

4.2)The query to import the data would now also have the code to create a country
node and relate the game with that country:

LOAD CSV WITH HEADERS FROM " http://192.168.0.1/data/games.csv" AS


LineOfCsv
MERGE (nation:Nation { name: LineOfCsv.nation })
CREATE (game:Game { id: toInt(LineOfCsv.id), game: LineOfCsv.game,
year:toInt(LineOfCsv.year)})
CREATE (game)-[:PLAYED_IN]->(nation)

##Creation of Node:nation with Label:Nation and properties name using Merge which
is combination of MATCH and CREATE.
##If node with below label and properties doesnt exists create it.
MERGE (nation:Nation { name: LineOfCsv.nation })

##Creation of Node: game with Label:Game and Properties ie Key value Pairs
id,game,year
CREATE (game:Game { id: toInt(LineOfCsv.id), game: LineOfCsv.game,
year:toInt(LineOfCsv.year)})

CREATE (game)-[:PLAYED_IN]->(nation)

5)Merge Neo4j

https://www.tutorialspoint.com/neo4j/neo4j_merge_command.htm

5.1)MERGE command is a combination of CREATE command and MATCH command.

5.2)Neo4j CQL MERGE command searches for a given "pattern in the graph". If it
exists, then it returns the results.
If it does NOT exist in the graph, then it creates a new node/relationship and
returns the results.

5.3)
In this chapter you are going to learn how to −

Merge a node with label


Merge a node with properties
OnCreate and OnMatch
Merge a relationship

5.4)
Syntax
Following is the syntax for the MERGE command.
MERGE (node: label {properties . . . . . . . })

Before proceeding to the examples in this section, create two nodes in the database
with labels Dhawan and Ind. Create a relationship of type “BATSMAN_OF” from Dhawan
to Ind as shown below.

CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})


CREATE (Ind:Country {name: "India"})
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)

#a)Merge a node with label

You can merge a node in the database based on the label using the MERGE clause. If
you try to merge a node based on the label, then Neo4j verifies whether there
exists any node with the given label. If not, the current node will be created.

Syntax
Following is the syntax to merge a node based on a label.
MERGE (node:label) RETURN node

Example 1
Following is a sample Cypher Query which merges a node into Neo4j (based on label).
When you execute this query, Neo4j verifies whether there is any node with the
label player. If not, it creates a node named “Jadeja” and returns it.
If, there exists any node with the given label, Neo4j returns them all.

MERGE (Jadeja:player) RETURN Jadeja

On executing, you will get the following result. Since you have already created a
node named “Jadeja” with the label “player” in the database, Neo4j returns it as
shown in the following screenshot.

Example 2
Now, try to merge a node named “CT2013” with a label named Tournament. Since there
are no nodes with this label, Neo4j creates a node with the given name and returns
it.

MERGE (CT2013:Tournament{name: "ICC Champions Trophy 2013"})


RETURN CT2013, labels(CT2013)

#b)Merge a node with properties

You can also merge a node with a set of properties. If you do so, Neo4j searches
for an equal match for the specified node, including the properties. If it doesn’t
find any, it creates one.

Syntax
Following is the syntax to merge a node using properties.

MERGE (node:label {key1:value, key2:value, key3:value . . . . . . . . })

Example
Following is a sample Cypher Query to merge a node using properties. This query
tries to merge the node named “jadeja” using properties and label. Since there is
no such node with the exact label and properties, Neo4j creates one.

MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})


RETURN Jadeja

#c)OnCreate and OnMatch

Whenever, we execute a merge query, a node is either matched or created. Using on


create and on match, you can set properties for indicating whether the node is
created or matched.

Syntax
Following is the syntax of OnCreate and OnMatch clauses.

MERGE (node:label {properties . . . . . . . . . . .})


ON CREATE SET property.isCreated ="true"
ON MATCH SET property.isFound ="true"

Following is a sample Cypher Query which demonstrates the usage of OnCreate and
OnMatch clauses in Neo4j. If the specified node already exists in the database,
then the node will be matched and the property with key-value pair isFound = "true"
will be created in the node.

If the specified node doesn’t exist in the database, then the node will be created,
and within it a property with a key-value pair isCreated ="true" will be created.

MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})


ON CREATE SET Jadeja.isCreated = "true"
ON MATCH SET Jadeja.isFound = "true"
RETURN Jadeja

#d)Merge a relationship

Just like nodes, you can also merge the relationships using the MERGE clause.

Example
Following is a sample Cypher Query which merges a relationship using the MATCH
clause in Neo4j. This query tries to merge a relationship named WINNERS_OF between
the nodes “ind” (label: Country & name: India) and ICC13 (label: Tournament & name:
ICC Champions Trophy 2013).

Since such relation doesn’t exist, Neo4j creates one.

MATCH (a:Country), (b:Tournament)


WHERE a.name = "India" AND b.name = "ICC Champions Trophy 2013"
MERGE (a)-[r:WINNERS_OF]->(b)
RETURN a, b

In the same way, you can merge multiple relationships and undirected relationships
too.

6)Working with paths

6.1)As we have seen earlier, graph databases are useful to find paths between two
nodes:

MATCH path = (a{surname:'Davies'}) -[*]- (b{surname:'Taylor'})


RETURN path

6.2)
This query uses a construct which we have not used so far—the path assignment, path
=.

The assignment of variables can be done only with paths. Note that the query in the
preceding code returns all the possible paths from two nodes.

Here,
the result is two paths in our database:
[Node[2]{name:"Nathan",surname:"Davies"},:BELONGS_TO[0]
{from:1297292400000},Node[0]{code:"CC1"},:MANAGER_OF[2]
{from:1268002800000},Node[3]{name:"Rose",surname:"Taylor"}] |
[Node[2]{name:"Nathan",surname:"Davies"},:REPORTS_TO[1]{},Node[3]
{name:"Rose",surname:"Taylor"}]

6.3)However, what if we need the shortest path between them? The shortest path is
the path with the least number of nodes visited. Clearly, we could iterate over all
the paths and take the shortest, but Cypher provides a function that does the work
for us:

MATCH (a{surname:'Davies'}), (b{surname:'Taylor'})


RETURN allShortestPaths((a)-[*]-(b)) as path

1)The primary configuration file, Neo4j, is located in the conf/neo4j-


server.properties directory.
For normal development purposes, the default settings are sufficient. However, as
an administrator, you can make suitable
changes to the settings.

2)
You can set the base directory on the disk where your database resides using the
following property:
org.neo4j.server.database.location=/path/to/database/graph.db

3)
The default port on which Neo4j operates is 7474. However, you can change the port
for accessing the data, UI and administrative use, using the following setting:
org.neo4j.server.webserver.port=9098

4)JVM configurations

Neo4j is written in Java and hence, the settings for JVM also decide the resource
constraints that are imposed upon the database.
You can however, configure these properties in the conf/neo4j-wrapper.conf file in
NEO4J_HOME in your installation.
Here are a few common properties that you can tweak according to your requirements:

#Name of property: What it stands for

wrapper.java.initmemory: Initial size of heap (in MB)


wrapper.java.maxmemory: Maximum size of heap (in MB)
wrapper.java.additional.N: Additional literal parameter of the JVM (N is the number
of each literal)

neo4j administration

Sharding Graph Data with Neo4j Fabric

You might also like