public IActionResult Index() { #region Initialize AerospikeClient client = new AerospikeClient("127.0.0.1", 3000); // Initialize policy. WritePolicy policy = new WritePolicy(); policy.SetTimeout(50); // 50 millisecond timeout. #endregion #region Write Records // Write single value. Key key = new Key("test", "myset", "mykey"); Bin bin = new Bin("mybin", "myvalue"); client.Put(policy, key, bin); // Write multiple values. Key key2 = new Key("test", "myset", "mykey"); Bin bin1 = new Bin("name", "John"); Bin bin2 = new Bin("age", 25); client.Put(policy, key2, bin1, bin2); #endregion #region Read Records //Reading a Single or Multiple Value Record record = client.Get(policy, key, "name"); if (record != null) { Console.WriteLine("Got name: " + record.GetValue("name")); } Record recordMultiple = client.Get(policy, key); if (recordMultiple != null) { foreach (KeyValuePair <string, object> entry in recordMultiple.bins) { Console.WriteLine("Name=" + entry.Key + " Value=" + entry.Value); } } #endregion #region Delete Record Key keyDel = new Key("test", "myset", "mykey"); client.Delete(policy, keyDel); #endregion #region Cleaning Up //Call Close() when all transactions are finished and the application is ready to shutdown. //The AerospikeClient object can no longer be called after calling Close). client.Close(); #endregion return(View()); }
private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName) { int begin = 4; int end = 7; console.Info("Query for:ns={0} set={1} index={2} bin={3} >= {4} <= {5}", args.ns, args.set, indexName, binName, begin, end); Statement stmt = new Statement(); stmt.SetNamespace(args.ns); stmt.SetSetName(args.set); stmt.SetBinNames(binName); stmt.SetFilter(Filter.Range(binName, begin, end)); ResultSet rs = client.QueryAggregate(null, stmt, "sum_example", "sum_single_bin", Value.Get(binName)); try { int expected = 22; // 4 + 5 + 6 + 7 int count = 0; while (rs.Next()) { object obj = rs.Object; if (obj is long) { long sum = (long)rs.Object; if (expected == (int)sum) { console.Info("Sum matched: value=" + expected); } else { console.Error("Sum mismatch: Expected {0}. Received {1}.", expected, sum); } } else { console.Error("Unexpected return value: " + obj); continue; } count++; } if (count == 0) { console.Error("Query failed. No records returned."); } } finally { rs.Close(); } }
/// <summary> /// Perform operations on a list bin. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasCDTMap) { console.Info("CDT map functions are not supported by the connected Aerospike server."); return; } RunSimpleExample(client, args); RunScoreExample(client, args); }
private static void CreateIndex(AerospikeClient client, string ns, string set, string indexName, string binName) { Console.WriteLine("Create index"); Policy policy = new Policy(); policy.SetTimeout(0); // Do not timeout on index create. IndexTask task = client.CreateIndex(policy, ns, set, indexName, binName, IndexType.NUMERIC); task.Wait(); }
private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName1, string binName2, int size) { console.Info("Write " + size + " records."); for (int i = 1; i <= size; i++) { Key key = new Key(args.ns, args.set, keyPrefix + i); client.Put(args.writePolicy, key, new Bin(binName1, i), new Bin(binName2, i)); } }
} //updateUser private void updateUserUsingOperate(AerospikeClient client, Key userKey, WritePolicy policy, long ts) { // TODO: Initiate operate passing in policy, user record key, .add operation incrementing tweet count, .put operation updating timestamp and .get operation to read the user record // Exercise 6 Console.WriteLine("\nTODO: Initiate operate passing in policy, user record key, .add operation incrementing tweet count, .put operation updating timestamp and .get operation to read the user record"); // TODO: Output most recent tweet count // Exercise 6 Console.WriteLine("\nTODO: Output most recent tweet count"); } //updateUserUsingOperate
private static void AddSingleValue(AerospikeClient client, WritePolicy writePolicy) { var newKey = new Key("test", "myAddSet", "myAddKey"); var counter = new Bin("mybin", 1); client.Add(writePolicy, newKey, counter); Console.WriteLine("Wrote this additional value (or bin): " + newKey); Console.WriteLine(""); }
private static void CloseClient() { if (client != null) { client.Close(); } client = null; writePolicy = null; }
public static String infoAll(AerospikeClient client, String cmd) { StringBuilder results = new StringBuilder(); foreach (Node node in client.Nodes) { results.Append(Info.Request(node.Host.name, node.Host.port, cmd)).Append("\n"); } return(results.ToString()); }
} //createTweet private void updateUser(AerospikeClient client, Key userKey, WritePolicy policy, long ts, int tweetCount) { // TODO: Update tweet count and last tweeted timestamp in the user record // Exercise 2 Console.WriteLine("\nTODO: Update tweet count and last tweeted timestamp in the user record"); // TODO: Update tweet count and last tweeted timestamp in the user record using Operate // Exercise 6 // Console.WriteLine("\nTODO: Update tweet count and last tweeted timestamp in the user record using Operate"); // updateUserUsingOperate(client, userKey, policy, ts); } //updateUser
private void CreateIndex(AerospikeClient client, Arguments args, IndexCollectionType indexType, string indexName, string binName) { console.Info("Create GeoJSON {0} index: ns={1} set={2} index={3} bin={4}", indexType, args.ns, args.set, indexName, binName); Policy policy = new Policy(); policy.timeout = 0; // Do not timeout on index create. IndexTask task = client.CreateIndex(policy, args.ns, args.set, indexName, binName, IndexType.GEO2DSPHERE, indexType); task.Wait(); }
public BenchmarkThreadSync ( Console console, BenchmarkArguments args, BenchmarkShared shared, Example example, AerospikeClient client ) : base(console, args, shared, example) { this.client = client; }
private void WriteRecord(AerospikeClient client, Arguments args, string userKey, string name, string password) { Key key = new Key(args.ns, args.set, userKey); Bin bin1 = new Bin("name", name); Bin bin2 = new Bin("password", password); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin1.name, bin1.value); client.Put(args.writePolicy, key, bin1, bin2); }
private static void WriteMultipleValues(AerospikeClient client, WritePolicy writePolicy, Key key) { var bin0 = new Bin("location", "Oslo"); var bin1 = new Bin("name", "Lynn"); var bin2 = new Bin("age", 42); client.Put(writePolicy, key, bin0, bin1, bin2); Console.WriteLine("Wrote these additional values: " + key + " " + bin0 + " " + bin1 + " " + bin2); Console.WriteLine(""); }
private static void WriteRecords(AerospikeClient client, string ns, string set, string keyPrefix, string binName, int size) { Console.WriteLine("Write " + size + " records."); WritePolicy policy = new WritePolicy(); for (int i = 1; i <= size; i++) { Key key = new Key(ns, set, keyPrefix + i); Bin bin = new Bin(binName, i); client.Put(policy, key, bin); } }
public static void Run() { AerospikeClient client = new AerospikeClient("172.28.128.3", 3000); WriteRecords(client); //BatchExists(client); BatchReads(client); //BatchReadHeaders(client); //BatchReadComplex(client); }
/// <summary> /// Create secondary index and query on list bins. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { string indexName = "qlindex"; string keyPrefix = "qlkey"; string binName = "listbin"; int size = 20; CreateIndex(client, args, indexName, binName); WriteRecords(client, args, keyPrefix, binName, size); RunQuery(client, args, indexName, binName); client.DropIndex(args.policy, args.ns, args.set, indexName); }
private void CreateIndex(AerospikeClient client, Arguments args, string indexName, string binName) { console.Info("Create index: ns={0} set={1} index={2} bin={3}", args.ns, args.set, indexName, binName); Policy policy = new Policy(); policy.timeout = 0; // Do not timeout on index create. IndexTask task = client.CreateIndex(policy, args.ns, args.set, indexName, binName, IndexType.NUMERIC); task.Wait(); }
} //updateUser private void updateUserUsingOperate(AerospikeClient client, Key userKey, WritePolicy policy, long ts) { // TODO: Initiate operate passing in policy, user record key, // .Add operation incrementing tweet count, .Put operation updating timestamp // and .Get operation to read the user record. // Exercise K6 Record record = client.Operate(policy, userKey, Operation.Add(new Bin("tweetcount", 1)), Operation.Put(new Bin("lasttweeted", ts)), Operation.Get()); // TODO: Output the most recent tweetcount // Exercise K6 Console.WriteLine("INFO: The tweet count now is: " + record.GetValue("tweetcount")); } //updateUserUsingOperate
/// <summary> /// Write and read a bin value. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (args.singleBin) { RunSingleBinTest(client, args); } else { RunMultiBinTest(client, args); } RunGetHeaderTest(client, args); }
/// <summary> /// Demonstrate touch command. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "touchkey"); Bin bin = new Bin(args.GetBinName("touchbin"), "touchvalue"); console.Info("Create record with 2 second expiration."); WritePolicy writePolicy = new WritePolicy(); writePolicy.expiration = 2; client.Put(writePolicy, key, bin); console.Info("Touch same record with 5 second expiration."); writePolicy.expiration = 5; Record record = client.Operate(writePolicy, key, Operation.Touch(), Operation.GetHeader()); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, null)); } if (record.expiration == 0) { throw new Exception(string.Format("Failed to get record expiration: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } console.Info("Sleep 3 seconds."); Thread.Sleep(3000); record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } console.Info("Success. Record still exists."); console.Info("Sleep 4 seconds."); Thread.Sleep(4000); record = client.Get(args.policy, key, bin.name); if (record == null) { console.Info("Success. Record expired as expected."); } else { console.Error("Found record when it should have expired."); } }
/// <summary> /// Batch multiple gets in one call to the server. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { string keyPrefix = "batchkey"; string valuePrefix = "batchvalue"; string binName = args.GetBinName("batchbin"); int size = 8; WriteRecords(client, args, keyPrefix, binName, valuePrefix, size); BatchExists(client, args, keyPrefix, size); BatchReads(client, args, keyPrefix, binName, size); BatchReadHeaders(client, args, keyPrefix, size); }
/// <summary> /// Perform operations on a list within a single bin. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasLargeDataTypes) { console.Info("Large set functions are not supported by the connected Aerospike server."); return; } Key key = new Key(args.ns, args.set, "setkey"); string binName = args.GetBinName("setbin"); // Delete record if it already exists. client.Delete(args.writePolicy, key); // Initialize large set operator. Aerospike.Client.LargeSet set = client.GetLargeSet(args.writePolicy, key, binName, null); // Write values. set.Add(Value.Get("setvalue1")); set.Add(Value.Get("setvalue2")); set.Add(Value.Get("setvalue3")); // Verify large set was created with default configuration. IDictionary map = set.GetConfig(); foreach (DictionaryEntry entry in map) { console.Info(entry.Key.ToString() + ',' + entry.Value); } // Remove last value. set.Remove(Value.Get("setvalue3")); int size = set.Size(); if (size != 2) { throw new Exception("Size mismatch. Expected 2 Received " + size); } string received = (string)set.Get(Value.Get("setvalue2")); string expected = "setvalue2"; if (received != null && received.Equals(expected)) { console.Info("Data matched: namespace={0} set={1} key={2} value={3}", key.ns, key.setName, key.userKey, received); } else { console.Error("Data mismatch: Expected {0}. Received {1}.", expected, received); } }
private void Login() { int port = int.Parse(portBox.Text.Trim()); Host[] hosts = Host.ParseHosts(hostBox.Text.Trim(), tlsName, port); string userName = userBox.Text.Trim(); string password = passwordBox.Text.Trim(); ClientPolicy policy = new ClientPolicy(); policy.user = userName; policy.password = password; policy.clusterName = clusterName; policy.failIfNotConnected = true; policy.timeout = 600000; policy.tlsPolicy = tlsPolicy; policy.authMode = authMode; AerospikeClient client = new AerospikeClient(policy, hosts); try { if (userName.Equals("admin") && password.Equals("admin")) { Form form = new PasswordForm(client, userName); form.ShowDialog(); } // Query own user. User user = client.QueryUser(null, userName); if (user != null) { bool admin = user.roles.Contains("user-admin"); // Initialize Global Data Globals.RefreshRoles(client, user, admin); Form form = new AdminForm(client, user, admin); form.Show(); } else { throw new Exception("Failed to find user: " + userName); } } catch (Exception) { client.Close(); throw; } }
/// <summary> /// Write and twice read a bin value, demonstrating record expiration. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { Key key = new Key(args.ns, args.set, "expirekey"); Bin bin = new Bin(args.GetBinName("expirebin"), "expirevalue"); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4} expiration=2", key.ns, key.setName, key.userKey, bin.name, bin.value); // Specify that record expires 2 seconds after it's written. WritePolicy writePolicy = new WritePolicy(); writePolicy.expiration = 2; client.Put(writePolicy, key, bin); // Read the record before it expires, showing it's there. console.Info("Get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey); Record record = client.Get(args.policy, key, bin.name); if (record == null) { throw new Exception(string.Format("Failed to get: namespace={0} set={1} key={2}", key.ns, key.setName, key.userKey)); } object received = record.GetValue(bin.name); string expected = bin.value.ToString(); if (received.Equals(expected)) { console.Info("Get successful: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, received); } else { throw new Exception(string.Format("Expire mismatch: Expected {0}. Received {1}.", expected, received)); } // Read the record after it expires, showing it's gone. console.Info("Sleeping for 3 seconds ..."); Thread.Sleep(3 * 1000); record = client.Get(args.policy, key, bin.name); if (record == null) { console.Info("Expiry successful. Record not found."); } else { console.Error("Found record when it should have expired."); } }
/// <summary> /// Perform operations on a list within a single bin. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasLargeDataTypes) { console.Info("Large list functions are not supported by the connected Aerospike server."); return; } RunSimpleExample(client, args); RunWithDistinctBins(client, args); RunWithSerializedBin(client, args); RunWithDefaultSerializedBin(client, args); }
private static void AddRecords(AerospikeClient client, WritePolicy writePolicy) { const int size = 1024; for (var i = 0; i < size; i++) { var key = new Key("test", "myset", (i + 1)); client.Put(writePolicy, key, new Bin("dots", i + " dots")); } Console.WriteLine("Added " + size + " Records"); Console.WriteLine(""); }
/// <summary> /// Write records individually. /// </summary> private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, string valuePrefix, int size) { for (int i = 1; i <= size; i++) { Key key = new Key(args.ns, args.set, keyPrefix + i); Bin bin = new Bin(binName, valuePrefix + i); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, bin.value); client.Put(args.writePolicy, key, bin); } }
/// <summary> /// Some database calls need to know how the server is configured. /// </summary> protected internal void SetServerSpecific(AerospikeClient client) { Node node = client.Nodes[0]; string featuresFilter = "features"; string namespaceFilter = "namespace/" + ns; Dictionary <string, string> tokens = Info.Request(null, node, featuresFilter, namespaceFilter); string features = tokens[featuresFilter]; hasGeo = false; hasUdf = false; hasCDTList = false; hasCDTMap = false; if (features != null) { string[] list = features.Split(';'); foreach (string s in list) { if (s.Equals("geo")) { hasGeo = true; } else if (s.Equals("udf")) { hasUdf = true; } else if (s.Equals("cdt-list")) { hasCDTList = true; } else if (s.Equals("cdt-map")) { hasCDTMap = true; } } } string namespaceTokens = tokens[namespaceFilter]; if (namespaceTokens == null) { throw new Exception(string.Format("Failed to get namespace info: host={0} namespace={1}", node, ns)); } singleBin = parseBoolean(namespaceTokens, "single-bin"); hasLargeDataTypes = parseBoolean(namespaceTokens, "ldt-enabled"); binName = singleBin ? "" : "demobin"; // Single bin servers don't need a bin name. }
public void Close() { if (client != null) { client.Close(); client = null; } if (asyncClient != null) { asyncClient.Close(); asyncClient = null; } }