Create vetrex if not exists for component analysis

I want to run more analytics on the results of connected components algorithms.
So I get the result of the conn_comp with ease but then I cant do anything with it since I only have the id (or the community) as an attribution.

I thought of making a “community” vertex and connect every node with that id to that vertex.
following this addition, I can run more queries per each community separately.

for example, I can check what is the average outdegree in each community.

Now, I can’t find the way to run over all the nodes check their connected component id and link them to a “community” node for this id.

In short:

  • how do I only create vertex if it does not exist?
  • whats the best way to link nodes together by one node and its attributions (here it’s the cc_id attr)?

thanks a lot.

This example works with a “Person” vertex, and we connect all valid Person-Community pairings with the edge PERSON_HAS_COMMUNITY.

CREATE QUERY communityLinks() FOR GRAPH GraphName { 
  start = {Person.*};
  ins = SELECT s FROM start:s
        POST-ACCUM INSERT INTO PERSON_HAS_COMMUNITY VALUES (s.id, s.cc_id);
}

If the Community vertex with PRIMARY ID s.cc_id does not exist, it will be created and linked. Otherwise, the new edge will be connected to the existing Community vertex.

You may have to change the order of the arguments for VALUES (in the INSERT INTO statement) depending on the TO/FROM vertices that you defined in the schema for the edge in question.

Learn more here:
docs.tigergraph.com/dev/gsql-ref/querying/data-modification-statements#insert-into-statement

1 Like

I am trying this!
Thanks a lot!

Ok. I Can’t make it work.

it doesn’t create it.
Maybe because “Community” (with cc_id id) vertex does not exist, and creating an edge when the second node is not defined doesn’t work?!

can it be because my PRIMARY_ID is set to be “as attribution” = TRUE

??

Solved.

For anyone else who seeks this. This what worked for me:

CREATE QUERY community_01(INT start_at=0, INT steps=1000000) FOR GRAPH B2B syntax v2{
SumAccum @@counter=0;

Res = SELECT s
FROM (FU|Company):s
// sampling to make the checking process faster.
WHERE s.data3>= start_at and s.data3 < start_at+steps
POST-ACCUM
if s.type == “FU” THEN
INSERT INTO COMMUNITY_MEMBER (FROM, TO) VALUES (s.fu_id FU, s.data3 Community)
ELSE
INSERT INTO COMMUNITY_MEMBER (FROM, TO) VALUES (s.company_node_id Company, s.data3 Community)
END,
@@counter+=1
;

PRINT @@counter;
}

1 Like

Kudos to you for getting it to work!

I just wanted to emphasize that when using INSERT INTO on an edge: if either the source/target edge does not exist, it will be created using the PRIMARY ID argument that you provided in the statement.