使用 SDK for Rust 的 DynamoDB 範例 - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

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

使用 SDK for Rust 的 DynamoDB 範例

下列程式碼範例示範如何使用 AWS SDK for Rust 搭配 DynamoDB 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

案例是向您展示如何呼叫服務中的多個函數或與其他 AWS 服務組合來完成特定任務的程式碼範例。

AWS 社群貢獻是由多個團隊所建立和維護的範例 AWS。若要提供意見回饋,請使用連結儲存庫中提供的機制。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

動作

下列程式碼範例示範如何使用 CreateTable

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

pub async fn create_table( client: &Client, table: &str, key: &str, ) -> Result<CreateTableOutput, Error> { let a_name: String = key.into(); let table_name: String = table.into(); let ad = AttributeDefinition::builder() .attribute_name(&a_name) .attribute_type(ScalarAttributeType::S) .build() .map_err(Error::BuildError)?; let ks = KeySchemaElement::builder() .attribute_name(&a_name) .key_type(KeyType::Hash) .build() .map_err(Error::BuildError)?; let pt = ProvisionedThroughput::builder() .read_capacity_units(10) .write_capacity_units(5) .build() .map_err(Error::BuildError)?; let create_table_response = client .create_table() .table_name(table_name) .key_schema(ks) .attribute_definitions(ad) .provisioned_throughput(pt) .send() .await; match create_table_response { Ok(out) => { println!("Added table {} with key {}", table, key); Ok(out) } Err(e) => { eprintln!("Got an error creating table:"); eprintln!("{}", e); Err(Error::unhandled(e)) } } }
  • 如需 API 的詳細資訊,請參閱《適用於 Rust 的AWS SDK API 參考》中的 CreateTable

下列程式碼範例示範如何使用 DeleteItem

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

pub async fn delete_item( client: &Client, table: &str, key: &str, value: &str, ) -> Result<DeleteItemOutput, Error> { match client .delete_item() .table_name(table) .key(key, AttributeValue::S(value.into())) .send() .await { Ok(out) => { println!("Deleted item from table"); Ok(out) } Err(e) => Err(Error::unhandled(e)), } }
  • 如需 API 的詳細資訊,請參閱《適用於 Rust 的AWS SDK API 參考》中的 DeleteItem

下列程式碼範例示範如何使用 DeleteTable

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

pub async fn delete_table(client: &Client, table: &str) -> Result<DeleteTableOutput, Error> { let resp = client.delete_table().table_name(table).send().await; match resp { Ok(out) => { println!("Deleted table"); Ok(out) } Err(e) => Err(Error::Unhandled(e.into())), } }

下列程式碼範例示範如何使用 ListTables

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

pub async fn list_tables(client: &Client) -> Result<Vec<String>, Error> { let paginator = client.list_tables().into_paginator().items().send(); let table_names = paginator.collect::<Result<Vec<_>, _>>().await?; println!("Tables:"); for name in &table_names { println!(" {}", name); } println!("Found {} tables", table_names.len()); Ok(table_names) }

判斷資料表是否存在。

pub async fn table_exists(client: &Client, table: &str) -> Result<bool, Error> { debug!("Checking for table: {table}"); let table_list = client.list_tables().send().await; match table_list { Ok(list) => Ok(list.table_names().contains(&table.into())), Err(e) => Err(e.into()), } }
  • 如需 API 的詳細資訊,請參閱《適用於 Rust 的AWS SDK API 參考》中的 ListTables

下列程式碼範例示範如何使用 PutItem

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

pub async fn add_item(client: &Client, item: Item, table: &String) -> Result<ItemOut, Error> { let user_av = AttributeValue::S(item.username); let type_av = AttributeValue::S(item.p_type); let age_av = AttributeValue::S(item.age); let first_av = AttributeValue::S(item.first); let last_av = AttributeValue::S(item.last); let request = client .put_item() .table_name(table) .item("username", user_av) .item("account_type", type_av) .item("age", age_av) .item("first_name", first_av) .item("last_name", last_av); println!("Executing request [{request:?}] to add item..."); let resp = request.send().await?; let attributes = resp.attributes().unwrap(); let username = attributes.get("username").cloned(); let first_name = attributes.get("first_name").cloned(); let last_name = attributes.get("last_name").cloned(); let age = attributes.get("age").cloned(); let p_type = attributes.get("p_type").cloned(); println!( "Added user {:?}, {:?} {:?}, age {:?} as {:?} user", username, first_name, last_name, age, p_type ); Ok(ItemOut { p_type, age, username, first_name, last_name, }) }
  • 如需 API 的詳細資訊,請參閱《適用於 Rust 的AWS SDK API 參考》中的 PutItem

下列程式碼範例示範如何使用 Query

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

尋找在指定年份製作的電影。

pub async fn movies_in_year( client: &Client, table_name: &str, year: u16, ) -> Result<Vec<Movie>, MovieError> { let results = client .query() .table_name(table_name) .key_condition_expression("#yr = :yyyy") .expression_attribute_names("#yr", "year") .expression_attribute_values(":yyyy", AttributeValue::N(year.to_string())) .send() .await?; if let Some(items) = results.items { let movies = items.iter().map(|v| v.into()).collect(); Ok(movies) } else { Ok(vec![]) } }
  • 如需 API 的詳細資訊,請參閱《適用於 Rust 的AWS SDK API 參考》中的Query

下列程式碼範例示範如何使用 Scan

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

pub async fn list_items(client: &Client, table: &str, page_size: Option<i32>) -> Result<(), Error> { let page_size = page_size.unwrap_or(10); let items: Result<Vec<_>, _> = client .scan() .table_name(table) .limit(page_size) .into_paginator() .items() .send() .collect() .await; println!("Items in table (up to {page_size}):"); for item in items? { println!(" {:?}", item); } Ok(()) }
  • 如需 API 的詳細資訊,請參閱《適用於 Rust 的AWS SDK API 參考》中的Scan

案例

下列程式碼範例示範如何覆寫端點 URL,以連線至 DynamoDB 和 AWS SDK 的本機開發部署。

如需詳細資訊,請參閱 DynamoDB Local

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/// Lists your tables from a local DynamoDB instance by setting the SDK Config's /// endpoint_url and test_credentials. #[tokio::main] async fn main() { tracing_subscriber::fmt::init(); let config = aws_config::defaults(aws_config::BehaviorVersion::latest()) .test_credentials() // DynamoDB run locally uses port 8000 by default. .endpoint_url("http://localhost:8000") .load() .await; let dynamodb_local_config = aws_sdk_dynamodb::config::Builder::from(&config).build(); let client = aws_sdk_dynamodb::Client::from_conf(dynamodb_local_config); let list_resp = client.list_tables().send().await; match list_resp { Ok(resp) => { println!("Found {} tables", resp.table_names().len()); for name in resp.table_names() { println!(" {}", name); } } Err(err) => eprintln!("Failed to list local dynamodb tables: {err:?}"), } }

下列程式碼範例示範如何建立無伺服器應用程式,讓使用者以標籤管理相片。

SDK for Rust

顯示如何開發照片資產管理應用程式,以便使用 Amazon Rekognition 偵測圖片中的標籤,並將其儲存以供日後擷取。

如需完整的原始碼和如何設定及執行的指示,請參閱 GitHub 上的完整範例。

如要深入探索此範例的來源,請參閱 AWS  社群上的文章。

此範例中使用的服務
  • API Gateway

  • DynamoDB

  • Lambda

  • Amazon Rekognition

  • Amazon S3

  • Amazon SNS

以下程式碼範例顯示做法:

  • 透過執行 SELECT 陳述式取得項目。

  • 透過執行 INSERT 陳述式新增項目。

  • 透過執行 UPDATE 陳述式更新項目。

  • 透過執行 DELETE 陳述式刪除項目。

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

async fn make_table( client: &Client, table: &str, key: &str, ) -> Result<(), SdkError<CreateTableError>> { let ad = AttributeDefinition::builder() .attribute_name(key) .attribute_type(ScalarAttributeType::S) .build() .expect("creating AttributeDefinition"); let ks = KeySchemaElement::builder() .attribute_name(key) .key_type(KeyType::Hash) .build() .expect("creating KeySchemaElement"); let pt = ProvisionedThroughput::builder() .read_capacity_units(10) .write_capacity_units(5) .build() .expect("creating ProvisionedThroughput"); match client .create_table() .table_name(table) .key_schema(ks) .attribute_definitions(ad) .provisioned_throughput(pt) .send() .await { Ok(_) => Ok(()), Err(e) => Err(e), } } async fn add_item(client: &Client, item: Item) -> Result<(), SdkError<ExecuteStatementError>> { match client .execute_statement() .statement(format!( r#"INSERT INTO "{}" VALUE {{ "{}": ?, "acount_type": ?, "age": ?, "first_name": ?, "last_name": ? }} "#, item.table, item.key )) .set_parameters(Some(vec![ AttributeValue::S(item.utype), AttributeValue::S(item.age), AttributeValue::S(item.first_name), AttributeValue::S(item.last_name), ])) .send() .await { Ok(_) => Ok(()), Err(e) => Err(e), } } async fn query_item(client: &Client, item: Item) -> bool { match client .execute_statement() .statement(format!( r#"SELECT * FROM "{}" WHERE "{}" = ?"#, item.table, item.key )) .set_parameters(Some(vec![AttributeValue::S(item.value)])) .send() .await { Ok(resp) => { if !resp.items().is_empty() { println!("Found a matching entry in the table:"); println!("{:?}", resp.items.unwrap_or_default().pop()); true } else { println!("Did not find a match."); false } } Err(e) => { println!("Got an error querying table:"); println!("{}", e); process::exit(1); } } } async fn remove_item(client: &Client, table: &str, key: &str, value: String) -> Result<(), Error> { client .execute_statement() .statement(format!(r#"DELETE FROM "{table}" WHERE "{key}" = ?"#)) .set_parameters(Some(vec![AttributeValue::S(value)])) .send() .await?; println!("Deleted item."); Ok(()) } async fn remove_table(client: &Client, table: &str) -> Result<(), Error> { client.delete_table().table_name(table).send().await?; Ok(()) }
  • 如需 API 的詳細資訊,請參閱《適用於 Rust 的AWS SDK API 參考》中的 ExecuteStatement

以下程式碼範例顯示做法:

  • 從 JPG、JPEG 或 PNG 檔案中取得 EXIF 資訊。

  • 將映像檔案上傳至 Amazon S3 儲存貯體。

  • 使用 Amazon Rekognition 識別檔案中的三個主要屬性 (標籤)。

  • 將 EXIF 和標籤資訊新增至區域中的 Amazon DynamoDB 資料表。

SDK for Rust

從 JPG、JPEG 或 PNG 檔案獲取 EXIF 資訊,將映像檔案上傳至 Amazon S3 儲存貯體,使用 Amazon Rekognition 識別三個主要屬性 (Amazon Rekognition 中的標籤),然後將 EXIF 和標籤資訊新增至區域中的 Amazon DynamoDB 資料表中。

如需完整的原始碼和如何設定及執行的指示,請參閱 GitHub 上的完整範例。

此範例中使用的服務
  • DynamoDB

  • Amazon Rekognition

  • Amazon S3

無伺服器範例

下列程式碼範例示範如何實作 Lambda 函數,該函數接收從 DynamoDB 串流接收記錄所觸發的事件。函數會擷取 DynamoDB 承載並記下記錄內容。

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在無伺服器範例儲存庫中設定和執行。

使用 Rust 搭配 Lambda 來使用 DynamoDB 事件。

use lambda_runtime::{service_fn, tracing, Error, LambdaEvent}; use aws_lambda_events::{ event::dynamodb::{Event, EventRecord}, }; // Built with the following dependencies: //lambda_runtime = "0.11.1" //serde_json = "1.0" //tokio = { version = "1", features = ["macros"] } //tracing = { version = "0.1", features = ["log"] } //tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] } //aws_lambda_events = "0.15.0" async fn function_handler(event: LambdaEvent<Event>) ->Result<(), Error> { let records = &event.payload.records; tracing::info!("event payload: {:?}",records); if records.is_empty() { tracing::info!("No records found. Exiting."); return Ok(()); } for record in records{ log_dynamo_dbrecord(record); } tracing::info!("Dynamo db records processed"); // Prepare the response Ok(()) } fn log_dynamo_dbrecord(record: &EventRecord)-> Result<(), Error>{ tracing::info!("EventId: {}", record.event_id); tracing::info!("EventName: {}", record.event_name); tracing::info!("DynamoDB Record: {:?}", record.change ); Ok(()) } #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .with_target(false) .without_time() .init(); let func = service_fn(function_handler); lambda_runtime::run(func).await?; Ok(()) }

下列程式碼範例示範如何實作 Lambda 函數的部分批次回應,以接收來自 DynamoDB 串流的事件。此函數會在回應中報告批次項目失敗,指示 Lambda 稍後重試這些訊息。

SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在無伺服器範例儲存庫中設定和執行。

使用 Rust 搭配 Lambda 報告 DynamoDB 批次項目失敗。

use aws_lambda_events::{ event::dynamodb::{Event, EventRecord, StreamRecord}, streams::{DynamoDbBatchItemFailure, DynamoDbEventResponse}, }; use lambda_runtime::{run, service_fn, Error, LambdaEvent}; /// Process the stream record fn process_record(record: &EventRecord) -> Result<(), Error> { let stream_record: &StreamRecord = &record.change; // process your stream record here... tracing::info!("Data: {:?}", stream_record); Ok(()) } /// Main Lambda handler here... async fn function_handler(event: LambdaEvent<Event>) -> Result<DynamoDbEventResponse, Error> { let mut response = DynamoDbEventResponse { batch_item_failures: vec![], }; let records = &event.payload.records; if records.is_empty() { tracing::info!("No records found. Exiting."); return Ok(response); } for record in records { tracing::info!("EventId: {}", record.event_id); // Couldn't find a sequence number if record.change.sequence_number.is_none() { response.batch_item_failures.push(DynamoDbBatchItemFailure { item_identifier: Some("".to_string()), }); return Ok(response); } // Process your record here... if process_record(record).is_err() { response.batch_item_failures.push(DynamoDbBatchItemFailure { item_identifier: record.change.sequence_number.clone(), }); /* Since we are working with streams, we can return the failed item immediately. Lambda will immediately begin to retry processing from this failed item onwards. */ return Ok(response); } } tracing::info!("Successfully processed {} record(s)", records.len()); Ok(response) } #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) // disable printing the name of the module in every log line. .with_target(false) // disabling time is handy because CloudWatch will add the ingestion time. .without_time() .init(); run(service_fn(function_handler)).await }

AWS 社群貢獻

下列程式碼範例示範如何使用 API Gateway 搭配 Lambda 和 DynamoDB 建置和測試無伺服器應用程式

SDK for Rust

示範如何使用 Rust 開發套件建置和測試由具有 Lambda 和 DynamoDB 的 API Gateway 組成的無伺服器應用程式。

如需完整的原始碼和如何設定及執行的指示,請參閱 GitHub 上的完整範例。

此範例中使用的服務
  • API Gateway

  • DynamoDB

  • Lambda