Java String to Neo4J Create Graph statements

126 views Asked by At

I must create a graph based on the result of a query. After getting the results based on a retrieved column as String ( column that represents a SQL sentence) I need to generate the corresponding graph creation sentences as follows:

Retrieved column:

_fn1(filed1, filed2, filed3, '', filed4, filed5) AS new_alias_field

Sentences to be generated for the graph creation on Neo4j:

CREATE (new_alias_field:fields_from {column:'new_alias_field'})
CREATE (filed1:fields_to{column:'filed1'})
CREATE (filed2:fields_to{column:'filed2'})
CREATE (filed3:fields_to{column:'filed3'})
CREATE (filed4:fields_to{column:'filed4'})
CREATE (filed5:fields_to{column:'filed5'})

CREATE (fn1:function {name:'_fn1'})

  CREATE
  (filed1)-[:used_by {roles:['param']}]->(fn1),
  (filed2)-[:used_by {roles:['param']}]->(fn1),
  (filed3)-[:used_by {roles:['param']}]->(fn1),
  (filed4)-[:used_by {roles:['param']}]->(fn1),
  (filed4)-[:used_by {roles:['param']}]->(fn1)

CREATE
  (fn1)-[:as ]->(new_alias_field)

The best way to accomplish this is creating a parser? or should I use something like JSqlParser since the original string has some SQL statements ? Should I created my own parser? is there any other tool available?

I am trying to use JSqlParser, get the objects and then convert them. Still in progress .

to try the the code in Neo4J, after run the creation script you can query the nodes using :

Match(new_alias_field{column:'new_alias_field'}) return new_alias_field

you must get:

Result Graph

1

There are 1 answers

0
JPRLCol On BEST ANSWER

JSqlParser is a great parser it uses the visitor pattern. I used to parse this statements using the next expression

select <<statement>> from dual

I only limitation I found was: IF statement used in MSQL aren't supported yet.

Once I parsed this statements I used Spring annotation

@NodeEnityty

To create all the entities and make the insertions, for instance the FieldsFrom node is going to look like:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@NodeEntity
public class FieldsFrom {
   @Id
   @GeneratedValue
   private Long id;

   private String name;


   @Relationship(type= "USED_BY", direction = Relationship.INCOMING)
   private Set<FieldsTo> fieldsTo;

}

After all the processing, and all the insertions I used https://github.com/neo4j-contrib/neovis.js