Mastering GSQL's PRINT, FILE, and LOG statements to extract results.
Output & Logging
Extracting data from GSQL can be done via standard JSON responses, external files, or system logs.
1. The PRINT Statement
The primary way to return data from a query. Results are aggregated into a single JSON object delivered at the end of the query.
gsqlterminalPRINT @@total_count, StartNodes AS seed_set;
Vertex Expression Sets
You can customize the output of a vertex set by specifying only the attributes you want, or by computing values on the fly.
gsqlterminal# Only print id and country, and alias the country attribute PRINT MyNodes[MyNodes.id, MyNodes.country AS location];
2. Writing to Files (FILE)
For large datasets where JSON is inefficient, use the FILE object to write CSV or custom formatted text.
gsqlterminalCREATE QUERY export_data(STRING path) { FILE f1 (path); # Print headers PRINT "ID,Score" TO_CSV f1; # Print data in parallel (order not guaranteed) V = SELECT s FROM Start:s ACCUM f1.println(s.id, s.@score); }
Key Keywords:
TO_CSV: Automatically formats output as comma-separated values..println(): Writes a custom line to the file. Can be used insideACCUMfor high-speed parallel writes.
3. Subquery Returns
Use RETURN to pass data from a subquery to a parent query. The return type must match the RETURNS clause in the subquery definition.
gsqlterminalCREATE QUERY get_score(VERTEX<Person> v) RETURNS (FLOAT) { # ... calculation ... RETURN @@final_score; }
4. Debugging with LOG
The LOG statement writes directly to the GPE (Graph Processing Engine) logs. It is essential for debugging parallel logic.
gsqlterminal# Syntax: LOG(condition, args...) LOG(true, "Processing vertex", s.id, "at hop", @@hop_count);
[!TIP] To view logs, run
gadmin log gpein the shell and look for entries prefixed withUDF_.