从 DynamoDB 表中读取项目
您可以使用 Amazon Web Services Management Console、Amazon CLI 或 Amazon SDK 从 DynamoDB 表中读取项目。有关项目的更多信息,请参阅 Amazon DynamoDB 的核心组件。
使用 Amazon SDK 从 DynamoDB 表中读取项目
以下代码示例显示如何使用 Amazon SDK 读取 DynamoDB 表中的项目。
- .NET
-
- Amazon SDK for .NET
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /// <summary> /// Gets information about an existing movie from the table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing information about /// the movie to retrieve.</param> /// <param name="tableName">The name of the table containing the movie.</param> /// <returns>A Dictionary object containing information about the item /// retrieved.</returns> public static async Task<Dictionary<string, AttributeValue>> GetItemAsync(AmazonDynamoDBClient client, Movie newMovie, string tableName) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var request = new GetItemRequest { Key = key, TableName = tableName, }; var response = await client.GetItemAsync(request); return response.Item; }
-
有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 GetItem。
-
- C++
-
- 适用于 C++ 的 SDK
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 //! Get an item from an Amazon DynamoDB table. /*! \sa getItem() \param tableName: The table name. \param partitionKey: The partition key. \param partitionValue: The value for the partition key. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::getItem(const Aws::String &tableName, const Aws::String &partitionKey, const Aws::String &partitionValue, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::GetItemRequest request; // Set up the request. request.SetTableName(tableName); request.AddKey(partitionKey, Aws::DynamoDB::Model::AttributeValue().SetS(partitionValue)); // Retrieve the item's fields and values. const Aws::DynamoDB::Model::GetItemOutcome &outcome = dynamoClient.GetItem(request); if (outcome.IsSuccess()) { // Reference the retrieved fields/values. const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item = outcome.GetResult().GetItem(); if (!item.empty()) { // Output each retrieved field and its value. for (const auto &i: item) std::cout << "Values: " << i.first << ": " << i.second.GetS() << std::endl; } else { std::cout << "No item found with the key " << partitionKey << std::endl; } } else { std::cerr << "Failed to get item: " << outcome.GetError().GetMessage(); } return outcome.IsSuccess(); }
-
有关 API 详细信息,请参阅《Amazon SDK for C++ API 参考》中的 GetItem。
-
- Go
-
- SDK for Go V2
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 // TableBasics encapsulates the Amazon DynamoDB service actions used in the examples. // It contains a DynamoDB service client that is used to act on the specified table. type TableBasics struct { DynamoDbClient *dynamodb.Client TableName string } // GetMovie gets movie data from the DynamoDB table by using the primary composite key // made of title and year. func (basics TableBasics) GetMovie(title string, year int) (Movie, error) { movie := Movie{Title: title, Year: year} response, err := basics.DynamoDbClient.GetItem(context.TODO(), &dynamodb.GetItemInput{ Key: movie.GetKey(), TableName: aws.String(basics.TableName), }) if err != nil { log.Printf("Couldn't get info about %v. Here's why: %v\n", title, err) } else { err = attributevalue.UnmarshalMap(response.Item, &movie) if err != nil { log.Printf("Couldn't unmarshal response. Here's why: %v\n", err) } } return movie, err }
-
有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 GetItem
。
-
- Java
-
- SDK for Java 2.x
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用 DynamoDbClient 从表中获取项目。
public static int queryTable(DynamoDbClient ddb, String tableName, String partitionKeyName, String partitionKeyVal, String partitionAlias) { // Set up an alias for the partition key name in case it's a reserved word. HashMap<String,String> attrNameAlias = new HashMap<String,String>(); attrNameAlias.put(partitionAlias, partitionKeyName); // Set up mapping of the partition name with the value. HashMap<String, AttributeValue> attrValues = new HashMap<>(); attrValues.put(":"+partitionKeyName, AttributeValue.builder() .s(partitionKeyVal) .build()); QueryRequest queryReq = QueryRequest.builder() .tableName(tableName) .keyConditionExpression(partitionAlias + " = :" + partitionKeyName) .expressionAttributeNames(attrNameAlias) .expressionAttributeValues(attrValues) .build(); try { QueryResponse response = ddb.query(queryReq); return response.count(); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return -1; }
-
有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 GetItem。
-
- JavaScript
-
- SDK for JavaScript V3
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 创建客户端。
// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });
创建 DynamoDB 文档客户端。
// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };
从表中获取项目。
import { GetCommand } from "@aws-sdk/lib-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; // Set the parameters. export const params = { TableName: "TABLE_NAME", Key: { primaryKey: "VALUE_1", sortKey: "VALUE_2", }, }; export const getItem = async () => { try { const data = await ddbDocClient.send(new GetCommand(params)); console.log("Success :", data.Item); } catch (err) { console.log("Error", err); } }; getItem();
使用 PartiQL 从表中获取项目。
// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient"; const tableName = process.argv[2]; const movieTitle1 = process.argv[3]; export const run = async (tableName, movieTitle1) => { const params = { Statement: "SELECT * FROM " + tableName + " where title=?", Parameters: [{ S: movieTitle1 }], }; try { const data = await ddbDocClient.send(new ExecuteStatementCommand(params)); for (let i = 0; i < data.Items.length; i++) { console.log( "Success. The query return the following data. Item " + i, data.Items[i].year, data.Items[i].title, data.Items[i].info ); } return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieTitle1);
使用 PartiQL 从表中批量获取项目。
// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[5]; const movieTitle2 = process.argv[6]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2 ) => { const params = { Statements: [ { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }, { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle2 }, { N: movieYear2 }], }, ], }; try { const data = await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); for (let i = 0; i < data.Responses.length; i++) { console.log(data.Responses[i].Item.year); console.log(data.Responses[i].Item.title); } return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, movieYear2, movieTitle2);
-
有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 GetItem。
-
- SDK for JavaScript V2
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 从表中获取项目。
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { TableName: 'TABLE', Key: { 'KEY_NAME': {N: '001'} }, ProjectionExpression: 'ATTRIBUTE_NAME' }; // Call DynamoDB to read the item from the table ddb.getItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Item); } });
使用 DynamoDB 文档客户端从表中获取项目。
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB document client var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'}); var params = { TableName: 'EPISODES_TABLE', Key: {'KEY_NAME': VALUE} }; docClient.get(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Item); } });
-
有关更多信息,请参阅 Amazon SDK for JavaScript 开发人员指南。
-
有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 GetItem。
-
- Kotlin
-
- SDK for Kotlin
-
注意 这是适用于预览版中功能的预发行文档。本文档随时可能更改。
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 suspend fun getSpecificItem(tableNameVal: String, keyName: String, keyVal: String) { val keyToGet = mutableMapOf<String, AttributeValue>() keyToGet[keyName] = AttributeValue.S(keyVal) val request = GetItemRequest { key = keyToGet tableName = tableNameVal } DynamoDbClient { region = "us-east-1" }.use { ddb -> val returnedItem = ddb.getItem(request) val numbersMap = returnedItem.item numbersMap?.forEach { key1 -> println(key1.key) println(key1.value) } } }
-
有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 GetItem
。
-
- PHP
-
- SDK for PHP
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 $movie = $service->getItemByKey($tableName, $key); echo "\nThe movie {$movie['Item']['title']['S']} was released in {$movie['Item']['year']['N']}.\n"; public function getItemByKey(string $tableName, array $key) { return $this->dynamoDbClient->getItem([ 'Key' => $key['Item'], 'TableName' => $tableName, ]); }
-
有关 API 详细信息,请参阅《Amazon SDK for PHP API 参考》中的 GetItem。
-
- Python
-
- 适用于 Python (Boto3) 的 SDK
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 class Movies: """Encapsulates an Amazon DynamoDB table of movie data.""" def __init__(self, dyn_resource): """ :param dyn_resource: A Boto3 DynamoDB resource. """ self.dyn_resource = dyn_resource self.table = None def get_movie(self, title, year): """ Gets movie data from the table for a specific movie. :param title: The title of the movie. :param year: The release year of the movie. :return: The data about the requested movie. """ try: response = self.table.get_item(Key={'year': year, 'title': title}) except ClientError as err: logger.error( "Couldn't get movie %s from table %s. Here's why: %s: %s", title, self.table.name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return response['Item']
-
有关 API 详细信息,请参阅《适用于 Python (Boto3) 的 Amazon SDK API 参考》中的 GetItem。
-
- Ruby
-
- SDK for Ruby
-
注意 在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 # Gets movie data from the table for a specific movie. # # @param title [String] The title of the movie. # @param year [Integer] The release year of the movie. # @return [Hash] The data about the requested movie. def get_movie(title, year) response = @table.get_item(key: {"year" => year, "title" => title}) rescue Aws::Errors::ServiceError => e puts("Couldn't get movie #{title} from table #{@table.name}. Here's why:") puts("\t#{e.code}: #{e.message}") raise else response.item end
-
有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 GetItem。
-
有关更多 DynamoDB 示例,请参阅适用于使用 Amazon SDK 的 DynamoDB 的代码示例。