Skip to content

Instantly share code, notes, and snippets.

@benoitguigal
Last active October 27, 2021 06:55
Show Gist options
  • Save benoitguigal/a9a2a7fa44cca0c84592283d28a0b743 to your computer and use it in GitHub Desktop.
Save benoitguigal/a9a2a7fa44cca0c84592283d28a0b743 to your computer and use it in GitHub Desktop.
graph = JanusGraphFactory.open('conf/janusgraph-cassandra-es.properties')
g = graph.traversal()
entityToId = [:]
new File('data/nodes.csv').eachLine { line, count ->
// Skip header
if (count > 1){
columns = line.split(',', -1)
(entity, attr1, attr2) = columns
node = g.addV('person')
.property('entity', entity)
.property('attr1', attr1)
.property('attr2', attr2)
.next()
nodeId = node.longId()
entityToId[entity] = nodeId
}
}
new File('data/links.csv').eachLine { line, count ->
if (count > 1){
columns = it.split(',')
(source_entity,
target_entity,
attr1,
attr2) = columns
fromVertex = entityToId.get(source_entity)
toVertex = entityToId.get(target_entity)
g.V(fromVertex).addE('is_linked_to').to(g.V(toVertex))
.property('attr1', attr1)
.property('attr2', attr2)
.next()
}
}
graph.tx().commit()
graph.close()
@apocalypsis360
Copy link

Great gist, thanks!

I succeeded with the following csv:

nodes.csv:
entity,attr1,attr2
1,'bla','bla bla'
2,'asdf','asdf2'
3,'abc','col2'
4,'col1','col2'
5,'col1a','col2a'
6,'col1b','col2b'

links.csv:
source_entity,target_entity,attr1,attr2
1,1,'1to1','attr2'
1,2,'1to2','attr2'
1,3,'1to3','attr2'
2,5,'2to5','bla'
5,2,'5to2','haha'
6,4,'6to4','sadf'
4,3,'4to3','ewwew'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment