public List <T> ScanTable(string tableName, List <DynamodbScanCondition> saScanConditions = null) { var retValue = new List <T>(); ScanResponse response; if (saScanConditions != null) { var filterConditions = new Dictionary <string, Condition>(); foreach (var saCondition in saScanConditions) { List <AttributeValue> val = new List <AttributeValue>(); if (saCondition.Value is List <string> ) { foreach (string value in (List <string>)saCondition.Value) { val.Add(new AttributeValue(value)); } } else { val.Add(new AttributeValue(saCondition.Value.ToString())); } Condition condition = new Condition() { AttributeValueList = val, ComparisonOperator = ComparisonOperator.FindValue(saCondition.Operator.ToString()) }; filterConditions.Add(saCondition.AttributeName, condition); } //var request = new ScanRequest(tableName); Table ThreadTable = Table.LoadTable(client, tableName); //ScanFilter scanFilter = new ScanFilter(); //scanFilter.AddCondition("ForumId", ScanOperator.Equal, 101); //scanFilter.AddCondition("Tags", ScanOperator.Between, "sortkey"); response = client.Scan(tableName, filterConditions); } else { response = client.Scan(tableName, new Dictionary <string, Condition>()); } foreach (var item in response.Items) { retValue.Add(ConvertTableItemToInstance(item)); } return(retValue); }
public DataInfo Get(DataInfo item) { AmazonDynamoDBClient client = DynamoUtilities.InitializeClient(); using (client) { ScanRequest request = CreateScanRequest(item.DataId); if (request.ScanFilter.Count == 0) { throw new InvalidOperationException(ErrorMessages.NoSearchCriteria); } ScanResponse response = client.Scan(request); if (response.Items == null) { string errorMessage = string.Format(ErrorMessages.MisingResponseItem, "Get Data Info"); throw new MissingFieldException(errorMessage); } Dictionary <string, AttributeValue> userData = response.Items[0]; var returnValue = DynamoUtilities.GetItemFromAttributeStore <DataInfo>(userData); return(returnValue); } }
private static void FindProductsForPriceLessThanZero() { Dictionary<string, AttributeValue> lastKeyEvaluated = null; do { var request = new ScanRequest { TableName = "ProductCatalog", Limit = 2, ExclusiveStartKey = lastKeyEvaluated, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { {":val", new AttributeValue { N = "0" }} }, FilterExpression = "Price < :val", ProjectionExpression = "Id, Title, Price" }; var response = client.Scan(request); foreach (Dictionary<string, AttributeValue> item in response.Items) { Console.WriteLine("\nScanThreadTableUsePaging - printing....."); PrintItem(item); } lastKeyEvaluated = response.LastEvaluatedKey; } while (lastKeyEvaluated != null && lastKeyEvaluated.Count != 0); Console.WriteLine("To continue, press Enter"); Console.ReadLine(); }
private static void ScanSegment(int totalSegments, int segment) { Console.WriteLine("*** Starting to Scan Segment {0} of {1} out of {2} total segments ***", segment, tableName, totalSegments); Dictionary <string, AttributeValue> lastEvaluatedKey = null; int totalScannedItemCount = 0; int totalScanRequestCount = 0; do { var request = new ScanRequest { TableName = tableName, Limit = scanItemLimit, ExclusiveStartKey = lastEvaluatedKey, Segment = segment, TotalSegments = totalSegments }; var response = client.Scan(request); lastEvaluatedKey = response.LastEvaluatedKey; totalScanRequestCount++; totalScannedItemCount += response.ScannedCount; foreach (var item in response.Items) { Console.WriteLine("Segment: {0}, Scanned Item with Title: {1}", segment, item["Title"].S); } } while (lastEvaluatedKey.Count != 0); Console.WriteLine("*** Completed Scan Segment {0} of {1}. TotalScanRequestCount: {2}, TotalScannedItemCount: {3} ***", segment, tableName, totalScanRequestCount, totalScannedItemCount); }
public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems) { AmazonDynamoDBConfig config = new AmazonDynamoDBConfig(); config.RegionEndpoint = region; ConfigureClient(config); AmazonDynamoDBClient client = new AmazonDynamoDBClient(creds, config); ScanResponse resp = new ScanResponse(); do { ScanRequest req = new ScanRequest { ExclusiveStartKey = resp.LastEvaluatedKey , Limit = maxItems }; resp = client.Scan(req); CheckError(resp.HttpStatusCode, "200"); foreach (var obj in resp.Items) { AddObject(obj); } }while (resp.LastEvaluatedKey.Count > 0); }
private static void ScanSampleProducts(string tableName) { switch (provider) { case Provider.NOSQL: var request = new ScanRequest { TableName = tableName, }; var response = client.Scan(request); //var result = response.ScanResult; //foreach (Dictionary<string, AttributeValue> item in response.ScanResult.Items) //{ // // Process the result. // PrintItem(item); //} //Console.WriteLine("No. of reads used (by get book item) {0}\n", response.ConsumedCapacity.CapacityUnits); break; case Provider.MSSQL: ArrayList products = new Product().GetProducts(); //foreach (Product item in products) //{ // // Process the result. // PrintItem(item); //} break; } }
private static async Task QueryDB(string lastName, string firstName) { AmazonDynamoDBClient client2 = new AmazonDynamoDBClient(clientRegion); var request = new ScanRequest { TableName = "Employee" }; try { var response = client2.Scan(request); string temp = ""; foreach (Dictionary<string, AttributeValue> item in response.Items) { if (!String.IsNullOrEmpty(lastName) && !String.IsNullOrEmpty(firstName)) { PrintItemByBoth(item, lastName, firstName); } if (!String.IsNullOrEmpty(lastName) && String.IsNullOrEmpty(firstName)) { PrintItemByLastName(item, lastName); } if (String.IsNullOrEmpty(lastName) && !String.IsNullOrEmpty(firstName)) { PrintItemByFirstName(item, firstName); } } } catch (ResourceNotFoundException) { } }
public List <AppreciationCards.Models.Messages> QueryDB(ScanRequest request, AmazonDynamoDBClient raw) { List <AppreciationCards.Models.Messages> allMessages = new List <AppreciationCards.Models.Messages>(); ScanResponse response = null; do { if (response != null) { request.ExclusiveStartKey = response.LastEvaluatedKey; } response = raw.Scan(request); foreach (var item in response.Items) { string DatenTime = item["Date"].N; string format = "yyyyMMddHHmmss"; CultureInfo provider = CultureInfo.InvariantCulture; DateTime result = DateTime.ParseExact(DatenTime, format, provider); var messages = new AppreciationCards.Models.Messages() { FromName = item["From_name"].S, ToName = item["To_name"].S, Content = item["Content"].S, Value = item["Value"].S, Unread = item["Unread"].BOOL, MessageDate = result }; allMessages.Add(messages); } } while (response.LastEvaluatedKey != null && response.LastEvaluatedKey.Count > 0); return(allMessages); }
public IEnumerable <Organization> GetAll() { List <Organization> returnValue = new List <Organization>(); AmazonDynamoDBClient client = DynamoUtilities.InitializeClient(); using (client) { ScanRequest request = CreateScanRequest(); ScanResponse response = client.Scan(request); if (response.Items == null || response.Items.Any() == false) { string errorMessage = string.Format(ErrorMessages.MisingResponseItem, "Get Organization"); throw new MissingFieldException(errorMessage); } foreach (Dictionary <string, AttributeValue> item in response.Items) { Organization organization = DynamoUtilities.GetItemFromAttributeStore <Organization>(item); returnValue.Add(organization); } return(returnValue); } }
internal static List <Document> GetDocumentListFromScan(AmazonDynamoDBClient client, ScanRequest scanRequest) { var table = GetTable(client, scanRequest.TableName); var tableKeys = GetTableKeys(table); var scanResults = client.Scan(scanRequest); var scanResultsList = scanResults.Items; while (scanResults.LastEvaluatedKey != null && scanResults.LastEvaluatedKey.Count > 0) { scanRequest.ExclusiveStartKey = scanResults.LastEvaluatedKey; scanResults = client.Scan(scanRequest); scanResultsList.AddRange(scanResults.Items); } var tableBatchGet = table.CreateBatchGet(); foreach (var item in scanResultsList) { var keyDictionary = new Dictionary <string, DynamoDBEntry>(); foreach (var key in tableKeys) { switch (key.Value) { case DynamoDBEntryType.String: keyDictionary.Add(key.Key, item[key.Key].S); break; case DynamoDBEntryType.Numeric: keyDictionary.Add(key.Key, item[key.Key].N); break; case DynamoDBEntryType.Binary: keyDictionary.Add(key.Key, item[key.Key].B); break; default: keyDictionary.Add(key.Key, item[key.Key].S); break; } } tableBatchGet.AddKey(keyDictionary); } tableBatchGet.Execute(); return(tableBatchGet.Results); }
private static void ScanWithNoReturnedConsumedCapacity(AmazonDynamoDBClient client) { var selectQuery = string.Format("SELECT * FROM GameScores WHERE GameTitle = \"{0}\" WITH (NoReturnedCapacity)", StarshipX); Console.WriteLine("(AmazonDynamoDBClient) Running scan with NoReturnedCapacity :\n\t\t{0}", selectQuery); var response = client.Scan(selectQuery); Debug.Assert(response.Items.Count == 1000); Debug.Assert(response.Items.TrueForAll(i => i["GameTitle"].S == StarshipX)); Debug.Assert(response.ConsumedCapacity == null); }
public async Task TestDDBScanUnsuccessful() #endif { var processor = new Mock <BaseProcessor <Activity> >(); var parent = new Activity("parent").Start(); using (Sdk.CreateTracerProviderBuilder() .SetSampler(new AlwaysOnSampler()) .AddXRayTraceId() .AddAWSInstrumentation() .AddProcessor(processor.Object) .Build()) { var ddb = new AmazonDynamoDBClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1); string requestId = @"fakerequ-esti-dfak-ereq-uestidfakere"; AmazonServiceException amazonServiceException = new AmazonServiceException(); amazonServiceException.StatusCode = System.Net.HttpStatusCode.NotFound; amazonServiceException.RequestId = requestId; CustomResponses.SetResponse(ddb, (request) => { throw amazonServiceException; }); var scan_request = new ScanRequest(); scan_request.TableName = "SampleProduct"; scan_request.AttributesToGet = new List <string>() { "Id", "Name" }; try { #if NET452 ddb.Scan(scan_request); #else await ddb.ScanAsync(scan_request); #endif } catch (AmazonServiceException) { var count = processor.Invocations.Count; Assert.Equal(3, count); Activity awssdk_activity = (Activity)processor.Invocations[2].Arguments[0]; this.ValidateAWSActivity(awssdk_activity, parent); this.ValidateDynamoActivityTags(awssdk_activity); Assert.Equal(requestId, Utils.GetTagValue(awssdk_activity, "aws.requestId")); Assert.Equal(Status.Error.WithDescription("Exception of type 'Amazon.Runtime.AmazonServiceException' was thrown."), awssdk_activity.GetStatus()); Assert.Equal("exception", awssdk_activity.Events.First().Name); } } }
public ResponseWrapper Read(Dictionary <string, AttributeValue> lastEvaluatedKey) { if (mReq is ScanRequest) { ((ScanRequest)mReq).ExclusiveStartKey = lastEvaluatedKey; return(new ResponseWrapper(mDynamoDB.Scan((ScanRequest)mReq))); } if (mReq is QueryRequest) { ((QueryRequest)mReq).ExclusiveStartKey = lastEvaluatedKey; return(new ResponseWrapper(mDynamoDB.Query((QueryRequest)mReq))); } throw new NotImplementedException(); }
private static void BasicScan(AmazonDynamoDBClient client, DynamoDBContext context) { var selectQuery = string.Format("SELECT * FROM GameScores WHERE GameTitle = \"{0}\"", StarshipX); Console.WriteLine("(AmazonDynamoDBClient) Running basic scan :\n\t\t{0}", selectQuery); var response = client.Scan(selectQuery); Debug.Assert(response.Items.Count == 1000); Debug.Assert(response.Items.TrueForAll(i => i["GameTitle"].S == StarshipX)); Console.WriteLine("(DynamoDBContext) Running basic scan :\n\t\t{0}", selectQuery); var gameScores = context.ExecScan <GameScore>(selectQuery).ToArray(); Debug.Assert(gameScores.Count() == 1000); Debug.Assert(gameScores.All(gs => gs.GameTitle == StarshipX)); }
private static void ScanWithScanPageSizeAndSegments(AmazonDynamoDBClient client, DynamoDBContext context) { var selectQuery = string.Format("SELECT * FROM GameScores WHERE GameTitle = \"{0}\" WITH (PageSize(20), Segments(2))", StarshipX); Console.WriteLine("(AmazonDynamoDBClient) Running scan with PageSize and 2 segments :\n\t\t{0}", selectQuery); var response = client.Scan(selectQuery); Debug.Assert(response.Items.Count == 1000); Debug.Assert(response.Items.TrueForAll(i => i["GameTitle"].S == StarshipX)); Console.WriteLine("(DynamoDBContext) Running scan with PageSize and 2 segments :\n\t\t{0}", selectQuery); var gameScores = context.ExecScan <GameScore>(selectQuery).ToArray(); Debug.Assert(gameScores.Count() == 1000); Debug.Assert(gameScores.All(gs => gs.GameTitle == StarshipX)); }
/* Returns the amount of total entries in the database. */ public static int GetSize() { var tempInt = 1; var request = new ScanRequest { TableName = table, }; var response = client.Scan(request); var result = response.ScanResult; foreach (Dictionary <string, AttributeValue> item in response.ScanResult.Items) { tempInt++; } return(tempInt); }
public Block GetNewest() { var request = new ScanRequest { TableName = tablename, }; var response = client.Scan(request); var result = response.Items[response.Items.Count - 1]; Block created = new Block(); created.index = Convert.ToDouble(result["Index"].N); created.previousHash = result["PreviousHash"].S; created.timestamp = Convert.ToDateTime(result["Timestamp"].S); created.data = result["Data"].S; created.hash = result["Hash"].S; return(created); }
virtual public List <Dictionary <string, AttributeValue> > GetImageItems(AmazonDynamoDBClient dynamoDbClient) { try { string tableName = ConfigurationManager.AppSettings["SESSIONTABLE"]; string keyPrefix = ConfigurationManager.AppSettings["PARAM3"]; var scanRequest = new ScanRequest { Select = "ALL_ATTRIBUTES", TableName = tableName }; // If the filter criteria is empty, then don't filter it. if (!String.IsNullOrEmpty(keyPrefix)) { scanRequest.ScanFilter = new Dictionary <string, Condition> { { "Key", new Condition { ComparisonOperator = "BEGINS_WITH", AttributeValueList = new List <AttributeValue> { new AttributeValue { S = keyPrefix } } } } }; } return(dynamoDbClient.Scan(scanRequest).Items); } catch (Exception ex) { _Default.LogMessageToPage("GetImageItems Error: {0}", ex.Message); return(null); } }
private static void SelectSpecificAttributes(AmazonDynamoDBClient client, DynamoDBContext context) { var selectQuery = string.Format("SELECT GameTitle, TopScoreDateTime FROM GameScores WHERE GameTitle = \"{0}\"", StarshipX); Console.WriteLine("(AmazonDynamoDBClient) Running basic scan :\n\t\t{0}", selectQuery); var response = client.Scan(selectQuery); Debug.Assert(response.Items.Count == 1000); Debug.Assert(response.Items.TrueForAll(i => i["GameTitle"].S == StarshipX)); Debug.Assert(response.Items.TrueForAll(i => i.Count == 2)); Console.WriteLine("(DynamoDBContext) Running basic scan :\n\t\t{0}", selectQuery); var gameScores = context.ExecScan <GameScore>(selectQuery).ToArray(); Debug.Assert(gameScores.Count() == 1000); Debug.Assert(gameScores.All(gs => gs.GameTitle == StarshipX)); Debug.Assert(gameScores.All(gs => gs.TopScoreDateTime > default(DateTime) && gs.Wins == 0 && gs.Losses == 0 && gs.TopScore == 0 && String.IsNullOrWhiteSpace(gs.UserId))); }
public void TestDDBScanSuccessful() { var processor = new Mock <BaseProcessor <Activity> >(); var parent = new Activity("parent").Start(); using (Sdk.CreateTracerProviderBuilder() .SetSampler(new AlwaysOnSampler()) .AddXRayTraceId() .AddAWSInstrumentation() .AddProcessor(processor.Object) .Build()) { var ddb = new AmazonDynamoDBClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1); string requestId = @"fakerequ-esti-dfak-ereq-uestidfakere"; CustomResponses.SetResponse(ddb, null, requestId, true); var scan_request = new ScanRequest(); scan_request.TableName = "SampleProduct"; scan_request.AttributesToGet = new List <string>() { "Id", "Name" }; #if NET452 ddb.Scan(scan_request); #else ddb.ScanAsync(scan_request).Wait(); #endif var count = processor.Invocations.Count; Assert.Equal(3, count); Activity awssdk_activity = (Activity)processor.Invocations[2].Arguments[0]; this.ValidateAWSActivity(awssdk_activity, parent); this.ValidateDynamoActivityTags(awssdk_activity); Assert.Equal(Status.Unset, awssdk_activity.GetStatus()); Assert.Equal(requestId, Utils.GetTagValue(awssdk_activity, "aws.requestId")); } }
private List <LastUpdate> GetLastUpdates(AmazonDynamoDBClient client, List <string> deviceIds) { var request = new ScanRequest { TableName = "lastupdate", ScanFilter = new Dictionary <string, Condition> { { "devicename", new Condition() { ComparisonOperator = ComparisonOperator.IN, AttributeValueList = deviceIds.Select(x => new AttributeValue { S = x }).ToList() } } } }; var response = client.Scan(request); var result = LastUpdateMapper.MapFromResponse(response); return(result); }
public void DeleteAllRows(string tableName, string hashKey, string rangeKey) { bool rangeKeyExists = !string.IsNullOrWhiteSpace(rangeKey); using (var dbClient = new AmazonDynamoDBClient()) { var scanRequest = new ScanRequest(tableName) { ProjectionExpression = rangeKeyExists ? $"{hashKey},{rangeKey}" : $"{hashKey}" }; ScanResponse scanResponse; while ((scanResponse = dbClient.Scan(scanRequest)).ScannedCount > 0) { foreach (var item in scanResponse.Items) { var deleteKeys = new Dictionary <string, AttributeValue> { { hashKey, item[hashKey] } }; if (rangeKeyExists) { deleteKeys.Add(rangeKey, item[rangeKey]); } if (deleteRequestList.Count >= maxBatchSize) { BatchDelete(dbClient, tableName, deleteRequestList); } deleteRequestList.Add(new WriteRequest(new DeleteRequest(deleteKeys))); } BatchDelete(dbClient, tableName, deleteRequestList); scanRequest.ExclusiveStartKey = scanResponse.LastEvaluatedKey; } } }
/// <summary> /// This method will use whatever information is supplied to scan the table /// supported fields are: /// CustomerId /// UserName /// Password(Hash) /// EmailAddress /// </summary> /// <param name="searchCriteria"></param> /// <returns></returns> public IEnumerable <UserInfo> Search(UserInfo searchCriteria) { AmazonDynamoDBClient client = DynamoUtilities.InitializeClient(); using (client) { ScanRequest request = CreateScanRequest(searchCriteria); if (request.ScanFilter.Count == 0) { throw new InvalidOperationException(ErrorMessages.NoSearchCriteria); } List <UserInfo> returnValue = new List <UserInfo>(); try { ScanResponse response = client.Scan(request); if (response.Items == null) { string errorMessage = string.Format(ErrorMessages.MisingResponseItem, "Search User"); throw new MissingFieldException(errorMessage); } foreach (Dictionary <string, AttributeValue> item in response.Items) { UserInfo user = DynamoUtilities.GetItemFromAttributeStore <UserInfo>(item); returnValue.Add(user); } } catch (AmazonDynamoDBException ex) { //expected exception, 404 } return(returnValue); } throw new NotImplementedException(); }
} //List of accounts like "125237747044" public static AssumeRoleAWSCredentials GetCredential(string env) { if (GlobalRoleCredentialsCache == null) { GlobalRoleCredentialsCache = new Dictionary <string, AssumeRoleAWSCredentials>(); foreach (var strEnv in AwsCommon.GetEnvironmentList()) { GlobalRoleCredentialsCache.Add(strEnv, null); } } if (GlobalRoleArnCache == null) { GlobalRoleArnCache = new Dictionary <string, string>(); GlobalAccountsCache = new List <string>(); var client = new AmazonDynamoDBClient(GetDynamoDbCredential(), AwsCommon.GetRetionEndpoint("us-east-2")); var request = new ScanRequest("sa_env_accounts"); var response = client.Scan(request); foreach (var item in response.Items) { GlobalRoleArnCache.Add(item["Environment"].S, item["RoleArn"].S + "/" + ConfigurationSettings.AppSettings["Role"]); var account = item["Account"].S; if (!GlobalAccountsCache.Exists(o => o == account)) { GlobalAccountsCache.Add(account); } } } if (GlobalRoleCredentialsCache[env.ToString()] == null) { GlobalRoleCredentialsCache[env.ToString()] = new AssumeRoleAWSCredentials( new StoredProfileAWSCredentials("schoolmessenger"), GlobalRoleArnCache[env.ToString()], "AssumeRole"); } return(GlobalRoleCredentialsCache[env.ToString()]); }
public void Test7ScanItems() { var tableName = "GameHistory"; DeleteTable(tableName); CreateTableWithLSI(tableName); PutDummyItems(tableName); var scanRequest = new ScanRequest { TableName = tableName, Limit = 5, FilterExpression = "SeasonId < :season_id", ExpressionAttributeValues = new Dictionary <string, AttributeValue> { { ":season_id", new AttributeValue { N = "15" } } } }; var scanResponse = Client.Scan(scanRequest); Assert.AreEqual(scanResponse.HttpStatusCode, HttpStatusCode.OK); }
public Results() { var client = new AmazonDynamoDBClient(); //var table = Table.LoadTable(client, "WarshipsScores"); var request = new ScanRequest { TableName = "WarshipsScores", }; var response = client.Scan(request); foreach (Dictionary <string, AttributeValue> item in response.Items) { PrintItem(item); } 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 ? "" : value.S) + (value.N == null ? "" : value.N) ); } Console.WriteLine("************************************************"); } Console.WriteLine(); }
public static long GetItemCount(string tableName) { long itemCount = 0; try { AmazonDynamoDBClient client; using (client = new AmazonDynamoDBClient(MyAWSConfigs.DynamodbRegion)) { Dictionary <string, AttributeValue> lastKeyEvaluated = null; do { ScanRequest scanRequest = new ScanRequest { TableName = tableName, ExclusiveStartKey = lastKeyEvaluated }; ScanResponse scanResponse = client.Scan(scanRequest); itemCount += scanResponse.Count; lastKeyEvaluated = scanResponse.LastEvaluatedKey; }while (lastKeyEvaluated != null && lastKeyEvaluated.Count != 0); } } catch (AmazonDynamoDBException e) { Console.WriteLine("AmazonDynamoDBException: " + e); } catch (Exception e) { Console.WriteLine("Error: " + e); } return(itemCount); }
private ScanResponse ScanData(string lastTimeStamp, string lastKey) { var request = new ScanRequest { TableName = DataTable, Limit = 10, ReturnConsumedCapacity = "TOTAL" }; if (lastKey != null && lastTimeStamp != null) { request.ExclusiveStartKey = new Dictionary <string, AttributeValue> { { "crash_id", new AttributeValue { S = lastKey } }, { "upload_timestamp", new AttributeValue { N = lastTimeStamp } } }; } return(_client.Scan(request)); }
static void Main(string[] args) { // Get an AmazonDynamoDBClient for the local DynamoDB database AmazonDynamoDBClient client = GetLocalClient(); // Get a Table object for the table that you created in Step 1 Table table = GetTableObject(client, "Movies"); if (table == null) { PauseForDebugWindow(); return; } /*----------------------------------------------------------------------- * 4.2a: Call Table.Scan to return the movies released in the 1950's, * displaying title, year, lead actor and lead director. *-----------------------------------------------------------------------*/ ScanFilter filter = new ScanFilter(); filter.AddCondition("year", ScanOperator.Between, new DynamoDBEntry[] { 1950, 1959 }); ScanOperationConfig config = new ScanOperationConfig { AttributesToGet = new List <string> { "year, title, info" }, Filter = filter }; Search search = table.Scan(filter); // Display the movie information returned by this query Console.WriteLine("\n\n Movies released in the 1950's (Document Model):" + "\n--------------------------------------------------"); List <Document> docList = new List <Document>(); Document infoDoc; string movieFormatString = " \"{0}\" ({1})-- lead actor: {2}, lead director: {3}"; do { try { docList = search.GetNextSet(); } catch (Exception ex) { Console.WriteLine("\n Error: Search.GetNextStep failed because: " + ex.Message); break; } foreach (var doc in docList) { infoDoc = doc["info"].AsDocument(); Console.WriteLine(movieFormatString, doc["title"], doc["year"], infoDoc["actors"].AsArrayOfString()[0], infoDoc["directors"].AsArrayOfString()[0]); } } while (!search.IsDone); /*----------------------------------------------------------------------- * 4.2b: Call AmazonDynamoDBClient.Scan to return all movies released * in the 1960's, only downloading the title, year, lead * actor and lead director attributes. *-----------------------------------------------------------------------*/ ScanRequest sRequest = new ScanRequest { TableName = "Movies", ExpressionAttributeNames = new Dictionary <string, string> { { "#yr", "year" } }, ExpressionAttributeValues = new Dictionary <string, AttributeValue> { { ":y_a", new AttributeValue { N = "1960" } }, { ":y_z", new AttributeValue { N = "1969" } }, }, FilterExpression = "#yr between :y_a and :y_z", ProjectionExpression = "#yr, title, info.actors[0], info.directors[0]" }; ScanResponse sResponse; try { sResponse = client.Scan(sRequest); } catch (Exception ex) { Console.WriteLine("\n Error: Low-level scan failed, because: " + ex.Message); PauseForDebugWindow(); return; } // Display the movie information returned by this scan Console.WriteLine("\n\n Movies released in the 1960's (low-level):" + "\n-------------------------------------------"); foreach (Dictionary <string, AttributeValue> item in sResponse.Items) { Dictionary <string, AttributeValue> info = item["info"].M; Console.WriteLine(movieFormatString, item["title"].S, item["year"].N, info["actors"].L[0].S, info["directors"].L[0].S); } }
public void SearchSamples() { RemoveTables(); CreateLSITable(); TableUtils.WaitUntilTableActive("SampleTable", TestClient); { // Create items to put into first table Dictionary <string, AttributeValue> item1 = new Dictionary <string, AttributeValue>(); item1["Author"] = new AttributeValue { S = "Mark Twain" }; item1["Title"] = new AttributeValue { S = "A Connecticut Yankee in King Arthur's Court" }; item1["Pages"] = new AttributeValue { N = "575" }; Dictionary <string, AttributeValue> item2 = new Dictionary <string, AttributeValue>(); item2["Author"] = new AttributeValue { S = "Booker Taliaferro Washington" }; item2["Title"] = new AttributeValue { S = "My Larger Education" }; item2["Pages"] = new AttributeValue { N = "313" }; item2["Year"] = new AttributeValue { N = "1911" }; // Construct write-request for first table List <WriteRequest> sampleTableItems = new List <WriteRequest>(); sampleTableItems.Add(new WriteRequest { PutRequest = new PutRequest { Item = item1 } }); sampleTableItems.Add(new WriteRequest { PutRequest = new PutRequest { Item = item2 } }); AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.BatchWriteItem(new BatchWriteItemRequest { RequestItems = new Dictionary <string, List <WriteRequest> > { { "SampleTable", sampleTableItems } } }); PutSample(); } { #region Query Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item hash-key to be string value "Mark Twain" AttributeValue hashKey = new AttributeValue { S = "Mark Twain" }; // Define query condition to search for range-keys that begin with the string "The Adventures" Condition condition = new Condition { ComparisonOperator = "BEGINS_WITH", AttributeValueList = new List <AttributeValue> { new AttributeValue { S = "The Adventures" } } }; // Create the key conditions from hashKey and condition Dictionary <string, Condition> keyConditions = new Dictionary <string, Condition> { // Hash key condition. ComparisonOperator must be "EQ". { "Author", new Condition { ComparisonOperator = "EQ", AttributeValueList = new List <AttributeValue> { hashKey } } }, // Range key condition { "Title", condition } }; // Define marker variable Dictionary <string, AttributeValue> startKey = null; do { // Create Query request QueryRequest request = new QueryRequest { TableName = "SampleTable", ExclusiveStartKey = startKey, KeyConditions = keyConditions }; // Issue request var result = client.Query(request); // View all returned items List <Dictionary <string, AttributeValue> > items = result.Items; foreach (Dictionary <string, AttributeValue> item in items) { Console.WriteLine("Item:"); foreach (var keyValuePair in item) { Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]", keyValuePair.Key, keyValuePair.Value.S, keyValuePair.Value.N, string.Join(", ", keyValuePair.Value.SS ?? new List <string>()), string.Join(", ", keyValuePair.Value.NS ?? new List <string>())); } } // Set marker variable startKey = result.LastEvaluatedKey; } while (startKey != null && startKey.Count > 0); #endregion } { #region Query Local Secondary Index Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item hash-key to be string value "Mark Twain" AttributeValue hashKey = new AttributeValue { S = "Mark Twain" }; // Define query condition to search for range-keys ("Year", in "YearsIndex") that are less than 1900 Condition condition = new Condition { ComparisonOperator = "LT", AttributeValueList = new List <AttributeValue> { new AttributeValue { N = "1900" } } }; // Create the key conditions from hashKey and condition Dictionary <string, Condition> keyConditions = new Dictionary <string, Condition> { // Hash key condition. ComparisonOperator must be "EQ". { "Author", new Condition { ComparisonOperator = "EQ", AttributeValueList = new List <AttributeValue> { hashKey } } }, // Range key condition { "Year", // Reference the correct range key when using indexes condition } }; // Define marker variable Dictionary <string, AttributeValue> startKey = null; do { // Create Query request QueryRequest request = new QueryRequest { TableName = "SampleTable", ExclusiveStartKey = startKey, KeyConditions = keyConditions, IndexName = "YearsIndex" // Specify the index to query against }; // Issue request var result = client.Query(request); // View all returned items List <Dictionary <string, AttributeValue> > items = result.Items; foreach (Dictionary <string, AttributeValue> item in items) { Console.WriteLine("Item:"); foreach (var keyValuePair in item) { Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]", keyValuePair.Key, keyValuePair.Value.S, keyValuePair.Value.N, string.Join(", ", keyValuePair.Value.SS ?? new List <string>()), string.Join(", ", keyValuePair.Value.NS ?? new List <string>())); } } // Set marker variable startKey = result.LastEvaluatedKey; } while (startKey != null && startKey.Count > 0); #endregion } { #region Scan Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define scan conditions Dictionary <string, Condition> conditions = new Dictionary <string, Condition>(); // Title attribute should contain the string "Adventures" Condition titleCondition = new Condition(); titleCondition.ComparisonOperator = ComparisonOperator.CONTAINS; titleCondition.AttributeValueList.Add(new AttributeValue { S = "Adventures" }); conditions["Title"] = titleCondition; // Pages attributes must be greater-than the numeric value "200" Condition pagesCondition = new Condition(); pagesCondition.ComparisonOperator = ComparisonOperator.GT;; pagesCondition.AttributeValueList.Add(new AttributeValue { N = "200" }); conditions["Pages"] = pagesCondition; // Define marker variable Dictionary <string, AttributeValue> startKey = null; do { // Create Scan request ScanRequest request = new ScanRequest { TableName = "SampleTable", ExclusiveStartKey = startKey, ScanFilter = conditions }; // Issue request ScanResult result = client.Scan(request); // View all returned items List <Dictionary <string, AttributeValue> > items = result.Items; foreach (Dictionary <string, AttributeValue> item in items) { Console.WriteLine("Item:"); foreach (var keyValuePair in item) { Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]", keyValuePair.Key, keyValuePair.Value.S, keyValuePair.Value.N, string.Join(", ", keyValuePair.Value.SS ?? new List <string>()), string.Join(", ", keyValuePair.Value.NS ?? new List <string>())); } } // Set marker variable startKey = result.LastEvaluatedKey; } while (startKey != null && startKey.Count > 0); #endregion } { // Create lots of items to put into first table var table = Amazon.DynamoDBv2.DocumentModel.Table.LoadTable(TestClient, "SampleTable"); var batchWrite = table.CreateBatchWrite(); for (int i = 0; i < 100; i++) { var document = new Amazon.DynamoDBv2.DocumentModel.Document(); document["Author"] = "FakeAuthor" + i; document["Title"] = "Book" + i; document["Pages"] = (180 + i); document["Year"] = 1900 + i; batchWrite.AddDocumentToPut(document); } batchWrite.Execute(); } { #region Parallel Scan Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define scan conditions Dictionary <string, Condition> conditions = new Dictionary <string, Condition>(); // Pages attributes must be greater-than the numeric value "200" Condition pagesCondition = new Condition(); pagesCondition.ComparisonOperator = ComparisonOperator.GT; pagesCondition.AttributeValueList.Add(new AttributeValue { N = "200" }); conditions["Pages"] = pagesCondition; // Setup 10 simultaneous threads, each thread calling Scan operation // with its own segment value. int totalSegments = 10; Parallel.For(0, totalSegments, segment => { // Define marker variable Dictionary <string, AttributeValue> startKey = null; do { // Create Scan request ScanRequest request = new ScanRequest { TableName = "SampleTable", ExclusiveStartKey = startKey, ScanFilter = conditions, // Total segments to split the table into TotalSegments = totalSegments, // Current segment to scan Segment = segment }; // Issue request var result = client.Scan(request); // Write returned items to file string path = string.Format("ParallelScan-{0}-of-{1}.txt", totalSegments, segment); List <Dictionary <string, AttributeValue> > items = result.Items; using (Stream stream = File.OpenWrite(path)) using (StreamWriter writer = new StreamWriter(stream)) { foreach (Dictionary <string, AttributeValue> item in items) { writer.WriteLine("Item:"); foreach (var keyValuePair in item) { writer.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]", keyValuePair.Key, keyValuePair.Value.S, keyValuePair.Value.N, string.Join(", ", keyValuePair.Value.SS ?? new List <string>()), string.Join(", ", keyValuePair.Value.NS ?? new List <string>())); } } } // Set marker variable startKey = result.LastEvaluatedKey; } while (startKey != null && startKey.Count > 0); }); #endregion } }