예: AWS SDK for .NET 하위 수준 API를 사용하여 이진 형식 속성 처리 - Amazon DynamoDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

예: AWS SDK for .NET 하위 수준 API를 사용하여 이진 형식 속성 처리

다음 C# 코드는 이진수 형식 속성의 처리 방법을 나타낸 예제입니다. 이 예제에서는 항목을 Reply 테이블에 추가합니다. 추가된 항목에는 압축 데이터가 저장된 이진수 형식의 속성(ExtendedMessage)이 포함되어 있습니다. 그런 다음 항목을 가져와서 모든 속성 값을 출력합니다. 이해를 돕기 위해 예제에서는 GZipStream 클래스를 사용해 샘플 스트림을 압축하여 ExtendedMessage 속성에 할당한 다음 이후 속성 값을 출력할 때 압축을 풉니다.

DynamoDB에서 테이블 생성 및 코드 예시에 대한 데이터 로드의 단계를 따랐다면 이미 Reply 테이블은 생성되어 있을 것입니다. 이러한 샘플 테이블은 프로그래밍 방식으로 생성할 수도 있습니다. 자세한 설명은 를 사용하여 예제 테이블 생성 및 데이터 업로드 AWS SDK for .NET 섹션을 참조하세요.

다음 예제를 테스트하기 위한 step-by-step 지침은 을 참조하십시오.NET 코드 예시.

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(); } } } }