This guide will walk you through the process of setting up and using a User-Defined Function (UDF) in TGCloud. Follow the steps below to achieve this.
- Login to TGCloud: Navigate to portal.tgcloud.io and log in with your credentials.
- Select Workspace:
- Go to
Workgroups
>Workspace
. - Click on the desired workspace name.
- Copy the host URL for the selected workspace.
- Go to
-
Open the browser console (usually by pressing
F12
orCtrl+Shift+I
). -
Run the following JavaScript command to retrieve the
id_token
:console.log(document.cookie.split('; ').find(row => row.startsWith('idTokenV4=')).split('=')[1]);
-
Copy the
id_token
from the output.
-
Use the copied host and
id_token
to verify access to the workspace. -
Run the following
curl
command in your terminal:curl https://{{host}}/api/ping2 -H 'Authorization: Bearer {{idTokenV4}}'
-
Replace
{{host}}
with the copied host URL and{{idTokenV4}}
with the copiedid_token
. -
Expected Result:
{ "error": false, "message": "pong", "results": null }
-
Use the following
curl
command to upload the UDF:curl -X PUT https://{{host}}/api/udf/ExprFunctions \ -H 'Authorization: Bearer {{idTokenV4}}' \ -H 'Content-Type: application/json' \ -d '{ "udf": "inline bool greater_than_three (double x) {\nreturn x > 5;\n}" }'
-
Expected Result:
{ "error": false, "message": "", "results": null }
-
Use the following
curl
command to get theExprFunctions
:curl https://{{host}}/api/udf/ExprFunctions \ -H 'Authorization: Bearer {{idTokenV4}}'
-
Expected Result:
{ "error": false, "message": "", "results": { "udf": "inline bool greater_than_three (double x) {\nreturn x > 5;\n}" } }
-
Create the GSQL Query:
CREATE QUERY CallUDFExample() { # Declare a variable to store the input value and the result double input_value = 6.0; bool result; # Call the UDF with the input value result = greater_than_three(input_value); # Print the input value and the result PRINT input_value, result; }
-
Install the Query:
INSTALL QUERY CallUDFExample
-
Run the Query:
RUN QUERY CallUDFExample()
-
Expected Result:
[ { "input_value": 6, "result": true } ]
By following these steps, you will have successfully set up and used a UDF in TDCloud.