Python 用 Amazon QLDBドライバー — クイックスタートチュートリアル - Amazon Quantum 台帳データベース (Amazon QLDB)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Python 用 Amazon QLDBドライバー — クイックスタートチュートリアル

重要

サポート終了通知: 既存のお客様は、07/31/2025 のサポート終了QLDBまで Amazon を使用できます。詳細については、「Amazon Ledger QLDB を Amazon Aurora Postgre に移行するSQL」を参照してください。

このチュートリアルでは、Python 用 Amazon QLDBドライバーの最新バージョンを使用してシンプルなアプリケーションを設定する方法について説明します。このガイドには、ドライバーのインストール手順と、基本的な作成、読み取り、更新、削除 (CRUD) オペレーションの短いコード例が含まれています。これらのオペレーションを完全なサンプルアプリケーションで実行する詳細な例については、「Python チュートリアル」を参照してください。

前提条件

作業を始める前に、次の操作を実行してください。

  1. Python ドライバー用の「前提条件」を完了します (まだ完了していない場合)。これには、 へのサインアップ AWS、開発用のプログラムによるアクセスの付与、Python バージョン 3.6 以降のインストールが含まれます。

  2. quick-start という名前の台帳を作成します。

    台帳の作成方法については、「Amazon QLDB台帳の基本オペレーション」、または「コンソールの開始方法」の「ステップ 1: 新しい台帳を作成する」を参照してください。

ステップ 1: プロジェクトを設定する

まず、Python プロジェクトをセットアップします。

注記

これらのセットアップステップを自動化する機能IDEを持つ を使用する場合は、「」にスキップできますステップ 2: ドライバーを初期化する

  1. アプリケーション用のフォルダを作成します。

    $ mkdir myproject $ cd myproject
  2. PyPI から Python 用QLDBドライバーをインストールするには、次のコマンドを入力しますpip

    $ pip install pyqldb

    ドライバーをインストールすると、AWS SDK for Python (Boto3)Amazon Ion パッケージなどの依存関係もインストールされます。

  3. app.py という名前の新しいファイルを作成します。

    次に、次のステップのコード例を段階的に追加して、いくつかの基本的なCRUDオペレーションを試します。または、 step-by-step チュートリアルをスキップして、完全なアプリケーション を実行することもできます。

ステップ 2: ドライバーを初期化する

quick-start という名前の台帳に接続するドライバーのインスタンスを初期化します。以下のコードを app.py ファイルに追加します。

from pyqldb.config.retry_config import RetryConfig from pyqldb.driver.qldb_driver import QldbDriver # Configure retry limit to 3 retry_config = RetryConfig(retry_limit=3) # Initialize the driver print("Initializing the driver") qldb_driver = QldbDriver("quick-start", retry_config=retry_config)

ステップ 3: テーブルとインデックスを作成する

以下のコード例は、CREATE TABLE および CREATE INDEX ステートメントの実行方法を示しています。

以下のコードを追加して、People という名前のテーブルと、そのテーブルの lastName フィールドのインデックスを作成します。インデックスは、クエリのパフォーマンスを最適化し、オプティミスティック同時実行制御 (OCC) の競合例外を制限するのに役立ちます。

def create_table(transaction_executor): print("Creating a table") transaction_executor.execute_statement("Create TABLE People") def create_index(transaction_executor): print("Creating an index") transaction_executor.execute_statement("CREATE INDEX ON People(lastName)") # Create a table qldb_driver.execute_lambda(lambda executor: create_table(executor)) # Create an index on the table qldb_driver.execute_lambda(lambda executor: create_index(executor))

ステップ 4: ドキュメントを挿入する

次のコード例は、INSERT ステートメントの実行方法を示しています。QLDB は、PartiQLクエリ言語 (SQL互換) と Amazon Ion データ形式 ( のスーパーセット) をサポートしていますJSON。

People テーブルにドキュメントを挿入する以下のコードを追加します。

def insert_documents(transaction_executor, arg_1): print("Inserting a document") transaction_executor.execute_statement("INSERT INTO People ?", arg_1) # Insert a document doc_1 = { 'firstName': "John", 'lastName': "Doe", 'age': 32, } qldb_driver.execute_lambda(lambda x: insert_documents(x, doc_1))

この例では、疑問符 (?) を変数プレースホルダーとして使用して、ドキュメント情報をステートメントに渡します。execute_statement メソッドは、Amazon Ion 型と Python ネイティブ型の両方の値をサポートします。

ヒント

1 つの INSERT ステートメントを使用して複数のドキュメントを挿入するために、次のように型 list のパラメータをステートメントに渡すことができます。

# people is a list transaction_executor.execute_statement("INSERT INTO Person ?", people)

リストを渡すときには、変数プレースホルダー (?) を二重山括弧 (<<...>>) で囲まないでください。マニュアルの PartiQL ステートメントでは、二重山括弧はバッグと呼ばれる順序付けされていないコレクションを表します。

ステップ 5: ドキュメントにクエリを実行する

次のコード例は、SELECT ステートメントの実行方法を示しています。

People テーブルからドキュメントをクエリする以下のコードを追加します。

def read_documents(transaction_executor): print("Querying the table") cursor = transaction_executor.execute_statement("SELECT * FROM People WHERE lastName = ?", 'Doe') for doc in cursor: print(doc["firstName"]) print(doc["lastName"]) print(doc["age"]) # Query the table qldb_driver.execute_lambda(lambda executor: read_documents(executor))

ステップ 6: ドキュメントを更新する

次のコード例は、UPDATE ステートメントの実行方法を示しています。

  1. age42 に更新して People テーブルのドキュメントを更新する以下のコードを追加します。

    def update_documents(transaction_executor, age, lastName): print("Updating the document") transaction_executor.execute_statement("UPDATE People SET age = ? WHERE lastName = ?", age, lastName) # Update the document age = 42 lastName = 'Doe' qldb_driver.execute_lambda(lambda x: update_documents(x, age, lastName))
  2. テーブルを再度クエリして、更新された値を確認します。

    # Query the updated document qldb_driver.execute_lambda(lambda executor: read_documents(executor))
  3. アプリケーションを実行するには、プロジェクトディレクトリから以下のコマンドを入力します。

    $ python app.py

完全なアプリケーションの実行

以下のコード例は、app.py アプリケーションの完全なバージョンです。上のステップを個別に実行する代わりに、このコード例を最初から最後までコピーして実行することもできます。このアプリケーションは、 という名前の台帳に対するいくつかの基本的なCRUDオペレーションを示していますquick-start

注記

このコードを実行する前に、quick-start 台帳に People という名前のアクティブなテーブルが存在しないことを確認してください。

from pyqldb.config.retry_config import RetryConfig from pyqldb.driver.qldb_driver import QldbDriver def create_table(transaction_executor): print("Creating a table") transaction_executor.execute_statement("CREATE TABLE People") def create_index(transaction_executor): print("Creating an index") transaction_executor.execute_statement("CREATE INDEX ON People(lastName)") def insert_documents(transaction_executor, arg_1): print("Inserting a document") transaction_executor.execute_statement("INSERT INTO People ?", arg_1) def read_documents(transaction_executor): print("Querying the table") cursor = transaction_executor.execute_statement("SELECT * FROM People WHERE lastName = ?", 'Doe') for doc in cursor: print(doc["firstName"]) print(doc["lastName"]) print(doc["age"]) def update_documents(transaction_executor, age, lastName): print("Updating the document") transaction_executor.execute_statement("UPDATE People SET age = ? WHERE lastName = ?", age, lastName) # Configure retry limit to 3 retry_config = RetryConfig(retry_limit=3) # Initialize the driver print("Initializing the driver") qldb_driver = QldbDriver("quick-start", retry_config=retry_config) # Create a table qldb_driver.execute_lambda(lambda executor: create_table(executor)) # Create an index on the table qldb_driver.execute_lambda(lambda executor: create_index(executor)) # Insert a document doc_1 = { 'firstName': "John", 'lastName': "Doe", 'age': 32, } qldb_driver.execute_lambda(lambda x: insert_documents(x, doc_1)) # Query the table qldb_driver.execute_lambda(lambda executor: read_documents(executor)) # Update the document age = 42 lastName = 'Doe' qldb_driver.execute_lambda(lambda x: update_documents(x, age, lastName)) # Query the table for the updated document qldb_driver.execute_lambda(lambda executor: read_documents(executor))

完全なアプリケーションを実行するには、プロジェクトディレクトリから以下のコマンドを入力します。

$ python app.py