Search code examples
memgraphdb

How can I load CSV data into Memgraph Lab?


I have a CSV file that represents relations between animals based on their taxonomy.

Columns are:

  • Animal_1 (Latin name of the first animal)
  • Animal_2 (Latin name of the second animal)
  • Relation (Type of relation: e.g., "is related to", "is a subclass of", "is a predator of")
  • Habitat (Where they are commonly found)
  • Diet (What they typically eat)

My CSV file looks like this:

Animal_1,Animal_2,Relation,Habitat,Diet
Gallus gallus,Bos taurus,is a predator of,Mountainous,Omnivore
Cervus elaphus,Felis catus,is related to,Jungle,Omnivore
Panthera leo,Panthera leo,is a predator of,Forest,Herbivore
Cervus elaphus,Panthera leo,is a predator of,Savannah,Herbivore
Equus caballus,Equus caballus,is a predator of,Mountainous,Herbivore
Cervus elaphus,Bos taurus,is related to,Forest,Omnivore
Equus caballus,Felis catus,is a predator of,Grassland,Omnivore
Gallus gallus,Felis catus,is related to,Forest,Carnivore
Panthera leo,Felis catus,is a subclass of,Jungle,Carnivore
Felis catus,Canis lupus,is a predator of,Jungle,Carnivore
Equus caballus,Canis lupus,is related to,Mountainous,Herbivore
Canis lupus,Sus scrofa,is a predator of,Grassland,Herbivore
Bos taurus,Sus scrofa,is a predator of,Grassland,Herbivore
Panthera tigris,Bos taurus,is a predator of,Jungle,Omnivore
Bos taurus,Cervus elaphus,is related to,Mountainous,Carnivore
Equus caballus,Panthera leo,is related to,Domestic,Herbivore
Canis lupus,Bos taurus,is a predator of,Savannah,Carnivore
Sus scrofa,Equus caballus,is related to,Forest,Carnivore
Bos taurus,Bos taurus,is related to,Jungle,Herbivore
Panthera tigris,Felis catus,is a subclass of,Mountainous,Omnivore

I've downloaded Memgraph Lab 2.8.3. And I've tried to run the query:

    LOAD CSV FROM '/path-to-your-file.csv' WITH HEADER AS row
    CREATE (a:Animal {name: row.Animal_1, habitat: row.Habitat, diet: row.Diet}),
           (b:Animal {name: row.Animal_2, habitat: row.Habitat, diet: row.Diet})
    CREATE (a)-[:RELATION {type: row.Relation}]->(b);

and

    LOAD CSV FROM 'c:\temp\animal_relations.csv' WITH HEADER AS row
    CREATE (a:Animal {name: row.Animal_1, habitat: row.Habitat, diet: row.Diet}),
            (b:Animal {name: row.Animal_2, habitat: row.Habitat, diet: row.Diet})
    CREATE (a)-[:RELATION {type: row.Relation}]->(b)

But all that I get are error that file is not there

Error 1

Error 2


Solution

  • It looks that your path is broken. Your prompt reveals that you are using Windows. I presume that you are using Docker installation of Memgraph. You can either copy the file to your Docker container and then reference it in LOAD CSV or you can use remote server.

    You can import CSV data from a remote server into Memgraph. The LOAD CSV clause in Memgraph allows you to load data from a CSV file located either on your local file system or over the network. If you are using http://, https://, or ftp://, the CSV file will be fetched over the network.

    Here is the syntax for the LOAD CSV clause:

    LOAD CSV FROM <csv-location> ( WITH | NO ) HEADER [IGNORE BAD] [DELIMITER <delimiter-string>] [QUOTE <quote-string>] [NULLIF <nullif-string>] AS <variable-name>
    

    In this syntax, <csv-location> is a string of the location to the CSV file. If you are using a URL protocol, it refers to a remote file location.