---
title: "Amazon SageMaker Unified StudioでS3のデータを読み込んでみた｜ブログ | STORYAI"
description: STORYAIのユースケースや様々なコンテンツの分析結果、実際にご利用いただいているお客様の声を紹介します。
canonical_url: "https://storyai.app/blog/h718r14jothv9zepuiqcmih4"
last_updated: "2025-03-14T04:35:20.649Z"
meta:
  "og:description": STORYAIのユースケースや様々なコンテンツの分析結果、実際にご利用いただいているお客様の声を紹介します。
  "og:title": "Amazon SageMaker Unified StudioでS3のデータを読み込んでみた｜ブログ"
  "twitter:description": STORYAIのユースケースや様々なコンテンツの分析結果、実際にご利用いただいているお客様の声を紹介します。
  "twitter:title": "Amazon SageMaker Unified StudioでS3のデータを読み込んでみた｜ブログ"
---

Story of STORYAIIP参謀

# **Amazon SageMaker Unified StudioでS3のデータを読み込んでみた**

![avatar](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/thumbnail_IP_51b0553b6b_85774809e0.png)

**hayato**

**2025.03.14**

**共有**

2024年12月初旬に米ラスベガスで行われたre: Inventにて発表された新機能Amazon SageMaker Unified StudioをSTORYAIの環境に実装しテストを行い、その過程を記事に致しましたでの是非お読みください。本番データで検証してはいるものの、諸事情でダミーデータに置き換えたものになっていおります。

この記事は、Amazon SageMaker Unified Studioでデータを取得して分析や機械学習に活用する手順を説明します。

正直、まだ手探りで試している段階のため、全部が正しいとは限りません。  
「こうしたら動いた」というレベルの話も多いため、「あ、こうやるのか」と思ってもらえたら嬉しいです。  
公式ドキュメントや他の情報も参考にしていただきつつ、ぜひ優しく見守ってください。

### 前提条件

- AWSアカウントが作成済みであること。
- SageMaker Unified Studioの準備があること。
- S3バケットにデータが保存されていること。

SageMaker Unified Studioの準備については、以下の記事が参考になります。  
ドメインの作成や、プロジェクトの作成をした後、JupyterLabのノートブックを使えるように準備をしてください。

[**次世代 Amazon SageMaker Unified Studio に触ってみた**](https://blog.usize-tech.com/amazon-sagemaker-unified-studio/)

### ダミーデータの作成

前提条件として記載した「S3バケットにデータが保存されていること」を満たすために、  
以下のダミーデータを用意しました。  
このデータをS3バケットにアップロードして、手順を試してみてください。

#### ダミーCSVファイル (dummy\_data.csv)

```plaintext
**id,name,age,email
1,山田太郎,28,taro.yamada@example.com
2,鈴木花子,34,hanako.suzuki@example.com
3,佐藤次郎,45,jiro.sato@example.com
4,高橋美咲,23,misaki.takahashi@example.com
5,伊藤直樹,39,naoki.ito@example.com
**
```

#### ダミーJSONファイル (dummy\_data.json)

```plaintext
**[
    {
        "id": 1,
        "name": "山田太郎",
        "age": 28,
        "email": "taro.yamada@example.com"
    },
    {
        "id": 2,
        "name": "鈴木花子",
        "age": 34,
        "email": "hanako.suzuki@example.com"
    },
    {
        "id": 3,
        "name": "佐藤次郎",
        "age": 45,
        "email": "jiro.sato@example.com"
    },
    {
        "id": 4,
        "name": "高橋美咲",
        "age": 23,
        "email": "misaki.takahashi@example.com"
    },
    {
        "id": 5,
        "name": "伊藤直樹",
        "age": 39,
        "email": "naoki.ito@example.com"
    }
]**
```

これらのデータをS3バケットにアップロードし、後述の手順で利用してください。

![画像1.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/1_8e58758f6e.png)

### 手順

#### 1. PySparkセッションの作成

PySparkを使用してS3からデータを取得するため、まずはSparkセッションを作成します。

セルのConnection TypeをPySparkとして、以下を実行します。

```plaintext
**from pyspark.sql import SparkSession
**

**spark = SparkSession.builder.getOrCreate()**


```

![画像2.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/2_20624f708a.png)

#### 2. S3からCSVファイルの読み込み

以下のコードで、S3に保存されたCSVファイルを読み込みます。

```plaintext
**df_csv = spark.read.format("csv") **
**
.option("multiLine", "true") **
**
.option("header", "true") **
**
.option("sep", ",") **
**
.load("s3://unifiedstudio-test/testdata/dummy_data.csv")**

**df_csv.show()**


```

"s3://unifiedstudio-test/testdata/dummy\_data.csv"の部分は、データファイルを格納した実際のS3バケット名とファイルパスに置き換えてください。

![画像3.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/3_cec0ad1142.png)

この既存のデータの読み込みの方法がわからずにしばらく悩みました。  
結果的には単純にバケットで指定すればよかったみたいです。  
以下の記事では、AWSがS3で公開しているデータファイルに対してクエリを実行しており、  
ここを自分のS3のパスに入れ替えたところ読み込むことができると気づきました。  
以降のクエリ自体もこの記事を参考にしています。

[**An integrated experience for all your data and AI with Amazon SageMaker Unified Studio (preview)**](https://aws.amazon.com/jp/blogs/big-data/an-integrated-experience-for-all-your-data-and-ai-with-amazon-sagemaker-unified-studio-preview/)

#### 3. データのParquet形式への変換と保存

読み込んだデータをParquet形式に変換し、メタデータの作成とデータのファイルをS3に保存します。

```plaintext
**df_csv.write.format("parquet") **
**
.option("path", "s3://your-bucket-name/path/to/save-directory") **
**
.option("header", True) **
**
.option("compression", "snappy") **
**
.mode("overwrite") **
**
.saveAsTable("testcsv")**
```

"s3://your-bucket-name/path/to/save-directory"の部分は、Parquetファイルが保存されるS3のパスになります。  
プロジェクト作成時に作成したバケットを指定してください。  
ここで保存されるファイルは、データの実体やメタデータなどが含まれており、テーブルごとにパスを分けるのがいいかもしれません。  
"testcsv"は任意のテーブル名に置き換えてください。

![画像4.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/4_0f1914fd67.png)

#### 4. S3からJSONファイルの読み込み

次に、S3に保存されたJSONファイルを読み込みます。

```plaintext
**df_json = spark.read.format("json") **
**
.option("multiLine", "true") **
**
.load("s3://unifiedstudio-test/testdata/dummy_data.json")**

**df_json.show(3, truncate=False)
**


```

"s3://your-bucket-name/path/to/json-files/\*"は、JSONファイルが保存されているディレクトリに置き換えてください。

![画像5.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/5_b4d2776573.png)

#### 6. JSONデータのParquet形式への変換と保存

読み込んだJSONデータをParquet形式に変換し、S3に保存します。

```plaintext
**df_json.write.format("parquet") **
**
.option("path", "s3://your-bucket-name/path/to/save-directory/testjson") **
**
.option("header", True) **
**
.option("compression", "snappy") **
**
.mode("overwrite") **
**
.saveAsTable("testjson")**
```

先程同様、"s3://your-bucket-name/path/to/save-directory"は適切なバケット名、保存先ディレクトリに、"testjson"は任意のテーブル名に置き換えてください。

![画像6.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/6_6f55b3f727.png)

#### 7. データのクエリ実行

保存したテーブルに対して、Spark SQLを使用してクエリを実行します。

```plaintext
**# テーブルのスキーマ情報を表示
spark.sql("DESCRIBE FORMATTED your_table_name").show(truncate=False)**
```

![画像7.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/7_a5efaa4f9e.png)

```plaintext
**# 実行結果
+----------------------------+-------------------------------------------------------------------------------------------------------+-------+
|col_name                    |data_type                                                                                              |comment|
+----------------------------+-------------------------------------------------------------------------------------------------------+-------+
|id                          |string                                                                                                 |NULL   |
|name                        |string                                                                                                 |NULL   |
|age                         |string                                                                                                 |NULL   |
|email                       |string                                                                                                 |NULL   |
|                            |                                                                                                       |       |
|# Detailed Table Information|                                                                                                       |       |
|Catalog                     |spark_catalog                                                                                          |       |
|Database                    |default                                                                                                |       |
|Table                       |testcsv                                                                                                |       |
|Owner                       |hadoop                                                                                                 |       |
|Created Time                |Wed Dec 18 02:44:54 GMT 2024                                                                           |       |
|Last Access                 |UNKNOWN                                                                                                |       |
|Created By                  |Spark 3.5.2-amzn-1                                                                                     |       |
|Type                        |EXTERNAL                                                                                               |       |
|Provider                    |parquet                                                                                                |       |
|Table Properties            |**isRegisteredWithLakeFormation=true**                                                                   |       |
|Location                    |s3://your-bucket-name/path/to/save-directory/testcsv                                                   |       |
|Serde Library               |org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe                                            |       |
|InputFormat                 |org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat                                          |       |
|OutputFormat                |org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat                                         |       |
+----------------------------+-------------------------------------------------------------------------------------------------------+-------+
only showing top 20 rows**

**Connection: project.spark | Run start time: 2024-12-18 03:30:15.433693 | Run duration : 0:00:07.957125s.**


```

```plaintext
**# テーブルのデータを表示
spark.sql("SELECT * FROM testcsv LIMIT 10").show()**
```

![画像8.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/8_7664cfa2d3.png)

セルのConnection Typeを切り替えることで、SQL文などによるデータの取得も可能です。

```plaintext
**# SQL文によるデータの取得（testcsvテーブル）
SELECT * FROM testcsv LIMIT 10**
```

![画像9.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/9_34447f1ff6.png)

```plaintext
**# SQL文によるデータの取得（testcsvテーブル）
SELECT * FROM testcsv LIMIT 10**
```

![画像10.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/10_be820dc708.png)

RedshiftやAthenaとも連携しており、SQLエディタのUIからもデータの検索ができます。  
上部のBuildタブ➡Data Analysis & Integration➡Query Editorと進み、SQLクエリを書き込み、実行してください。

![画像11.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/11_bb2c70d708.png)

### **その他注意点**

- 「とりあえず使ってみた」レベルの情報を提供するものです。
- SageMaker Unified Studioが、Glueを使うための権限やS3への読み書きをする権限が必要になります。  
  私はエラーメッセージを見ながら少しずづ権限を付与しました。  
  完全に検証用のアカウントということでしたら、FullAccess権限をつけていただくと話がはやいはずです。
- Glueを始めてご利用される方はクエリの書き方にご注意ください。予想以上のコストになる場合があります。
- ノートブックでの作業終了後は、SageMaker Unified Studio内でインスタンスの停止と、AWS Glueのインタラクティブセッションを停止させないと、こちらも予想以上のコストになる場合があります。  
  停止し忘れた場合に備え、デフォルトのタイムアウト時間を設定することもできるようです。  
  Glueのコンソールの、Cost management➡Create usage profileと進み、プロファイル（各設定のリスト）を作成し、ユーザーに割り当てることで、割り当てられたユーザーが開始したセッションに設定が適用されます。  
   

![画像12.png](https://storyai-strapi.s3.ap-northeast-1.amazonaws.com/12_6f1c56b805.png)

*プロファイルの設定画面。ここではParameter configurations for sessionsの4項目を設定しています。*

###  

### **SageMakerの機械学習モデルとの連携の可能性**

今回の記事ではデータの取得や保存に焦点を当てましたが、SageMaker Unified Studioの魅力はここからが本番です。取得したデータをそのまま機械学習モデルのトレーニングや推論に活用できるようで、AIを活用したプロジェクトの効率化に大いに貢献すると考えられます。

たとえば、今回作成したParquet形式のデータをSageMaker内のJupyter Notebookに取り込み、モデルをトレーニングしたり、エンドポイントを作成してリアルタイムで推論を行うことが可能です。このように、データ準備からモデル運用までを一貫して実施できるのがSageMaker Unified Studioの大きな特徴です。

今後は、取得したデータを用いた具体的なモデルのトレーニングや、AIの実践的な活用法をさらに掘り下げていきたいと思います。

### **まとめ**

Amazon SageMaker Unified Studioを活用すれば、データの取得から分析、そして機械学習モデルの構築・デプロイまで、一元的に行うことができます。本記事の内容を参考に、ぜひご自身のデータで試してみてください。

ただし、この記事で紹介した内容は執筆時点での試行錯誤の結果に基づいており、正確性やベストプラクティスを保証するものではありません。実際に運用する際は公式ドキュメントを併せて確認し、自身のプロジェクトに最適な方法を見つけてください。

今後は、この基盤をさらに拡張し、SageMakerの機械学習モデルを活用したより実践的なユースケースに挑戦していく予定です。

株式会社ジールは、このようなデータ基盤の構築および運用設計やAIを用いたデータマネジメント等を構築・運用に特化したスペシャリスト集団です。ぜひご用命の際は [**https://www.zdh.co.jp/contact**](https://www.zdh.co.jp/contact) の「 [**製品・サービス・事例に関するお問い合わせ**](https://ma.zdh.co.jp/contact/product/)」からお問い合わせください。

#### **公式ドキュメント**

##### **1 Managing users in Amazon SageMaker Unified Studio **

[**https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/user-management.html**](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/user-management.html) 

\--------------------------------------------------

 By default, Amazon SageMaker platform domains support IAM user credentials. You can also enable access to the Amazon SageMaker platform domains in the Amazon SageMaker Unified Studio for users with SSO and SAML credentials. 

デフォルトでは、Amazon SageMakerプラットフォームドメインはIAMユーザーの資格情報をサポートしています。また、SSOおよびSAML資格情報を持つユーザーのために、Amazon SageMaker Unified StudioでAmazon SageMakerプラットフォームドメインへのアクセスを有効にすることもできます。

\--------------------------------------------------

##### ** 2 What is Amazon SageMaker Unified Studio? - Accessing Amazon SageMaker Unified Studio **

[**https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/what-is-sagemaker-unified-studio.html#acessing-sagemaker-unified-studio**](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/what-is-sagemaker-unified-studio.html#acessing-sagemaker-unified-studio) 

-------------------------------------------------- 

\- Amazon SageMaker Unified Studio Amazon SageMaker Unified Studio is a browser-based web application where you can use all your data and tools for analytics and AI. Amazon SageMaker Unified Studio can authenticate you with your IAM credentials or with credentials from your identity provider through the AWS IAM Identity Center or with your SAML credentials. You can obtain the Amazon SageMaker Unified Studio URL for you domains by accessing the Amazon SageMaker management console at [**https://console.aws.amazon.com/datazone**](https://console.aws.amazon.com/datazone). 

\- Amazon SageMaker Unified Studio Amazon SageMaker Unified Studioは、分析とAIのためのすべてのデータとツールを使用できるブラウザベースのWebアプリケーションです。Amazon SageMaker Unified Studioは、IAM資格情報またはAWS IAMアイデンティティセンターを通じてアイデンティティプロバイダーからの資格情報、またはSAML資格情報を使用して認証できます。Amazon SageMaker Unified StudioのURLは、 [**https://console.aws.amazon.com/datazone**](https://console.aws.amazon.com/datazone) のAmazon SageMaker管理コンソールにアクセスすることで取得できます。

-------------------------------------------------- 

##### **3 Domains in Amazon SageMaker Unified Studio **

[**https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/working-with-domains.html**](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/working-with-domains.html) 

-------------------------------------------------- 

For the Amazon SageMaker platform domains, you can choose either the Quick setup or the Manual setup options. 

Amazon SageMakerプラットフォームのドメインでは、クイックセットアップまたはマニュアルセットアップのオプションを選択できます。

-------------------------------------------------- 

##### **4 Create a Amazon SageMaker Unified Studio domain - manual setup **

[**https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/create-domain-sagemaker-unified-studio-manual.html**](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/adminguide/create-domain-sagemaker-unified-studio-manual.html) 

--- 

##### **5 Configure the IAM permissions required to use the Amazon DataZone data portal - Amazon DataZone **

[**https://docs.aws.amazon.com/datazone/latest/userguide/data-portal-permissions.html**](https://docs.aws.amazon.com/datazone/latest/userguide/data-portal-permissions.html) 

-------------------------------------------------- 

Amazon DataZone data portal (outside the AWS Management Console) is a browser-based web application where users can go to catalog, discover, govern, share, and analyze data in a self-service fashion. The data portal authenticates users with IAM credentials or existing credentials from your identity provider through AWS IAM Identity Center. 

Amazon DataZoneデータポータル（AWS管理コンソール外）は、ユーザーがデータをカタログ化、発見、管理、共有、分析できるブラウザベースのウェブアプリケーションです。このデータポータルは、AWS IAMアイデンティティセンターを通じてIAM資格情報またはアイデンティティプロバイダーからの既存の資格情報でユーザーを認証します。

-------------------------------------------------- 

##### **6 Amazon DataZone コンソールでユーザーを管理する - IAM ロールとユーザーの管理** [**https://docs.aws.amazon.com/ja\_jp/datazone/latest/userguide/user-management-console.html#manage-IAM-users-roles**](https://docs.aws.amazon.com/ja_jp/datazone/latest/userguide/user-management-console.html#manage-IAM-users-roles)

#### <br>**参考記事**

見た限り一番細かく手順を解説している記事

[**An integrated experience for all your data and AI with Amazon SageMaker Unified Studio (preview) | AWS Big Data Blog**](https://aws.amazon.com/jp/blogs/big-data/an-integrated-experience-for-all-your-data-and-ai-with-amazon-sagemaker-unified-studio-preview/)

用語の関係を図解している記事

[**Amazon SageMaker Unified Studio の構成や仕組みをまとめてみた #AWSreInvent | DevelopersIO**](https://dev.classmethod.jp/articles/sagemaker-unified-studio/#unified-studio-%25E3%2581%25AE%25E6%2596%2599%25E9%2587%2591)

AWS Glue インタラクティブセッションとは？

[**AWS Glue インタラクティブセッションを使用したデータ準備 - Amazon SageMaker**](https://docs.aws.amazon.com/ja_jp/sagemaker/latest/dg/studio-notebooks-glue.html)

AWS Glueのデフォルトのタイムアウト時間やDPUの変更について

[**使用プロファイルの作成と管理 - AWS Glue**](https://docs.aws.amazon.com/ja_jp/glue/latest/dg/start-usage-profiles-managing.html)

## **STORYAIを使ってみませんか？**

**　STORYAIは、コンテンツマーケティングの効果を最大限に引き出すことを目的に開発されました。AIによる精度の高い分析と改善案の提示により、質の高いコンテンツ作成をサポートします。従来のマーケティング手法では見落とされがちな課題も、STORYAIなら的確に発見し、解決へと導きます。**

**　まずは、フリーでお試しください。法人様には特別なサポートやコンサルティングをおつけしたサービスをご提供しております。**

[**個人のフリーアカウントはこちら **](https://storyai.app/)

**　また、今後は個人の方向けに、グラフの読み方、カイゼンポイントをセルフで行うためのセミナーも開催していきます。**

**　チャンネル登録はこちらから。 **[~~https://www.youtube.com/@storyai~~](https://www.youtube.com/@storyai)

[**法人様のお問い合わせはこちら **](https://www.zdh.co.jp/storyai-enterprise/#whitepaper_download)

**　自社サービスへの組み込み、AI部分のみAPI連携がしたいなど、まずはご相談ください。御社に適切なプランニングを専任担当から行います。**

[**お問い合わせはこちら **](https://storyai.app/contact)

Story of STORYAIIP参謀

**共有**

**関連記事**

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
