> ## Documentation Index
> Fetch the complete documentation index at: https://tigerdata-747200db-chore-custom-colored-homepage.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Amazon Sagemaker with Tiger Cloud

> Amazon SageMaker is a fully managed machine learning service. Integrate Amazon SageMaker with Tiger Cloud to store and analyze ML model data

export const HYPERCORE = 'hypercore';

export const PG = 'Postgres';

export const TIMESCALE_DB = 'TimescaleDB';

export const SERVICE_LONG = 'Tiger Cloud service';

export const SERVICE_SHORT = 'service';

export const CONSOLE = 'Tiger Cloud Console';

export const CLOUD_LONG = 'Tiger Cloud';

[Amazon SageMaker AI][Amazon Sagemaker] is a fully managed machine learning (ML) service. With SageMaker AI, data
scientists and developers can quickly and confidently build, train, and deploy ML models into a production-ready
hosted environment.

This page shows you how to integrate Amazon Sagemaker with a {SERVICE_LONG}.

## Prerequisites

To follow the steps on this page:

* Create a target [{SERVICE_LONG}][create-service] with time-series and analytics enabled.

  You need your [connection details][connection-info].

[create-service]: /cloud/get-started/create-services

[connection-info]: /integrations/find-connection-details/

* Set up an [AWS Account][aws-sign-up]

## Prepare your {SERVICE_LONG} to ingest data from SageMaker

Create a table in {SERVICE_LONG} to store model predictions generated by SageMaker.

<Procedure>
  1. **Connect to your {SERVICE_LONG}**

     For {CLOUD_LONG}, open an [SQL editor][run-queries] in [{CONSOLE}][open-console]. For {SELF_LONG}, use [`psql`][psql].

  2. **For better performance and easier real-time analytics, create a hypertable**

     [Hypertables][about-hypertables] are {PG} tables that automatically partition your data by time. You interact
     with hypertables in the same way as regular {PG} tables, but with extra features that makes managing your
     time-series data much easier.

     ```sql
     CREATE TABLE model_predictions (
       time TIMESTAMPTZ NOT NULL,
       model_name TEXT NOT NULL,
       prediction DOUBLE PRECISION NOT NULL
     ) WITH (
       tsdb.hypertable,
       tsdb.partition_column='time'
     );
     ```

     If you are self-hosting {TIMESCALE_DB} v2.19.3 and below, create a [{PG} relational table][pg-create-table],
     then convert it using [create\_hypertable][create_hypertable]. You then enable {HYPERCORE} with a call
     to [ALTER TABLE][alter_table_hypercore].

     [pg-create-table]: https://www.postgresql.org/docs/current/sql-createtable.html

     [create_hypertable]: /api/hypertable/create_hypertable/

     [alter_table_hypercore]: /api/hypercore/alter_table/
</Procedure>

## Create the code to inject data into a {SERVICE_LONG}

<Procedure>
  1. **Create a SageMaker Notebook instance**

     1. In [Amazon SageMaker > Notebooks and Git repos][aws-notebooks-git-repos], click `Create Notebook instance`.
     2. Follow the wizard to create a default Notebook instance.

  2. **Write a Notebook script that inserts data into your {SERVICE_LONG}**

     1. When your Notebook instance is `inService,` click `Open JupyterLab` and click `conda_python3`.
     2. Update the following script with your [connection details][connection-info], then paste it in the Notebook.

        ```python
        import psycopg2
        from datetime import datetime

        def insert_prediction(model_name, prediction, host, port, user, password, dbname):
              conn = psycopg2.connect(
                 host=host,
                 port=port,
                 user=user,
                 password=password,
                 dbname=dbname
              )
              cursor = conn.cursor()

              query = """
                 INSERT INTO model_predictions (time, model_name, prediction)
                 VALUES (%s, %s, %s);
              """

              values = (datetime.utcnow(), model_name, prediction)
              cursor.execute(query, values)
              conn.commit()

              cursor.close()
              conn.close()

        # Example usage
        insert_prediction(
              model_name="example_model",
              prediction=0.95,
              host="<host>",
              port="<port>",
              user="<user>",
              password="<password>",
              dbname="<dbname>"
        )
        ```

  3. **Test your SageMaker script**

     1. Run the script in your SageMaker notebook.
     2. Verify that the data is in your {SERVICE_SHORT}

        Open an [SQL editor][run-queries] and check the `sensor_data` table:

        ```sql
        SELECT * FROM model_predictions;
        ```

        You see something like:

        | time                          | model\_name           | prediction |
        | ----------------------------- | --------------------- | ---------- |
        | 2025-02-06 16:56:34.370316+00 | timescale-cloud-model | 0.95       |
</Procedure>

Now you can seamlessly integrate Amazon SageMaker with {CLOUD_LONG} to store and analyze time-series data generated by
machine learning models. You can also untegrate visualization tools like [Grafana][grafana-integration] or
[Tableau][tableau-integration] with {CLOUD_LONG} to create real-time dashboards of your model predictions.

[Amazon Sagemaker]: https://docs.aws.amazon.com/sagemaker/latest/dg/whatis.html

[aws-sign-up]: https://signin.aws.amazon.com/signup?request_type=register

[install-aws-cli]: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html

[install-python]: https://www.python.org/downloads/

[install-postgresql]: https://www.postgresql.org/download/

[console]: https://console.cloud.timescale.com/

[grafana-integration]: /integrations/:currentVersion:/grafana/

[tableau-integration]: /integrations/:currentVersion:/tableau/

[run-queries]: /getting-started/:currentVersion:/run-queries-from-console/

[open-console]: https://console.cloud.timescale.com/dashboard/services

[psql]: /integrations/:currentVersion:/psql/

[about-hypertables]: /use-timescale/:currentVersion:/hypertables/

[aws-notebooks-git-repos]: https://console.aws.amazon.com/sagemaker/home#/notebooks-and-git-repos

[secure-vpc-aws]: /use-timescale/:currentVersion:/vpc/

[connection-info]: /integrations/:currentVersion:/find-connection-details/
