简体   繁体   中英

Hierarchical Graph in Neo4J from csv

I have a csv as follows:

City State  Country
A     WB      BAN
B     WB      BAN
C     MA      BAN
D     MA      BAN
E     RG      BAN

Another file has citywise sales. I have to compute sales for city,region,state interchangeably in python depending on user request. Can I represent these in Neo4j and access sales from a single function in python which automatically takes city, state or region as required?

The city sales data is as follows:

City  Period   Sales_Amt($)
A      May17    $5
B      May17    $10
C      May17    $15
D      May17    $5

The load of the data (using LOAD CSV for example) could look like this :

LOAD CSV WITH HEADERS FROM 'file:///hierarchy.csv' as line fieldterminator ','
MERGE (c:Country {id: line.countryId})
MERGE (s:State {id: line.stateId})
MERGE (ci:City {id: line.cityId})
MERGE (c)-[:HAS_STATE]->(s)
MERGE (s)-[:HAS_CITY]->(ci);

The load of the sales data could look like this :

LOAD CSV WITH HEADERS FROM 'file:///sales.csv' as line fieldterminator ','
MATCH (ci:City {id: line.cityId})
CREATE (s:Sale {period: line.period, amount: toFloat(line.amount)})
MERGE(ci)-[:HAS_SALE]->(s);

Now, you will have to build-up the query depending on what the entrypoint (country, state, city) is :

MATCH (c:Country {id: "theinputforcountry})-[:HAS_STATE]->(s:State)-[:HAS_CITY]->(ci:City)-[:HAS_SALE]->(s:Sale)
RETURN c.id, sum(s.amount);

or

MATCH (s:State {id: "theinputforstate"})-[:HAS_CITY]->(ci:City)-[:HAS_SALE]->(s:Sale)
RETURN s.id, sum(s.amount);

or

MATCH (ci:City {id: "theinputforcity"})-[:HAS_SALE]->(s:Sale)
RETURN ci.id, sum(s.amount);

Hope this helps !

Regards, Tom

PS It is possible to walk a variable length path (so you don't have to specify the whole path) and it is also possible to turn this into a more general query that would just differ on the WHERE and RETURN clause (the WHERE would have the key you get as input, the RETURN would then group on the level that you got as input) ... but both approaches make it less clear what you are trying to accomplish (imo :-).

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