I have a relations csv file. I have to load this relations to neo4j but i can't.I have already created my News node and this nodes have unique id.
The csv file has iStart, iRel, iEnd columns. Thanks
Sample .csv file looks like this:
iStart iRel iEnd
------------------------
114471 IS 2225
and this is my code:
LOAD CSV WITH HEADERS FROM "file:///try.csv" as input
MATCH (from:News {id: input.iStart}), (to:News {id: input.iEnd})
CREATE (from)-[:RELATION { type: input.iRel }]->(to);
Try this one:
LOAD CSV WITH HEADERS FROM 'file:///try.csv' as input
CREATE (fromnews:FromNews{id: input.iStart}),(tonews:ToNews{id: input.iEnd})
WITH fromnews,tonews
CREATE (fromnews)-[:RELATION{type: input.iRel}]->(tonews );
Related
I have a csv file and I want to make 2 nodes with relation (node country-reported_on->node report_date). I have tried this code but it returns empty nodes with numbers instead of country name.
Here is what my dataset looks like:
PEOPLE_POSITIVE_CASES_COUNT;REPORT_DATE;COUNTRY_SHORT_NAME;PEOPLE_DEATH_COUNT;LIFE_EXPECTANCY;GDP;DENSITY_POPULATION;WORKFORCE
0;22.01.2020;Lesotho;0;54.836;875.353432963926;70.5616600790514
134;09.07.2020;Lesotho;1;54.836;875.353432963926;70.5616600790514
79557;02.03.2021;Zambia;1104;64.194;985.132436038869;94.4781600309238
106470;02.03.2021;Kenya;1863;66.991;1878.58070251348;94.4781600309238
Here is the code that I used:
LOAD CSV WITH HEADERS FROM "file:///dataset.csv"
as row WITH row WHERE row.COUNTRY_SHORT_NAME IS NOT NULL
MERGE (c:Country {name: row.COUNTRY_SHORT_NAME,
life_exp: row.LIFE_EXPECTANCY,
gdp: row.GDP,
density_population: row.DENSITY_POPULATION,
worforce: row.WORKFORCE } )
MERGE ( d:Report_date { date: row.REPORT_DATE } )
MERGE (c)-[:reported_on {cases_count: row.PEOPLE_POSITIVE_CASES_COUNT,
death_count: row.PEOPLE_DEATH_COUNT}]->(d)
EDIT
I changed the delimiter to ';' because that is what we had in our dataset however we still get bad results here is how it looks like in neo4j after running this code:
LOAD CSV WITH HEADERS FROM "file:///dataset.csv"
as row FIELDTERMINATOR ';' WITH row WHERE row.COUNTRY_SHORT_NAME IS NOT NULL
MERGE (c:Country {name: row.COUNTRY_SHORT_NAME,
life_exp: row.LIFE_EXPECTANCY,
gdp: row.GDP,
density_population: row.DENSITY_POPULATION,
worforce: row.WORKFORCE } )
MERGE ( d:Report_date { date: row.REPORT_DATE } )
MERGE (c)-[:reported_on {cases_count: row.PEOPLE_POSITIVE_CASES_COUNT,
death_count: row.PEOPLE_DEATH_COUNT}]->(d)
I think you got confused with node caption in Neo4j browser, all nodes get assigned an node id by default and nodes must be showing that. You can change it to country name property by clicking on node label. Screen shot for reference.
Trying to import rows and create nodes from different .csv files in one cypher query:
// User nodes
LOAD CSV WITH HEADERS
FROM 'file:///profile.csv' AS profile_line
CREATE (user:User { userId: profile_line.User })
// Project nodes
LOAD CSV WITH HEADERS
FROM 'file:///project.csv' AS project_line
CREATE (project:Project { projectId: project_line.projectId })
// Image nodes
LOAD CSV WITH HEADERS
FROM 'file:///media.csv' AS image_line
CREATE (image:Image { imageId: '<imageId>' })
Throws the following error:
"WITH is required between CREATE and LOAD CSV (line 9, column 1 (offset: 211))
"CREATE (project:Project { projectId: project_line.projectId })"
I am unclear as to how the WITH statement should be constructed.
If you're using the Neo4j Browser, the easiest way to do this is to just separate your statements with semicolons and turn on the 'multi-statement query editor' (which also gives you a nice little progress indicator as each statement is run):
LOAD CSV WITH HEADERS
FROM 'file:///profile.csv' as profile_line
CREATE (user: User { userId: profile_line.User });
LOAD CSV WITH HEADERS
FROM 'file:///project.csv' as project_line
CREATE (project: Project { projectId: project_line.projectId });
LOAD CSV WITH HEADERS
FROM 'file:///media.csv' as image_line
CREATE (image: Image { imageId: image_line.ImageId });
Otherwise, it is still possible to do this. What we want is a WITH statement that will return a single row irrespective of how many nodes the previous CREATE ended up creating, so any aggregation function will do. For example:
LOAD CSV WITH HEADERS
FROM 'file:///profile.csv' as profile_line
CREATE (user: User { userId: profile_line.User })
WITH max(1) as dummy
LOAD CSV WITH HEADERS
FROM 'file:///project.csv' as project_line
CREATE (project: Project { projectId: project_line.projectId })
WITH max(1) as dummy
LOAD CSV WITH HEADERS
FROM 'file:///media.csv' as image_line
CREATE (image: Image { imageId: image_line.ImageId })
Added 9 labels, created 9 nodes, set 9 properties, completed after 17 ms.
Though I think this is super unclear for future-you, and I wouldn't recommend it.
This example is taken from https://neo4j.com/developer/guide-importing-data-and-etl/#_importing_the_data_using_cypher"
LOAD CSV WITH HEADERS FROM "file:customers.csv" AS row
CREATE (:Customer {companyName: row.CompanyName, customerID: row.CustomerID, fax: row.Fax, phone: row.Phone});
What I want to do is use a field in the CSV file to define the label in the node. For example:
LOAD CSV WITH HEADERS FROM "FILE:///Neo4j_AttributeProvenance.csv" AS CSVLine CREATE (q:CSVLine.NodeType { NodeID:CSVLine.NodeID, SchemaName:CSVLine.SchemaName, TableName:CSVLine.TableName, DataType:CSVLine.DataType, PreviousNodeID:CSVLine.PreviousNodeID });
You should have a look at the APOC procedures. In this case there's a procedure able to create nodes dinamically based on column values in your .csv file. The syntax is:
CALL apoc.create.node(['Label'], {key:value,…})
In your case the simplest syntax should be:
CALL apoc.create.node(["' + CSVLine.NodeType + '"], {NodeID: "' + NodeID:CSVLine.NodeID + '", etc}) yield node
This is an example of my csv file:
_id,official_name,common_name,country,started_by,
ABO.00,Association Football Club Bournemouth,Bournemouth,England,"{""day"":NumberInt(1),""month"":NumberInt(1),""year"":NumberInt(1899)}"
AOK.00,PAE Kerkyra,Kerkyra,Greece,"{""day"":NumberInt(30),""month"":NumberInt(11),""year"":NumberInt(1968)}"
I have to import this csv into Neo4j:
LOAD CSV WITH HEADERS FROM
'file:///Z:/path/to/file/team.csv' as line
create (p:Team {_id:line._id, official_name:line.official_name, common_name:line.common_name, country:line.country, started_by_day:line.started_by.day,started_by_month:line.started_by.month,started_by_year:line.started_by.year
I get an error(Neo.ClientError.Statement.InvalidType) setting started_by.day, started_by.month, started_by.year
How can I set rightly the properties about started_by?
Format of you csv should be following:
_id,official_name,common_name,country,started_by_day,started_by_month,started_by_year
ABO.00,Association Football Club Bournemouth,Bournemouth,England,1,1,1899
Cypher:
LOAD CSV WITH HEADERS FROM 'file:///Z:/path/to/file/team.csv' as line
CREATE (p:Team {_id:line._id, official_name:line.official_name, common_name:line.common_name, country:line.country, started_by_day:line.started_by_day,started_by_month:line.started_by_month,started_by_year:line.started_by_year})
It looks like your date part in the csv file is in JSON format - don't you need to parse that first?
line.started_by
is this string
"{""day"":NumberInt(30),""month"":NumberInt(11),""year"":NumberInt(1968)}"
There is no line.started_by.day
I will try to be very succinct with my problem. I have the node Person that I loaded using a .csv file and I have another .csv file to be loaded - person_speaks_language_0.csv
(got this header: idPerson|languagePSL )
How can I relate this? How can I create this relationship?
Grabbing another example, that is very similar to the previous one, and that I can't solve. I have the Comment node loaded in Neo4j an I need to load another .csv file, that file is - comment_replyOf_comment_0.csv
(got his header: idComment|idComment)
How can I load this file? How can I connect a relation that goes "in and out" from the same node - that connects the same node?
For the first example. there is 2 options.
If you want Language to be a separate node, try this cypher:
LOAD CSV FROM 'person_speaks_language_0.csv' AS line
MATCH (p:Person)
WHERE p.id=line[0]
MERGE (p)-[r:Speaks]->(l:Language { name: line[1])})
RETURN p, l, r
Or, probably, better option
LOAD CSV FROM 'person_speaks_language_0.csv' AS line
MERGE (p:Person { id:line[0] })-[r:Speaks]->(l:Language { name: line[1]) })
RETURN p, l, r
If you want Language to be a property, try this:
LOAD CSV FROM 'person_speaks_language_0.csv' AS line
MERGE (p { id:line[0], language:line[1] })
RETURN p
The RETURN statement is optional and you don't want to include it for a big csv files (although it could be useful for debug).
For the second example, try this:
LOAD CSV FROM 'comment_replyOf_comment_0.csv' AS line
MERGE (c1:Comment { id:line[0] })-[r:Commented]->(c2:Comment { id:line[1]) })
RETURN c1, r, c2