> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Amazon SageMaker with Tiger Cloud

> Build, train, and deploy ML models with time-series data storage and analysis

export const SERVICE_SHORT = 'service';

export const PG = 'Postgres';

export const CONSOLE = 'Tiger Console';

export const CLOUD_LONG = 'Tiger Cloud';

export const CHUNK = 'chunk';

export const HYPERTABLE = 'hypertable';

export const COLUMNSTORE = 'columnstore';

export const TIMESCALE_DB = 'TimescaleDB';

export const SERVICE_LONG = 'Tiger Cloud service';

export const SELF_LONG = 'self-hosted TimescaleDB';

[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 Real-time analytics enabled.<p />

  You need [your connection details][connection-info]. This procedure also
  works for [{SELF_LONG}][enable-timescaledb].

[create-service]: /deploy-and-operate/tiger-cloud/get-started/create-services

[enable-timescaledb]: /deploy-and-operate/self-hosted/install-and-update/install-self-hosted

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

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

## Prepare your service to ingest data from SageMaker

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

1. **Connect to your {SERVICE_LONG}**

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

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

   [Hypertables][hypertables-section] 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 theme={"dark"}
   CREATE TABLE model_predictions (
     time TIMESTAMPTZ NOT NULL,
     model_name TEXT NOT NULL,
     prediction DOUBLE PRECISION NOT NULL
   ) WITH (
     tsdb.hypertable
   );
   ```

   When you create a {HYPERTABLE} using [CREATE TABLE ... WITH ...][hypertable-create-table], the default partitioning
   column is automatically the first column with a timestamp data type. Also, {TIMESCALE_DB} creates a
   [columnstore policy][add_columnstore_policy] that automatically converts your data to the {COLUMNSTORE}, after an
   interval equal to the value of the [chunk\_interval][create_table_arguments], defined through `compress_after` in the
   policy. This columnar format enables fast scanning and
   aggregation, optimizing performance for analytical workloads while also saving significant storage space. In the
   {COLUMNSTORE} conversion, {HYPERTABLE} {CHUNK}s are compressed by up to 98%, and organized for efficient, large-scale queries.

   You can customize this policy later using [alter\_job][alter_job_samples]. However, to change `after` or
   `created_before`, the compression settings, or the {HYPERTABLE} the policy is acting on, you must
   [remove the columnstore policy][remove_columnstore_policy] and [add a new one][add_columnstore_policy].

   You can also manually [convert {CHUNK}s][convert_to_columnstore] in a {HYPERTABLE} to the {COLUMNSTORE}.

   [add_columnstore_policy]: /api-reference/timescaledb/hypercore/add_columnstore_policy

   [alter_job_samples]: /api-reference/timescaledb/jobs-automation/alter_job#samples

   [convert_to_columnstore]: /api-reference/timescaledb/hypercore/convert_to_columnstore

   [create_table_arguments]: /api-reference/timescaledb/hypertables/create_table#arguments

   [hypertable-create-table]: /api-reference/timescaledb/hypertables/create_table

   [remove_columnstore_policy]: /api-reference/timescaledb/hypercore/remove_columnstore_policy

## Create the code to inject data into a service

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 theme={"dark"}
      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][in-console-editors] and check the `sensor_data` table:

      ```sql theme={"dark"}
      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       |

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 integrate visualization tools like [Grafana][grafana] or
[Tableau][tableau] 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-notebooks-git-repos]: https://console.aws.amazon.com/sagemaker/home#/notebooks-and-git-repos

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

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

[grafana]: /integrations/observability-alerting/grafana

[hypertables-section]: /use-timescale/hypertables

[in-console-editors]: /deploy-and-operate/tiger-cloud/get-started/run-queries-from-console

[psql]: /integrations/query-administration/psql

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

[tableau]: /integrations/bi-vizualization/tableau
