

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 查詢編輯器：搭配 Aurora DSQL 使用 JupyterLab
<a name="SECTION_program-with-jupyter"></a>

 本指南提供step-by-step指示，說明如何使用 JupyterLab 搭配 Python 來連接和查詢 Amazon Aurora DSQL。JupyterLab 是一種熱門的互動式運算環境，將程式碼、文字和視覺化結合在單一文件中。它廣泛用於資料科學和研究應用程式。

 以下說明將涵蓋 JupyterLab 的本機安裝以及使用 Amazon SageMaker AI 的 Aurora DSQL 使用基本知識，Amazon SageMaker AI 是一種全受管的機器學習服務，可為資料工作流程提供具有 UI 的託管環境。

## 開始使用
<a name="SECTION_program-with-jupyter-getting-started"></a>

### 要求
<a name="SECTION_program-with-jupyter-requirements"></a>
+  Aurora DSQL 叢集 
+  已設定的 AWS 登入資料 （僅限本機安裝） 
+  Python 3.9 版或更新版本 （僅限本機安裝） 

### 使用本機 JupyterLab
<a name="SECTION_program-with-jupyter-local"></a>

 若要開始使用 JupyterLab，使用者必須先使用 Python 的 **pip** 安裝應用程式：

```
pip install jupyterlab
```

 然後，執行 即可開啟 JupyterLab**jupyter lab**。這將在 localhost：8888 開啟 JupyterLab 應用程式，可在瀏覽器中存取。在繼續之前，請確定已在本機環境中設定 AWS 登入資料。

### 使用 Amazon SageMaker AI
<a name="SECTION_program-with-jupyter-sagemaker"></a>

 在 AWS 主控台中，繼續前往 Amazon SageMaker AI 主控台頁面，然後前往**應用程式和 IDEs **下的**筆記本**區段。您可以在該處選取**建立筆記本執行個體**，以開始建立 SageMaker 環境。選取執行個體類型和平台，再按一下**建立筆記本執行個體**。

 如需[設定和執行個體選項的詳細資訊，請參閱 Amazon SageMaker AI 設定文件](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-setup-working-env.html)。

**注意**  
警告：使用 Amazon SageMaker AI 可能會導致您的 AWS 帳戶產生費用。

 一旦 SageMaker 執行個體變成作用中，您就可以使用 **Open JupyterLab** 從**筆記本執行個體**區段開啟它。在筆記本中開始使用 Aurora DSQL 之前，您必須在 SageMaker 執行個體的 IAM 角色中提供 DSQL 叢集的存取權。最簡單的方法是遵循筆記本執行個體頁面中 IAM 角色的連結。您可以在該處編輯連接到 SageMaker IAM 角色的政策。如需設定 IAM 政策以允許存取 Aurora DSQL 的詳細資訊，請參閱[身分驗證和授權](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/authentication-authorization.html)。

### 使用 JupyterLab 連線至 Aurora DSQL
<a name="SECTION_program-with-jupyter-connecting"></a>

 設定 JupyterLab 執行個體後，連線至 Aurora DSQL 的步驟會在本機和 SageMaker AI 中相同。建立空的 Python 3 筆記本，您可以在其中使用 Python 程式碼新增儲存格。

 在 Python 儲存格中，從官方信任存放區下載 Amazon 根憑證：

```
import urllib.request
urllib.request.urlretrieve('https://www.amazontrust.com/repository/AmazonRootCA1.pem', 'root.pem')
```

 若要連線至 Aurora DSQL，請先在 Python 儲存格中安裝適用於 [Python 的 Aurora DSQL Connector](https://github.com/awslabs/aurora-dsql-connectors/tree/main/python/connector) 和 Psycopg 驅動程式，然後將其匯入：

```
pip install aurora_dsql_python_connector psycopg
```

```
import aurora_dsql_psycopg as dsql
```

 匯入連接器後，您就可以建立 DSQL 組態並進行連線。Aurora DSQL Python Connector 會自動在每個連線上處理身分驗證字符的建立。

```
config = {
    'host': "your-cluster.dsql.us-east-1.on.aws",
    'region': "us-east-1",
    'user': "admin"
    }

conn = dsql.connect(**config)
```

 執行程式碼時，您現在應該擁有與 Aurora DSQL 的 Psycopg 連線。然後，您可以使用 Psycopg 游標執行查詢並提供 SQL 查詢。如需搭配 Postgres 相容資料庫使用 Psycopg 的詳細資訊，請參閱 Psycopg [文件](https://www.psycopg.org/psycopg3/docs/)。此查詢將產生 中的元組清單`results_list`。

```
with conn:
    with conn.cursor() as cur:
        cur.execute("SELECT * FROM table")
        results_list = cur.fetchall()
```

 然後，您可以使用 [Pandas](https://pandas.pydata.org/) 等 Python 架構來分析或視覺化查詢結果，例如：

```
pip install pandas

import pandas as pd

df = pd.DataFrame(tuples_list)
print(df)
print(f"Total records: {len(df)}")
```

## 筆記本範例
<a name="SECTION_program-with-jupyter-example"></a>

 [使用 Aurora DSQL 的範例筆記本可在 Aurora DSQL 範例儲存庫中使用。](https://github.com/aws-samples/aurora-dsql-samples/tree/main/python/jupyter/sample.ipynb)

## 深入閱讀
<a name="SECTION_program-with-jupyter-reading"></a>

 [Amazon SageMaker AI 設定文件](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-setup-working-env.html) 

 [適用於 Python 的 Aurora DSQL 連接器](https://github.com/awslabs/aurora-dsql-connectors/tree/main/python/connector) 

 [Pandas 文件](https://pandas.pydata.org/docs/user_guide/index.html) 