简体   繁体   中英

Importing csv file into neo4j

I am beggining with neo4j and i have a problem for importing some data from a csv file.

My code is the following :

CREATE CONSTRAINT ON (w:word) ASSERT w.stem IS UNIQUE

USING PERIODIC COMMIT 1000
     LOAD CSV WITH HEADERS FROM "file:/home/quentin/.pfe/short_abstracts_fr0.csv" AS row
     MATCH (a:article {title: row.article})
     MATCH (w:word {stem: row.word})
     MERGE (a)-[r:contains {count:row.count}]->(w)

This is not creating any error, but it doesn't import anything. My csv file location is the good one, and it's content is the following :

article,word,count
Aux_couleurs_du_Moyen_Âge,accompagné,1
Aux_couleurs_du_Moyen_Âge,auss,1
Aux_couleurs_du_Moyen_Âge,banquet,1

I have already imported 2 similar csv files using : :

   USING PERIODIC COMMIT 1000
     LOAD CSV WITH HEADERS FROM "file:/home/quentin/.pfe/category_relations_csv" AS row
     MATCH (a:article {title: row.article})
     MATCH (cat:category {label: row.categorie})
     MERGE (a)-[:is_under]->(cat)

and

    MATCH (c0:category {label: row.cat0})
     MATCH (c1:category {label: row.cat1})
     MERGE (c0)-[r:relates {type:row.link}]->(c1)"

If someone has any idea what is wrong with it.

Have you confirmed that it is matching a and w ? If one or both of a and w is not being found then your MERGE statement will not create anything. Try outputting the results, rather than attempting the MERGE . eg

LOAD CSV WITH HEADERS FROM "file:/home/quentin/.pfe/short_abstracts_fr0.csv" AS row
MATCH (a:article {title: row.article}), (w:word {stem: row.word})  
RETURN a.article, w.word 

If you know that they do not already exist then you can omit the MATCH clauses.

eg

CREATE CONSTRAINT ON (w:word) ASSERT w.stem IS UNIQUE

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:/home/quentin/.pfe/short_abstracts_fr0.csv" AS row
MERGE (a:article {title: row.article})-[r:contains {count:row.count}]->(w:word {stem: row.word})

Likewise, if you expect that only one end of the pattern will already exist then include only one MATCH clause. From your query and addition of a constraint, it looks like you may be expecting the creation of the node with the :Word label. If so your query could look like this.

CREATE CONSTRAINT ON (w:word) ASSERT w.stem IS UNIQUE

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:/home/quentin/.pfe/short_abstracts_fr0.csv" AS row
MATCH (a:article {title: row.article})
MERGE (a)-[r:contains {count:row.count}]->(w:word {stem: row.word})

However, it may be better to create the nodes first and then create the relationship pattern. Hope this helps!

我认为我在安装Neo4j的过程中遇到问题,因为在卸载并重新安装它之后,导入终于可以了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM