Luis Trigueiros
Luis Trigueiros

Reputation: 666

How to conditional MERGE only if the column has value when using LOAD CSV to load data

I am using LOAD CSV to import nodes and relations, but one of the columns the "country_id" that I use to create the relation with another not is NULL at times in the input data set, how does one do conditional MERGE only if the column has value. I tried the "CASE" but it does not work with "IS NOT NULL". Thank you. Luis Oscar
The following is my code at the moment discarting NULL's for country_id:

USING PERIODIC COMMIT 20
LOAD CSV WITH HEADERS FROM 'file:///physical_location.csv' as row
WITH
toInt(row.physical_location_id)    as physical_location_id,
CASE row.country_id WHEN IS NOT NULL THEN MERGE (c:Country {code:country_id})  END, 
toFloat(row.latitude)       as latitude,
toFloat(row.longitude)      as longitude,
row.addressline1            as addressline1,
row.addressline2            as addressline2,
row.cityname                as cityname,
row.postalcode              as postalcode,
row.telephone               as telephone,
row.airport                 as airport,
row.railway                 as railway,
row.ferryport               as ferryport,
row.iata_airport_code       as iata_airport_code,
row.iata_city_code          as iata_city_code,
row.timezone                as timezone,
row.pickup_only             as pickup_only,
row.return_only             as return_only,
row.outofhours_return       as outofhours_return,
row.dropbox                 as dropbox,
row.delivery_collection     as delivery_collection,
row.pl_email                as pl_email
//where country_id IS NOT NULL
MERGE (pl:PhysicalLocation {physical_location_id:physical_location_id})
 ON MATCH SET
    pl.addressline1          = addressline1,
    pl.addressline2          = addressline2,
    pl.cityname              = cityname,
    pl.postalcode            = postalcode,
    pl.telephone             = telephone,
    pl.airport               = airport,
    pl.railway               = railway,
    pl.ferryport             = ferryport,
    pl.iata_airport_code     = iata_airport_code,
    pl.iata_city_code        = iata_city_code,
    pl.timezone              = timezone,
    pl.pickup_only           = pickup_only,
    pl.return_only           = return_only,
    pl.outofhours_return     = outofhours_return,
    pl.dropbox               = dropbox,
    pl.delivery_collection   = delivery_collection,
    pl.pl_email              = pl_email
//MERGE (pl)-[r:IN_COUNTRY]->(c)
RETURN count(pl)

Upvotes: 2

Views: 136

Answers (1)

logisima
logisima

Reputation: 7458

You can use cypher coalesce function to have a default value if your country_id is null : MERGE (c:Country {code:coalesce(row.country_id, 'NULL')})

After, when your LOAD CSV is done, you can make a cleanup query to remove your default country with : DETACH DELETE (c:Country code:'NULL'});

Upvotes: 3

Related Questions