Skip to main content

SageMaker SQL Magic Extension

This is a notebook extension provided by AWS SageMaker Studio team to run SQL queries inside SageMaker Jupyter notebooks. Currently, it supports running SQL on Redshift, Snowflake, and Athena.

Usage

Introduces the %%sm_sql and %sm_sql_manage ipython magic commands to run SQL queries inside SageMaker Jupyter notebooks.

Install

pip install amazon-sagemaker-sql-magic

Register the magic command:

%load_ext amazon_sagemaker_sql_magic

Show help content for %%sm_sql:

%%sm_sql?
Docstring:
::

  %sm_sql [--metastore-id METASTORE_ID] [--metastore-type METASTORE_TYPE]
              [--query-parameters QUERY_PARAMETERS]
              [--connection-properties CONNECTION_PROPERTIES]
              [--connection-name CONNECTION_NAME] [-df DATAFRAME]

Cell magic command to run SQL queries inside SageMaker Jupyter notebooks.

Format:
    %%sm_sql --metastore-id METASTORE_ID --metastore-type METASTORE_TYPE --query-parameters QUERY_PARAMETERS --connection-properties CONNECTION_PROPERTIES --connection-name CONNECTION_NAME -df, --dataframe DATAFRAME

Examples:
     # How to use '--metastore-id' and '--metastore-type'
     %%sm_sql --metastore-id my_glue_conn --metastore-type GLUE_CONNECTION
     SELECT * FROM my_db.my_schema.my_table

    # How to use '--connection-properties'
    %%sm_sql --connection-properties '{"connection_type": "SNOWFLAKE", "aws_secret_arn":"arn:aws:secretsmanager:us-west-2:123456789012:secret:my-snowflake-secret-123"}'
    SELECT * FROM my_db.my_schema.my_table

    # How to use '--query-parameters' with SNOWFLAKE/REDSHIFT as a data-source
    %%sm_sql --metastore-id my_glue_conn --metastore-type GLUE_CONNECTION --query-parameters '{"parameters":("John Smith")}'
    SELECT * FROM my_db.my_schema.my_table WHERE name = (%s);

    # How to use '--query-parameters' with ATHENA as a data-source
    %%sm_sql --metastore-id my_glue_conn --metastore-type GLUE_CONNECTION --query-parameters '{"parameters":{"name_var": "John Smith"}}'
    SELECT * FROM my_db.my_schema.my_table WHERE name = (%(name_var)s);

options:
  --metastore-id METASTORE_ID
                        Defines the metastore entity holding data-source
                        connection parameters e.g. a Glue connection name.
                        Support available for Glue connection.
  --metastore-type METASTORE_TYPE
                        Type of metastore to use for connecting to data-
                        source. Supported value(s): 'GLUE_CONNECTION'
  --query-parameters QUERY_PARAMETERS
                        SQL Query parameters as a dictionary encapsulator. See
                        examples above on how to use.
  --connection-properties CONNECTION_PROPERTIES
                        Data-source connection properties as a dictionary
                        encapsulator.See examples above on how to use.
  --connection-name CONNECTION_NAME
                        Name of the Glue connection to be re-used.
  -df DATAFRAME, --dataframe DATAFRAME
                        The name of pandas dataframe where the query results
                        will be stored

Show help content for %sm_sql_manage:

%sm_sql_manage?
Docstring:
::

  %sm_sql_manage [--set-connection-reuse SET_CONNECTION_REUSE]
                     [--list-cached-connections] [--clear-cached-connections]

Line magic command to manage SQL connections inside SageMaker Jupyter notebooks.

Format:
  %sm_sql_manage --set-connection-reuse True/False --list-cached-connections --clear-cached-connections

options:
  --set-connection-reuse SET_CONNECTION_REUSE
                        Set if connection should be reused. Example use:
                        %sm_sql_manage --set-connection-reuse True
  --list-cached-connections
                        List the cached connections. Example use:
                        %sm_sql_manage --list-cached-connections
  --clear-cached-connections
                        Clear all cached connections. Example use:
                        %sm_sql_manage --clear-cached-connections

Examples on how to use %%sm_sql

  1. Connect to a data-source using custom connection properties and fetch data from a table.
%%sm_sql --connection-properties '{"connection_type": "SNOWFLAKE", "aws_secret_arn":"arn:aws:secretsmanager:us-west-2:123456789012:secret:my-snowflake-secret-123"}'
SELECT * FROM my_db.my_schema.my_table
  1. Connect to a data-source using a Glue connection and fetch data from a table.
%%sm_sql --metastore-id my_glue_conn --metastore-type GLUE_CONNECTION
SELECT * FROM my_db.my_schema.my_table
  1. Connect to a data-source to fetch data from a table and save results into a pandas dataframe.
%%sm_sql --metastore-id my_glue_conn --metastore-type GLUE_CONNECTION --dataframe my_df
SELECT * FROM my_db.my_schema.my_table
  1. Connect to a data-source to fetch data from a table using a parameterized SQL query.
%%sm_sql --metastore-id my_glue_conn --metastore-type GLUE_CONNECTION --query-parameters '{"parameters":("John Smith")}'
UPDATE my_db.my_schema.my_table SET name = (%s);

License

This library is licensed under the Apache 2.0 License. See the LICENSE file.

Metadata

Release files for amazon-sagemaker-sql-magic 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for amazon-sagemaker-sql-magic 0.2.0
File Size Uploaded Provenance Actions
amazon_sagemaker_sql_magic-0.2.0.tar.gz 18.8 kB Without provenance Details for amazon_sagemaker_sql_magic-0.2.0.tar.gz

Release files / amazon_sagemaker_sql_magic-0.2.0.tar.gz

Download URL amazon_sagemaker_sql_magic-0.2.0.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4ac40526c844c53b54eb71e4d58346da747ba5cb5430af10abf36b7405662db1
BLAKE2b-256 checksum
How to use checksums
297df905a08a4b4c20d0bc73c8c7da2d7ea6c6c5d10d6023e2bc221ab69bdfb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Security for amazon-sagemaker-sql-magic 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page