Beispiel: Behandeln von binären Typattributen mithilfe der AWS SDK for .NET Low-Level-API - Amazon-DynamoDB

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Beispiel: Behandeln von binären Typattributen mithilfe der AWS SDK for .NET Low-Level-API

Das folgende C#-Codebeispiel veranschaulicht den Umgang mit Attributen des Typs Binärwert. Bei dem Beispiel wird der Reply-Tabelle ein Element hinzugefügt. Das Element enthält ein Binärtypattribut (ExtendedMessage), in dem komprimierte Daten gespeichert sind. Daraufhin wird das Element abgerufen und es werden alle Attributwerte gedruckt. Zur Veranschaulichung verwendet das Beispiel die GZipStream-Klasse, um einen Beispiel-Stream zu komprimieren und ihn dem Attribut ExtendedMessage hinzuzufügen, und dekomprimiert den Stream beim Drucken des Attributwerts.

Wenn Sie die Schritte in Erstellen von Tabellen und Laden von Daten für Codebeispiele in DynamoDB ausgeführt haben, haben Sie die Reply-Tabelle bereits erstellt. Darüber hinaus können Sie diese Beispieltabellen programmgesteuert erstellen. Weitere Informationen finden Sie unter Erstellen von Beispieltabellen und Hochladen von Daten mit dem AWS SDK for .NET.

step-by-step Anweisungen zum Testen des folgenden Beispiels finden Sie unter . NETCodebeispiele.

Beispiel
using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; using Amazon.Runtime; namespace com.amazonaws.codesamples { class LowLevelItemBinaryExample { private static string tableName = "Reply"; private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { // Reply table primary key. string replyIdPartitionKey = "Amazon DynamoDB#DynamoDB Thread 1"; string replyDateTimeSortKey = Convert.ToString(DateTime.UtcNow); try { CreateItem(replyIdPartitionKey, replyDateTimeSortKey); RetrieveItem(replyIdPartitionKey, replyDateTimeSortKey); // Delete item. DeleteItem(replyIdPartitionKey, replyDateTimeSortKey); Console.WriteLine("To continue, press Enter"); Console.ReadLine(); } catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } } private static void CreateItem(string partitionKey, string sortKey) { MemoryStream compressedMessage = ToGzipMemoryStream("Some long extended message to compress."); var request = new PutItemRequest { TableName = tableName, Item = new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue { S = partitionKey }}, { "ReplyDateTime", new AttributeValue { S = sortKey }}, { "Subject", new AttributeValue { S = "Binary type " }}, { "Message", new AttributeValue { S = "Some message about the binary type" }}, { "ExtendedMessage", new AttributeValue { B = compressedMessage }} } }; client.PutItem(request); } private static void RetrieveItem(string partitionKey, string sortKey) { var request = new GetItemRequest { TableName = tableName, Key = new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue { S = partitionKey } }, { "ReplyDateTime", new AttributeValue { S = sortKey } } }, ConsistentRead = true }; var response = client.GetItem(request); // Check the response. var attributeList = response.Item; // attribute list in the response. Console.WriteLine("\nPrinting item after retrieving it ............"); PrintItem(attributeList); } private static void DeleteItem(string partitionKey, string sortKey) { var request = new DeleteItemRequest { TableName = tableName, Key = new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue { S = partitionKey } }, { "ReplyDateTime", new AttributeValue { S = sortKey } } } }; var response = client.DeleteItem(request); } private static void PrintItem(Dictionary<string, AttributeValue> attributeList) { foreach (KeyValuePair<string, AttributeValue> kvp in attributeList) { string attributeName = kvp.Key; AttributeValue value = kvp.Value; Console.WriteLine( attributeName + " " + (value.S == null ? "" : "S=[" + value.S + "]") + (value.N == null ? "" : "N=[" + value.N + "]") + (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray()) + "]") + (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray()) + "]") + (value.B == null ? "" : "B=[" + FromGzipMemoryStream(value.B) + "]") ); } Console.WriteLine("************************************************"); } private static MemoryStream ToGzipMemoryStream(string value) { MemoryStream output = new MemoryStream(); using (GZipStream zipStream = new GZipStream(output, CompressionMode.Compress, true)) using (StreamWriter writer = new StreamWriter(zipStream)) { writer.Write(value); } return output; } private static string FromGzipMemoryStream(MemoryStream stream) { using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress)) using (StreamReader reader = new StreamReader(zipStream)) { return reader.ReadToEnd(); } } } }