0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DynamoDBのプライマリキーとGetItem操作について

Posted at

DynamoDBのプライマリキーとGetItem操作について

1. パーティションキーのみの場合

テーブル設計

# テーブル定義の例
user_table = dynamodb.Table("Users")
# プライマリキー: userId (パーティションキーのみ)

GetItem操作

# パーティションキーのみを指定
response = user_table.get_item(
    Key={
        "userId": "user123"  # パーティションキーのみ指定
    }
)

2. 複合プライマリキー(パーティションキー + ソートキー)の場合

テーブル設計

# テーブル定義の例
organization_table = dynamodb.Table("Organizations")
# プライマリキー: 
# - globalPartition (パーティションキー)
# - id (ソートキー)

GetItem操作

# パーティションキーとソートキーの両方を指定
response = organization_table.get_item(
    Key={
        "globalPartition": "all",  # パーティションキー
        "id": "org123"            # ソートキー
    }
)

3. 実装例(現在のコードベース)

組織の取得

# 複合プライマリキーを使用した例
db_res = organization_table.get_item(
    Key={
        "globalPartition": "all",  # パーティションキー
        "id": body.id             # ソートキー
    }
)
target_org = db_res.get("Item", None)
if target_org is None: 
    raise ServerException("Organization not found", 404)

4. 重要なポイント

  1. パーティションキーのみの場合

    • GetItemではパーティションキーのみを指定
    • 例:userIdのみを指定
  2. 複合プライマリキーの場合

    • GetItemではパーティションキーとソートキーの両方を指定
    • 例:globalPartitionidの両方を指定
  3. エラーハンドリング

    • キーが存在しない場合の処理が必要
    • 現在のコードではNoneチェックを行っている

5. 実装のベストプラクティス

def get_item_by_partition_key(table, partition_key):
    """パーティションキーのみの場合"""
    try:
        response = table.get_item(
            Key={
                "userId": partition_key
            }
        )
        return response.get("Item")
    except Exception as e:
        raise ServerException(f"Failed to get item: {str(e)}", 500)

def get_item_by_composite_key(table, partition_key, sort_key):
    """複合プライマリキーの場合"""
    try:
        response = table.get_item(
            Key={
                "globalPartition": partition_key,
                "id": sort_key
            }
        )
        return response.get("Item")
    except Exception as e:
        raise ServerException(f"Failed to get item: {str(e)}", 500)

参考

Amazon DynamoDB のプライマリキーについて学ぼう

したがって、GetItem操作を行う際は、テーブルのプライマリキーの設計に応じて、必要なキーをすべて指定する必要があります。パーティションキーのみの場合はパーティションキーだけを、複合プライマリキーの場合はパーティションキーとソートキーの両方を指定する必要があります。

Amazon DynamoDBのGetItem操作について

GetItem

DynamoDBのGetItem操作は、大文字小文字のどちらでも使用可能です:

大文字表記

# 大文字での表記
response = table.GetItem(
    Key={
        "userId": "user123"
    }
)

小文字表記

# 小文字での表記
response = table.get_item(
    Key={
        "userId": "user123"
    }
)

2. 実装例(現在のコードベース)

# 現在のコードベースでは小文字を使用
db_res = organization_table.get_item(
    Key={
        "globalPartition": "all",
        "id": body.id
    }
)

3. 重要なポイント

  1. 表記の一貫性

    • プロジェクトのコーディング規約に従う
    • 現在のコードベースでは小文字のスネークケースを使用
  2. SDKの実装

    • AWS SDKは大文字小文字を区別しない
    • どちらの表記も同じ操作を実行

参考

Amazon DynamoDB API Reference - GetItem

したがって、GetItem操作は大文字小文字のどちらでも使用可能ですが、プロジェクト内での一貫性を保つことが重要です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?