} //updatePasswordUsingUDF public void aggregateUsersByTweetCountByRegion() { ResultSet rs = null; try { int min; int max; Console.WriteLine("\nEnter Min Tweet Count:"); min = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Max Tweet Count:"); max = int.Parse(Console.ReadLine()); // NOTE: UDF registration has been included here for convenience and to demonstrate the syntax. // NOTE: The recommended way of registering UDFs in production env is via AQL string luaDirectory = @"..\..\udf"; LuaConfig.PackagePath = luaDirectory + @"\?.lua"; string filename = "aggregationByRegion.lua"; string path = Path.Combine(luaDirectory, filename); RegisterTask rt = client.Register(null, path, filename, Language.LUA); rt.Wait(); string[] bins = { "tweetcount", "region" }; Statement stmt = new Statement(); stmt.SetNamespace("test"); stmt.SetSetName("users"); stmt.SetIndexName("tweetcount_index"); stmt.SetBinNames(bins); stmt.SetFilters(Filter.Range("tweetcount", min, max)); Console.WriteLine("\nAggregating users with " + min + "-" + max + " tweets by region. Hang on...\n"); rs = client.QueryAggregate(null, stmt, "aggregationByRegion", "sum"); if (rs.Next()) { Dictionary <object, object> result = (Dictionary <object, object>)rs.Object; Console.WriteLine("Total Users in North: " + result["n"]); Console.WriteLine("Total Users in South: " + result["s"]); Console.WriteLine("Total Users in East: " + result["e"]); Console.WriteLine("Total Users in West: " + result["w"]); } } finally { if (rs != null) { // Close record set rs.Close(); } } } //aggregateUsersByTweetCountByRegion
private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName) { console.Info("Query for:ns={0} set={1} index={2} bin={3}", args.ns, args.set, indexName, binName); Statement stmt = new Statement(); stmt.SetNamespace(args.ns); stmt.SetSetName(args.set); stmt.SetFilter(Filter.Equal(binName, 1)); ResultSet rs = client.QueryAggregate(null, stmt, "average_example", "average"); try { if (rs.Next()) { object obj = rs.Object; if (obj is Dictionary <object, object> ) { Dictionary <object, object> map = (Dictionary <object, object>)obj; object objsum = map["sum"]; object objcount = map["count"]; double sum = (double)(long)objsum; double count = (double)(long)objcount; double avg = sum / count; console.Info("Sum=" + sum + " Count=" + count + " Average=" + avg); double expected = 5.5; if (avg != expected) { console.Error("Data mismatch: Expected {0}. Received {1}.", expected, avg); } } else { console.Error("Unexpected object returned: " + obj); } } else { console.Error("Query failed. No records returned."); } } finally { rs.Close(); } }
private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName1, string binName2) { StringBuilder rgnsb = new StringBuilder(); rgnsb.Append("{ "); rgnsb.Append(" \"type\": \"Polygon\", "); rgnsb.Append(" \"coordinates\": [ "); rgnsb.Append(" [[-122.500000, 37.000000],[-121.000000, 37.000000], "); rgnsb.Append(" [-121.000000, 38.080000],[-122.500000, 38.080000], "); rgnsb.Append(" [-122.500000, 37.000000]] "); rgnsb.Append(" ] "); rgnsb.Append(" } "); console.Info("QueryRegion: " + rgnsb); string amenStr = "school"; Statement stmt = new Statement(); stmt.SetNamespace(args.ns); stmt.SetSetName(args.set); stmt.SetFilter(Filter.GeoWithinRegion(binName1, rgnsb.ToString())); stmt.SetAggregateFunction("geo_filter_example", "match_amenity", Value.Get(amenStr)); ResultSet rs = client.QueryAggregate(null, stmt); try { int count = 0; while (rs.Next()) { object result = rs.Object; console.Info("Record found: " + result); count++; } if (count != 2) { console.Error("Wrong number of schools found. %d != 2", count); } } finally { rs.Close(); } }
private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName) { string nameFilter = "Bill"; string passFilter = "hknfpkj"; console.Info("Query for: ns=%s set=%s index=%s name=%s pass=%s", args.ns, args.set, indexName, nameFilter, passFilter); Statement stmt = new Statement(); stmt.SetNamespace(args.ns); stmt.SetSetName(args.set); stmt.SetFilters(Filter.Equal(binName, nameFilter)); stmt.SetAggregateFunction("filter_example", "profile_filter", Value.Get(passFilter)); // passFilter will be applied in filter_example.lua. ResultSet rs = client.QueryAggregate(null, stmt); try { int count = 0; while (rs.Next()) { Dictionary <object, object> map = (Dictionary <object, object>)rs.Object; Validate(map, "name", nameFilter); Validate(map, "password", passFilter); count++; } if (count == 0) { console.Error("Query failed. No records returned."); } } finally { rs.Close(); } }
} //updatePasswordUsingCAS public void aggregateUsersByTweetCountByRegion() { // TODO: Create NUMERIC index on tweetcount in users set (Same as Exercise Q4) // Exercise A2 // NOTE: Index creation has been included in here for convenience and to demonstrate the syntax // The recommended way of creating indexes in production env is via AQL // or create once using a standalone application. //IndexTask task = client.CreateIndex(null, "test", "users", "tweetcount_index", "tweetcount", IndexType.NUMERIC); //task.Wait(); ResultSet rs = null; try { int min; int max; Console.WriteLine("\nEnter Min Tweet Count:"); min = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Max Tweet Count:"); max = int.Parse(Console.ReadLine()); // TODO: Register UDF // Exercise A2 // NOTE: UDF registration has been included here for convenience and to demonstrate the syntax. // The recommended way of registering UDFs in production env is via AQL // or standalone application using code similar to below. string luaDirectory = @"..\..\udf"; LuaConfig.PackagePath = luaDirectory + @"\?.lua"; string filename = "aggregationByRegion.lua"; string path = Path.Combine(luaDirectory, filename); RegisterTask rt = client.Register(null, path, filename, Language.LUA); rt.Wait(); // TODO: Create string array of bins that you would like to retrieve // In this example, we want to display which region has how many tweets. // Exercise A2 string[] bins = { "tweetcount", "region" }; // TODO: Create Statement instance // Exercise A2 Statement stmt = new Statement(); // TODO: Set namespace on the instance of the Statement // Exercise A2 stmt.SetNamespace("test"); // TODO: Set the name of the set on the instance of the Statement // Exercise A2 stmt.SetSetName("users"); // TODO: Set the name of index on the instance of the Statement // Exercise A2 stmt.SetIndexName("tweetcount_index"); // TODO: Set the list of bins to retrieve on the instance of the Statement // Exercise A2 stmt.SetBinNames(bins); // TODO: Set the range Filter on tweetcount on the instance of the Statement // Exercise A2 stmt.SetFilters(Filter.Range("tweetcount", min, max)); Console.WriteLine("\nAggregating users with " + min + "-" + max + " tweets by region. Hang on...\n"); // TODO: Execute the Aggregation Query passing null policy and Statement instance, // Lua Module and module function to call. // Exercise A2 rs = client.QueryAggregate(null, stmt, "aggregationByRegion", "sum"); if (rs.Next()) { // TODO: Iterate through returned RecordSet and output text in format "Total Users in <region>: <#>" // Exercise A2 Dictionary <object, object> result = (Dictionary <object, object>)rs.Object; Console.WriteLine("Total Users in North: " + result["n"]); Console.WriteLine("Total Users in South: " + result["s"]); Console.WriteLine("Total Users in East: " + result["e"]); Console.WriteLine("Total Users in West: " + result["w"]); } } finally { // TODO: Close the RecordSet // Exercise A2 if (rs != null) { // Close record set rs.Close(); } } } //aggregateUsersByTweetCountByRegion
} //main public void aggregateUsersByTweetCountByRegion(AerospikeClient client) { ResultSet rs = null; try { int min; int max; Console.WriteLine("\nEnter Min Tweet Count:"); min = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Max Tweet Count:"); max = int.Parse(Console.ReadLine()); Console.WriteLine("\nAggregating users with " + min + "-" + max + " tweets by region. Hang on...\n"); // NOTE: Index creation has been included in here for convenience and to demonstrate the syntax. // NOTE: The recommended way of creating indexes in production env is via AQL. IndexTask task = client.CreateIndex(null, "test", "testusers", "tweetcountindex", "tweetcount", IndexType.NUMERIC); task.Wait(); // NOTE: UDF registration has been included here for convenience and to demonstrate the syntax. // NOTE: The recommended way of registering UDFs in production env is via AQL string luaDirectory = @"..\..\udf"; LuaConfig.PackagePath = luaDirectory + @"\?.lua"; string filename = "aggregationByRegion.lua"; string path = Path.Combine(luaDirectory, filename); RegisterTask rt = client.Register(null, path, filename, Language.LUA); rt.Wait(); Statement stmt = new Statement(); stmt.SetNamespace("test"); stmt.SetSetName("testusers"); stmt.SetIndexName("tweetcountindex"); stmt.SetFilters(Filter.Range("tweetcount", min, max)); rs = client.QueryAggregate(null, stmt, "aggregationByRegion", "sum"); if (rs.Next()) { Dictionary <object, object> result = (Dictionary <object, object>)rs.Object; Console.WriteLine("Here's the breakdown...\n"); Console.WriteLine("Total Users in North: " + result["n"]); Console.WriteLine("Total Users in South: " + result["s"]); Console.WriteLine("Total Users in East: " + result["e"]); Console.WriteLine("Total Users in West: " + result["w"]); } } catch (AerospikeException e) { Console.WriteLine("AerospikeException - Message: " + e.Message); Console.WriteLine("AerospikeException - StackTrace: " + e.StackTrace); } finally { if (rs != null) { // Close record set rs.Close(); } } } //aggregateUsersByTweetCountByRegion