Reputation: 247
I'm just importing some stuff from .csv file to Neo4j. I've always used MERGE
to create a node, but now, when trying to import from .csv, some of data is null, e.g. column address
. When I'm doing MERGE
instead of CREATE
it gives an error, but when I do CREATE
it works fine. The only difference I know between MERGE
and CREATE
is that if the node already exists, MERGE
doesn't make a new one.
My query:
LOAD CSV WITH HEADERS FROM '<path>' as line
CREATE (a: Address
{
address: line.address,
postalCode: toInteger(line.postalCode),
town: line.town,
municipalityNr: toInteger(line.municipalityNr),
municipality: line.municipality,
countryCode: line.countryCode,
country: line.country
})
RETURN a.address
Upvotes: 3
Views: 538
Reputation: 793
Agree with Graphileon.
There is a trap you should avoid. You also want to avoid properties that may vary within a data set. For instance the same person has a birth date in different formats (e.,g, 5/16/85, 05/16/1985), you would add duplicate nodes if birth date was part of the merge criteria. To avoid this, you can do the merge without the date and then, in a subsequent step, add the property ... of course, only the last such effort would endure.
You can check to see that there are no duplicate nodes. In the example given, if your sure the name is unique, or license number of the car:
match (p:Person) return p.name, count(*) as ct order by ct desc
Upvotes: 0
Reputation: 5385
When doing a MERGE
, Neo4j expects a value that it can merge on. MERGE
using a null will always result in an error.
In general, this is the approach:
Only MERGE
on properties that are relevant to finding a unique node. So for instance, if you want to MERGE
cars, the licence plate would be the property to use.
Make sure you have a CONSTRAINT
for the property you MERGE
on. This will help speed up the import.
To avoid nulls in the MERGE
, you can use COALESCE()
.
After the MERGE
, you can SET
the other properties, which may have nulls.
MERGE {c:Car {licensePlate: COALESCE(line.licensePlate,'Unknown') })
SET c.color = line.color,
c.someproperty = line.someproperty
At the end of the run, you will find a single :Car
node with licensePlate:'Unknown'
Upvotes: 2