Utilizzo delle API DynamoDB Service Level - AWS Mobile SDK

L'SDKAWS Mobile per Xamarin è ora incluso inAWS SDK for .NET. Questa guida fa riferimento alla versione archiviata di Mobile SDK per Xamarin.

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Utilizzo delle API DynamoDB Service Level

Le API di livello di servizio di Dynamo ti consentono di creare, aggiornare ed eliminare tabelle. Puoi anche eseguire operazioni tipiche di creazione, lettura, aggiornamento ed eliminazione (CRUD) sugli item di una tabella utilizzando questa API.

Creazione di un client DynamoDB

Per creare un client DynamoDB

AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials,region);

Operazioni CRUD

Salvataggio di una voce

Per salvare una voce in una tabella DynamoDB:

// Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials,region); // Define item attributes Dictionary<string, AttributeValue> attributes = new Dictionary<string, AttributeValue>(); // Author is hash-key attributes["Author"] = new AttributeValue { S = "Mark Twain" }; attributes["Title"] = new AttributeValue { S = "The Adventures of Tom Sawyer" }; attributes["PageCount"] = new AttributeValue { N = "275" }; attributes["Price"] = new AttributeValue{N = "10.00"}; attributes["Id"] = new AttributeValue{N="10"}; attributes["ISBN"] = new AttributeValue{S="111-1111111"}; // Create PutItem request PutItemRequest request = new PutItemRequest { TableName = "Books", Item = attributes }; // Issue PutItem request var response = await client.PutItemAsync(request);

Recupero di una voce

Per recuperare un articolo:

// Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials,region); Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { N = "10" } } }; // Create GetItem request GetItemRequest request = new GetItemRequest { TableName = "Books", Key = key, }; // Issue request var result = await client.GetItemAsync(request); // View response Console.WriteLine("Item:"); Dictionary<string, AttributeValue> item = result.Item; foreach (var keyValuePair in item) { Console.WriteLine("Author := {0}", item["Author"]); Console.WriteLine("Title := {0}", item["Title"]); Console.WriteLine("Price:= {0}", item["Price"]); Console.WriteLine("PageCount := {0}", item["PageCount"]); }

Aggiornamento di una voce

Per aggiornare una voce:

// Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials,region); Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { N = "10" } } }; // Define attribute updates Dictionary<string, AttributeValueUpdate> updates = new Dictionary<string, AttributeValueUpdate>(); // Add a new string to the item's Genres SS attribute updates["Genres"] = new AttributeValueUpdate() { Action = AttributeAction.ADD, Value = new AttributeValue { SS = new List<string> { "Bildungsroman" } } }; // Create UpdateItem request UpdateItemRequest request = new UpdateItemRequest { TableName = "Books", Key = key, AttributeUpdates = updates }; // Issue request var response = await client.UpdateItemAsync(request);

Eliminazione di una voce

Per eliminare una voce:

// Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials,region); Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { N = "10" } } }; // Create DeleteItem request DeleteItemRequest request = new DeleteItemRequest { TableName = "Books", Key = key }; // Issue request var response = await client.DeleteItemAsync(request);

Esecuzione query e scansione

Per interrogare e recuperare tutti i libri il cui autore è «Mark Twain»:

public void Query(AWSCredentials credentials, RegionEndpoint region) { using(var client = new AmazonDynamoDBClient(credentials, region)) { var queryResponse = await client.QueryAsync(new QueryRequest() { TableName = "Books", IndexName = "Author-Title-index", KeyConditionExpression = "Author = :v_Id", ExpressionAttributeValues = new Dictionary < string, AttributeValue > { { ":v_Id", new AttributeValue { S = "Mark Twain" } } } }); queryResponse.Items.ForEach((i) = > { Console.WriteLine(i["Title"].S); }); } }

Il codice di esempio di scansione riportato di seguito restituisce tutti i libri nella nostra tabella:

public void Scan(AWSCredentials credentials, RegionEndpoint region) { using(var client = new AmazonDynamoDBClient(credentials, region)) { var queryResponse = client.Scan(new ScanRequest() { TableName = "Books" }); queryResponse.Items.ForEach((i) = > { Console.WriteLine(i["Title"].S); }); } }