Define a Schema - GSQL 101
This section of the tutorial uses a simple example to walk you through the steps to define a schema in GSQL.
The first step in creating a GSQL graph is to define its schema. GSQL provides a set of Data Definition Language (DDL) commands, similar to SQL DDL commands, to model vertex types, edge types and a graph.
For this tutorial, we will work mostly in the GSQL shell, in interactive mode. A few commands will be from a Linux shell.
CREATE VERTEX to define a vertex type named
PRIMARY_ID is required: each person must have a unique identifier.
The rest is the optional list of attr ibutes which characterize each person vertex, in the format
attribute_name data_type, attribute_name data_type, …
BEGIN CREATE VERTEX Person ( PRIMARY_ID name STRING, name STRING, age INT, gender STRING, state STRING ) END
We show GSQL keywords in all caps to highlight them in accordance with the GSQL Style Guide, but they are case-insensitive.
GSQL will confirm the creation of the vertex type.
GSQL > CREATE VERTEX Person (PRIMARY_ID name STRING, name STRING, age INT, gender STRING, state STRING) Successfully created vertex types: [Person].
You can create as many vertex types as you need.
A vertex primary key has a length limit of 16384 bytes. The size of a vertex, including all of its attributes, cannot exceed 10 megabytes.
Next, use the
CREATE … EDGE command to create an edge type named
UNDIRECTED indicates this edge is a bidirectional edge, meaning that information can flow starting from either vertex.
If you’d rather have a unidirectional connection where information flows only from the
FROM vertex, use the
DIRECTED keyword in place of
TO are required to specify which two vertex types the edge type connects.
An individual edge is specified by giving the primary_ids of its source (
FROM) vertex and target (
These are followed by an optional list of attributes, just as in the vertex definition.
CREATE UNDIRECTED EDGE Friendship (FROM Person, TO Person, connect_day DATETIME)
GSQL will confirm the creation of the edge type.
GSQL > CREATE UNDIRECTED EDGE Friendship (FROM Person, TO Person, connect_day DATETIME) Successfully created edge types: [Friendship]. GSQL >
You can create as many edge types as you need.
Next, use the
CREATE GRAPH command to create a graph named
Here, we just list the vertex types and edge types that we want to include in this graph.
CREATE GRAPH Social (Person, Friendship)
GSQL will confirm the creation of the first graph after several seconds, during which it pushes the catalog information to all services, such as the Graph Storage Engine (GSE), Graph Processing Engine (GPE) and TigerGraph’s REST server - RESTPP.
GSQL > CREATE GRAPH Social (Person, Friendship) Stopping GPE GSE RESTPP Successfully stopped GPE GSE RESTPP in 16.554 seconds Starting GPE GSE RESTPP Successfully started GPE GSE RESTPP in 0.119 seconds The graph Social is created.
At this point, we have created a
Person vertex type, a
Friendship edge type, and a
Social graph that includes them.
You’ve now built your first graph schema! Take a look at the catalog by typing the
ls command in the GSQL shell.
GSQL > ls ---- Graph Social Vertex Types: - VERTEX Person(PRIMARY_ID name STRING, name STRING, age INT, gender STRING, state STRING) WITH STATS="OUTDEGREE_BY_EDGETYPE" Edge Types: - UNDIRECTED EDGE Friendship(FROM Person, TO Person, connect_day DATETIME) Graphs: - Graph Social(Person:v, Friendship:e) Jobs: Queries: