Skip to main content

AGENT Client

Overview​

You can also use the antigranular_enterprise library to connect and make queries to AGENT SQL. There are two ways you can use the library:

Using AGENT SQL in jupyter notebooks​

You can use antigranular_enterprise to make SQL queries to AGENT SQL from Jupyter notebooks. When you login, a %%sql magic is registered. Just write your query and execute the cell. It will run the SQL query and print a results preview.

import antigranular_enterprise as ag

ag.load_config("<Your Config URL>", profile='default')
ag.login_sql(api_key = "<Your API Key>", params={"skip_cache": True})

Connection parameters should be passed as a dictionary to the params argument. It is optional and can be left blank. Note that we have used the login_sql method to login here. Hence, we now use %%sql magic to run the queries. This is separate from %%ag which is used to run a code snippet in AGENT Private Python environment. You cannot use %ag% unless you login with the login method.

%%sql
with dp(eps=0.01, delta=0.0005)
select gender, count(*) as total
from school.students
group by gender

The execution prints a preview and exports the results in the form of a pandas dataframe into a local variable sql_results_df.

UserWarning: No variable name provided, using default name 'sql_results_df' instead
warnings.warn("No variable name provided, using default name 'sql_results_df' instead")
Execution successful, 2 rows returned
Preview:
gender total
0 Female 2433
1 Male 2464
Exported results dataframe into local variable 'sql_results_df'

Overwriting the default variable name​

If you want the results to be exported to a different variable name, you have to provide the variable name after %%sql. For example,

%%sql my_var
with dp(eps=0.01, delta=0.0005)
select gender, count(*) as total
from school.students
group by gender

This will export the results to a variable called my_var instead of sql_results_df.

Execution successful, 2 rows returned
Preview:
gender total
0 Female 2433
1 Male 2464
Exported results dataframe into local variable 'my_var'
Warning! ⚠️

If the variable name is left blank or the name is not a valid python variable name, the results will be exported to a variable called sql_results_df by default. This will override any existing variable with the same name and the previous value will be lost.

Using the AGENT SQL in Python scripts​

You can use antigranular_enterprise to make SQL queries to AGENT SQL from Python scripts. The snippet below shows how to connect to AGENT SQL and run a query.

import antigranular_enterprise as ag

ag.load_config("<Your Config URL>", profile='default')
sql_client = ag.login_sql(api_key = "<Your API Key>")
results = sql_client.execute(
"with dp(eps=0.01, delta=0.0005) "
"select gender, count(*) as total "
"from schol.students "
"group by gender"
)
print(results)

The results returned from the .execute method are in the form of a 2-dimensional list where the first element is a list of columns and the following elements denote the corresponding rows.

[['gender', 'total'], ['Female', 2433], ['Male', 2464]]