Beginner: GSQL/TG Inserts

Hi everyone, I’m less than a day new to tigergraph and am trying to understand some of the GSQL syntax. One task I am interested in is performing and INSERT and retrieving the automatically created UUID.

Here’s my attempt at it, the error makes it mostly explicit what is wrong with the query, but I still am not sure how to proceed.


CREATE VERTEX human(
    UID STRING PRIMARY KEY,
    first_name STRING,
    last_name STRING
)

CREATE QUERY MakeHuman() FOR GRAPH people {
    STRING uuid = gsql_uuid_v4();
    INSERT INTO human (UID, first_name, last_name) 
    VALUES (uuid, "John", "Smith");
    PRINT uuid;
}

This also makes me curious about the role the REST++ API serves. For example, when I create a schema, do I get a route that lets me do INSERT/SELECT for free? If so, is there documentation on that?

Thanks again,
Casey

Hi Casey,

Which version of TigerGraph are you using? As far as I know, gsql_uuid_v4() is not available as a built-in function in the query language yet.

There is an endpoint that allows you to upsert data to your graph: https://docs.tigergraph.com/v/3.2/dev/restpp-api/built-in-endpoints#upsert-data-to-graph

There is also an endpoint to retrieve vertices from the graph:https://docs.tigergraph.com/v/3.2/dev/restpp-api/built-in-endpoints#list-vertices. It’s not as powerful as an actual SELECT statement, but it has parameters that allow you to filter vertices.

1 Like

I am using 3.1.5 while trying out the technology for our use case’s.

The gsql_uuid function does seem to work in the query language(it compiles and can be called), but it seems to only work in the following context:

CREATE QUERY MakeHuman() FOR GRAPH people {
    INSERT INTO human (UID, first_name, last_name) 
    VALUES (gsql_uuid_v4(), "John", "Smith");
}

Which is great, but the issue there is - I can’t insert a node and return it’s UID in one transaction. That kind of operation is pretty important too us for dynamic use of the graph. Is there an alternate means to generate read only UID’s? I saw some syntax for it using something like INSERT INTO human (PRIMARY_ID, first_name, last_name) VALUES (UID, "John", "Smith"); but I couldn’t find a means to either generate a UID instance within GSQL/TG API or get the function to compile.

if gsql_uuid_v4() doesn’t work in the query language, how is it intended to be used - is there a work around to get the behavior I am looking for? IE insert and return what was inserted.

Thanks for the information about the REST API - that could come in handy.

I was able to resolve the issue. The query I was looking for looks something like the following:

    CREATE QUERY MakeHuman() FOR GRAPH people{
        STRING guid;
        guid = gsql_uuid_v4();
        INSERT INTO human (UID, first_name, last_name) VALUES (guid, "John", "Smith");
        PRINT guid;
    }
1 Like

For data loading, I’m considering a situation where I would create several vertices from the same input line, but would need to create edges between them as well. I need to use GUIDs as the primary id/key. Do I need to first create a TEMP_TABLE and load it with the data and the GUIDs that I need for the vertices - so that a subsequent pass over the TEMP_TABLE will have all the values that it needs? [I don’t want to have to put GUIDs in the input file.] For example something like this:

LOAD file
TO TEMP_TABLE tmp(guid_1, guid_2, data_1, data_2) VALUES (gsql_uuid_v4(), gsql_uuid_v4(), $0, $1);

LOAD TEMP_TABLE tmp
TO VERTEX type_1 VALUES($“guid_1”, $“data_1”),
TO VERTEX type_2 VALUES($“guid_2”, $“data_2”),
TO EDGE edge_1_2 VALUES($“guid_1”, $“guid_2”);

Or is there a better way to do this?

Thanks,