Leave a comment

APOC to the rescue #2


In this short post, another APOC feature worth its weight.

The function apoc.any.property takes two parameters, first being the object (node, relation) the second being the name of a property of that object.

It returns the value of any property of an object.   <– genius 🙂

That is super useful when you want to use several columns from a CSV* that have alike names, like pos(swa) pos (fra) pos (eng).

Here is an example to return wanted columns from the line object

WITH ['eng', 'swa', 'fra'] AS lngs
LOAD CSV WITH HEADERS FROM "file:///my_12_langs.csv.file" AS line FIELDTERMINATOR ';'
UNWIND lngs AS lng
WITH 'pos ('+ lng + ')' AS colName, line
RETURN apoc.any.property(line, colName) LIMIT 10

 

 

Here is something a little closer to the real Cypher I use. I create temp nodes. Because then I check for the presence of properties in conditions.

WITH ['eng', 'swa', 'fra'] AS lngs   # just 3 here, has 12
LOAD CSV WITH HEADERS FROM "file:///my_12_langs.csv.file" AS line FIELDTERMINATOR ';'
CREATE (t:TEMP)
SET t += line
WITH t,lngs
UNWIND lngs AS lng
WITH 'pos ('+ lng + ')' AS colName,t
RETURN colName, apoc.any.property(t, colName) LIMIT 50

MATCH (t:TEMP)
DELETE t   # clean up

 

Hope this is useful for you.

Big up to the APOC maintainers.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: