cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Range of Relationships

I have created 24 nodes which represents different hours of the day and also all other nodes required for my use case.

I want to import data with time values and create a relationship from node (s1) to time (0am). The time values certainly differ, but I want that all time within a range be mapped to a specific hour let’s say between 0:00:00 to 0:59:59 be mapped to node labelled 0 hour as shown in the image displayed below.

My difficulty is creating that range of relationships.

Thanks in advance.

2X_6_666833c1352b467b58ba5ed446cb5bd38e4dd100.png

Dataset
2X_9_9d5a0befe5a114109e248c61a696659483d44162.png

5 REPLIES 5

ameyasoft
Graph Maven

Here is a solution:

I created a sample .csv file with four rows with hours 0, 1, 2, and 3. In the sample data I took only the first two columns from your post.

S1,Date
com.sina.weibo,2014-04-30T0:00:37
com.sina.weibo,2014-04-30T1:00:37
com.sina.weibo,2014-04-30T2:00:37
com.sina.weibo,2014-04-30T3:00:37

<

LOAD CSV WITH HEADERS FROM 'file:/bayitaa.csv' AS line

WITH line, split(line.Date, "T") as tn

WITH line, tn, split(tn[1], ':') as tn2

MERGE (dt:Dte {dte: tn[0]})

MERGE (hr:Hrs {hr: tn2[0]})
MERGE (hr)-[:DATE]->(dt)
MERGE (s1:S1 {name: line.S1})
MERGE (s1)-[:Time {val:tn[1]}]->(hr)
;
/>

Result:
2X_c_c5316bb25779025492b9b00b841168067a5f5cee.png

@ameyasoft thanks for the insight.

Based on your solution I tried to add the connections that the users made as shown in @strato.bayitaa 's initial post.

//create constraints
create constraint on (u:User) assert u.ID is unique

//our version of community code
LOAD CSV WITH HEADERS FROM 'file:/connections1.csv'AS line
WITH line, split(line.Date, "T") as tn
WITH line, tn, split(tn[1], ':') as tn2
//return tn2[1]
MERGE (dt:Date {date: tn[0]})
MERGE (hr:Hour {hr: tn2[0]})
MERGE (tm:Time {t:tn[1]})
MERGE (tm)-[:FOR]->(hr)
MERGE (dt)-[:HAS]->(hr)
MERGE (con:Connection)-[:AT]->(tm)
MERGE (s:Service {name: line.S})
MERGE (con)-[:TO]->(s)
WITH con,line
MERGE (U:User {ID:line.User})
with U,con
MERGE (U)-[:MAKES]->(con)
//MERGE (s)-[:Time {t:tn[1]}]->(hr)

It appears quite entangled. @ameyasoft can you think of a simpler way to achieve the same result?

@strato.bayitaa it may be necessary to place a unique constraint on the services nodes too for take care of different services.

Here is my data model:
LOAD CSV WITH HEADERS FROM 'file:/connections1.csv'AS line
WITH line, split(line.Date, "T") as tn
WITH line, tn, split(tn[1], ':') as tn2

MERGE (s:Service {name: line.S})
MERGE (dt:Date {date: tn[0]})
MERGE (s)-[:CONNECTED_DATE]->(dt)

MERGE (hr:Hour {hr: tn2[0]})
MERGE (dt)-[:HAS]->(hr)

MERGE (tm:Time {t:tn[1]})
MERGE (hr)-[:FOR]->(tm)

MERGE (U:User {ID:line.User})
MERGE (tm)-[:CONNECTED_USER]->(u)
;

I tested with my sample file with user id added.
Result:
2X_9_9feadc0f52ecf6f697f2d7a4e83be374aeadc2ac.png

Schema:

This way you can get all users connected to a service on any given date and hour.

I did not find code fro Connection node and hence I did not include.
Check and let me know.

My reason for explicitly modeling the connection node was to avoid the scenario where it would be difficult to query some information . For instance, in your proposed model, it appears that for multiple connections to different services on the same day, it will be difficult to identify which service a particular ->(hour)->(time)->(user) pattern is related to.

Thanks very much @ [ameyasoft]