Skip to content

Latest commit

ย 

History

History
167 lines (114 loc) ยท 2.63 KB

File metadata and controls

167 lines (114 loc) ยท 2.63 KB

Schema Design using GSQL

๐Ÿ”น 1. Connect to GSQL

First, enter your container:

docker exec -it tigergraph bash

Then launch the GSQL shell:

gsql

๐Ÿ”น 2. Global Context

Always start schema design with global mode:

USE GLOBAL

This allows you to define vertices, edges, and graphs.


๐Ÿ”น 3. Dropping Old Schema (Optional)

Before creating a fresh schema, you may want to drop existing ones.

  • Drop an edge:
DROP EDGE placed
  • Drop a vertex:
DROP VERTEX Customer

๐Ÿ”น 4. Creating Vertices

Define entities as vertices. Each vertex needs a PRIMARY_ID and can have attributes.

CREATE VERTEX Customer (
  PRIMARY_ID customer_id STRING,
  name STRING,
  email STRING
)

CREATE VERTEX Order (
  PRIMARY_ID order_id STRING,
  date DATETIME,
  amount FLOAT
)

CREATE VERTEX Product (
  PRIMARY_ID product_id STRING,
  name STRING,
  price FLOAT
)

๐Ÿ”น 5. Creating Edges

Edges represent relationships between vertices.

CREATE DIRECTED EDGE placed (FROM Customer, TO Order)

CREATE DIRECTED EDGE contains (FROM Order, TO Product)
  • DIRECTED โ†’ one-way relationship (e.g., Customer โ†’ Order).
  • UNDIRECTED โ†’ two-way relationship (e.g., friendship).

๐Ÿ”น 6. Creating the Graph

Finally, bundle everything into a graph object.

CREATE GRAPH ecommerceGraph(
  Customer,
  Order,
  Product,
  placed,
  contains
)

This graph now knows which vertices and edges belong together.


๐Ÿ”น 7. Example Workflow

Hereโ€™s the full minimal schema file (schema.gsql):

USE GLOBAL

DROP GRAPH ecommerceGraph

DROP EDGE placed
DROP EDGE contains

DROP VERTEX Customer
DROP VERTEX Order
DROP VERTEX Product

CREATE VERTEX Customer (
  PRIMARY_ID customer_id STRING,
  name STRING,
  email STRING
)

CREATE VERTEX Order (
  PRIMARY_ID order_id STRING,
  date DATETIME,
  amount FLOAT
)

CREATE VERTEX Product (
  PRIMARY_ID product_id STRING,
  name STRING,
  price FLOAT
)

CREATE DIRECTED EDGE placed (FROM Customer, TO Order)
CREATE DIRECTED EDGE contains (FROM Order, TO Product)

CREATE GRAPH ecommerceGraph(Customer, Order, Product, placed, contains)

Run this inside GSQL:

gsql schema.gsql

Incase of any previous loading job conflicts run this to clear all vertex and edge data instances without dropping the schema (similar to truncate):

CLEAR GRAPH STORE

๐Ÿ”น 8. Next Steps

  • Create a loading job (LOAD statements) to load data from CSV into these vertices/edges.
  • Access GraphStudio (GUI) at: ๐Ÿ‘‰ http://localhost:14240