Connect to SQL data
To connect to your SQL data, you first create a Data Source which tells GX where your database resides and how to connect to it. You then configure Data Assets for your Data Source to tell GX which sets of records you want to be able to access from your Data Source. Finally, you will define Batch Definitions which allow you to request all the records retrieved from a Data Asset or further partition the returned records based on the contents of a date and time field.
GX supports the following SQL dialects:
- PostgreSQL
- SQLite
- Snowflake
- Databricks SQL
- BigQuery SQL
All other SQL dialects are handled through the python module SQLAlchemy
. You can find more information on the dialects supported by SQLAlchemy
on their dialects page.
Configure credentials
To connect GX to your SQL data, you will need your connection string and corresponding credentials. Because your connection string and credentials provide access to your data they should be stored securely outside of version control. GX Core allows you to securely store credentials and connection strings as environment variables or in an uncommitted config file. These variables are then accessed through string substitution in your version controlled code.
Prerequisites
- The ability to set environment variables or a File Data Context.
GX Core also supports referencing credentials that have been stored in the AWS Secrets Manager, Google Cloud Secret Manager, and Azure Key Vault secrets managers. To set up GX Core to access one of these secrets managers you will additionally require:
- The ability to install Python modules with
pip
.
Procedure
-
Determine your connection string format.
Different types of SQL database have different formats for their connection string. In the following table, the text in
<>
corresponds to the values specific to your credentials and connection string.Database type Connection string PostgreSQL postgresql+psycopg2://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/<DATABASE>
SQLite sqlite:///<PATH_TO_DB_FILE>
Snowflake snowflake://<USER_NAME>:<PASSWORD>@<ACCOUNT_NAME>/<DATABASE_NAME>/<SCHEMA_NAME>?warehouse=<WAREHOUSE_NAME>&role=<ROLE_NAME>&application=great_expectations_oss
Databricks SQL databricks://token:<TOKEN>@<HOST>:<PORT>?http_path=<HTTP_PATH>&catalog=<CATALOG>&schema=<SCHEMA>
BigQuery SQL bigquery://<GCP_PROJECT>/<BIGQUERY_DATASET>?credentials_path=/path/to/your/credentials.json
Other connection string formats are valid provided they are for a SQL database that is supported by SQLAlchemy. You can find more information on the dialects supported by
SQLAlchemy
on their dialects page. -
Store the credentials required for your connection string.
GX supports the following methods of securely storing credentials. Chose one to implement for your connection string:
- Environment Variables
- config.yml
Environment variables provide the quickest way to securely set up your credentials.
You can set environment variables by replacing the values in
<>
with your information and enteringexport <VARIABLE_NAME>=<VALUE>
commands in the terminal or adding the commands to your~/.bashrc
file. If you use theexport
command from the terminal, the environment variables will not persist beyond the current session. If you add them to the~/.bashrc
file, the variables will be exported each time you log in.You can export individual credentials or an entire connection string. For example:
Terminal or ~/.bashrcexport MY_POSTGRES_USERNAME=<USERNAME>
export MY_POSTGRES_PASSWORD=<PASSWORD>or:
Terminal or ~/.bashrcexport POSTGRES_CONNECTION_STRING=postgresql+psycopg2://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/<DATABASE>
You can also reference your stored credentials within a stored connection string by wrapping their corresponding variable in
${
and}
. For example:Terminal or ~/.bashrcexport MY_POSTGRES_USERNAME=<USERNAME>
export MY_POSTGRES_PASSWORD=<PASSWORD>
export POSTGRES_CONNECTION_STRING=postgresql+psycopg2://${MY_POSTGRES_USERNAME}:${MY_POSTGRES_PASSWORD}@<HOST>:<PORT>/<DATABASE>Because the dollar sign character
$
is used to indicate the start of a string substitution they should be escaped using a backslash\
if they are part of your credentials. For example, if your password ispa$$word
then in the previous examples you would use the command:Terminal or ~/.bashrcexport MY_POSTGRES_PASSWORD=pa\$\$word
YAML files make variables more visible, are easier to edit, and allow for modularization. For example, you can create a YAML file for development and testing and another for production.
A File Data Context is required before you can configure credentials in a YAML file. By default, the credentials file in a File Data Context is located at
/great_expectations/uncommitted/config_variables.yml
. Theuncommitted/
directory is included in a default.gitignore
and will be excluded from version control.Save your access credentials or the database connection string to
great_expectations/uncommitted/config_variables.yml
. For example:config_variables.ymlMY_POSTGRES_USERNAME: <USERNAME>
MY_POSTGRES_PASSWORD: <PASSWORD>or:
config_variables.ymlPOSTGRES_CONNECTION_STRING: postgresql+psycopg2://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/<DATABASE>
You can also reference your stored credentials within a stored connection string by wrapping their corresponding variable in
${
and}
. For example:config_variables.ymlMY_POSTGRES_USERNAME: <USERNAME>
MY_POSTGRES_PASSWORD: <PASSWORD>
POSTGRES_CONNECTION_STRING: postgresql+psycopg2://${MY_POSTGRES_USERNAME}:${MY_POSTGRES_PASSWORD}@<HOST>:<PORT>/<DATABASE>Because the dollar sign character
$
is used to indicate the start of a string substitution they should be escaped using a backslash\
if they are part of your credentials. For example, if your password ispa$$word
then in the previous examples you would use the command:Terminalexport MY_POSTGRES_PASSWORD=pa\$\$word
-
Access your credentials in Python strings.
Securely stored credentials are accessed via string substitution. You can reference your credentials in a Python string by wrapping the variable name in
${
and}
. Using individual credentials would look like:Pythonconnection_string="postgresql+psycopg2://${MY_POSTGRES_USERNAME}:${MY_POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DATABASE}",
Or you could reference a configured variable that contains the full connection string by providing a Python string that contains just a reference to that variable:
Pythonconnection_string="${POSTGRES_CONNECTION_STRING}"
When you pass a string that references your stored credentials to a GX Core method that requires a connection string as a parameter the referenced variable will be substituted for the corresponding stored value.
-
Optional. Access credentials stored in a secret manager.
GX Core supports the AWS Secrets Manager, Google Cloud Secret Manager, and Azure Key Vault secrets managers. For more information on how to set up string substitutions that pull credentials from these sources, see Access secrets managers.
Create a SQL Data Source
Data Sources tell GX where your data is located and how to connect to it. With SQL databases this is done through a connection string you will provide.
Prerequisites
- Python version 3.9 to 3.12.
- An installation of GX Core with support for SQL dependencies
- A preconfigured Data Context.
- Credentials stored securely outside of version control.
Procedure
- Instructions
- Sample code
-
Import GX and instantiate a Data Context:
Pythonimport great_expectations as gx
context = gx.get_context() -
Define a name and connection string for your Data Source.
You can assign any name to a Data Source as long as it is unique within your Data Context.
Your connection string or credentials should not be saved in plain text in your code. Instead, you should reference a securely stored connection string or credentials through string substitution. The guidance on how to Configure your credentials covers how to determine the format of your connection string, securely store your connection string or credentials, and how to reference your connection string or credentials in Python.
The following code defines a Data Source name and references a PostgreSQL connection string that has been securely stored in its entirety:
Pythondatasource_name = "my_new_datasource"
my_connection_string = "${POSTGRESQL_CONNECTION_STRING}" -
Create a Data Source.
GX Core provides specific methods for creating Data Sources that correspond to supported SQL dialects. All of these methods are accessible from the
data_sources
attribute of your Data Context. Reference the following table to determine the method used for your data's SQL dialect:Database type Data Context method PostgreSQL context.data_sources.add_postgres(name: str, connection_string: str)
SQLite context.data_sources.add_sqlite(name: str, connection_string: str)
Snowflake context.data_sources.add_snowflake(name:str, connection_string: str)
DataBricks SQL context.data_sources.add_databricks_sql(name: str, connection_string: str)
Other SQL context.data_sources.add_sql(name: str, connection_string:str)
Once you have the method for your data's SQL dialect, you can call it with the previously defined Data Source name and connection string to create your Data Source. The following example creates a PostgreSQL Data Source:
Pythondata_source = context.data_sources.add_postgres(
name=datasource_name, connection_string=my_connection_string
) -
Optional. Verify the Data Source is connected:
Pythonprint(context.data_sources.get(datasource_name))
The details of your Data Source are retrieved from the Data Context and displayed.
import great_expectations as gx
context = gx.get_context()
datasource_name = "my_new_datasource"
my_connection_string = "${POSTGRESQL_CONNECTION_STRING}"
data_source = context.data_sources.add_postgres(
name=datasource_name, connection_string=my_connection_string
)
print(context.data_sources.get(datasource_name))