Neo4j Interview4
Neo4j Interview4
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
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.
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
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:
##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.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 −
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.
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.
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.
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.
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.
Syntax
Following is the syntax of OnCreate and OnMatch clauses.
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.
#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).
In the same way, you can merge multiple relationships and undirected relationships
too.
6.1)As we have seen earlier, graph databases are useful to find paths between two
nodes:
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:
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:
neo4j administration