private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName, string valuePrefix)
        {
            string filter = valuePrefix + 3;

            console.Info("Query for: ns={0} set={1} index={2} bin={3} filter={4}",
                         args.ns, args.set, indexName, binName, filter);

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilter(Filter.Equal(binName, filter));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Key    key    = rs.Key;
                    Record record = rs.Record;
                    string result = (string)record.GetValue(binName);

                    if (result.Equals(filter))
                    {
                        console.Info("Record found: namespace={0} set={1} digest={2} bin={3} value={4}",
                                     key.ns, key.setName, ByteUtil.BytesToHexString(key.digest), binName, result);
                    }
                    else
                    {
                        console.Error("Query mismatch: Expected {0}. Received {1}.", filter, result);
                    }
                    count++;
                }

                if (count == 0)
                {
                    console.Error("Query failed. No records returned.");
                }
            }
            finally
            {
                rs.Close();
            }
        }
        private void RunQuery1(AerospikeClient client, Arguments args, string binName)
        {
            int begin = 10;
            int end   = 40;

            console.Info("Query Predicate: (bin2 > 126 && bin2 <= 140) or (bin2 = 360)");

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);

            // Filter applied on query itself.  Filter can only reference an indexed bin.
            stmt.SetFilter(Filter.Range(binName, begin, end));

            // Predicates are applied on query results on server side.
            // Predicates can reference any bin.
            stmt.SetPredExp(
                PredExp.IntegerBin("bin2"),
                PredExp.IntegerValue(126),
                PredExp.IntegerGreater(),
                PredExp.IntegerBin("bin2"),
                PredExp.IntegerValue(140),
                PredExp.IntegerLessEq(),
                PredExp.And(2),
                PredExp.IntegerBin("bin2"),
                PredExp.IntegerValue(360),
                PredExp.IntegerEqual(),
                PredExp.Or(2)
                );

            RecordSet rs = client.Query(null, stmt);

            try
            {
                while (rs.Next())
                {
                    Record record = rs.Record;
                    console.Info("Record: " + record.ToString());
                }
            }
            finally
            {
                rs.Close();
            }
        }
        private void RunQuery(AerospikeClient client, Arguments args, string binName, string binName2, IndexCollectionType indexType)
        {
            console.Info("Query for: ns={0} set={1} bin={2} {3} within <region>", args.ns, args.set, binName, indexType.ToString());

            StringBuilder rgnsb = GenerateQueryRegion();

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.GeoWithinRegion(binName, indexType, rgnsb.ToString()));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;
                HashSet <string> uniques = new HashSet <string>();

                while (rs.Next())
                {
                    Record record = rs.Record;
                    string val    = record.GetString(binName2);
                    uniques.Add(val);
                    count++;
                }

                if (count != 697)
                {
                    console.Error("Query failed. {0} records expected. {1} returned.", 697, count);
                }
                else if (uniques.Count != 21)
                {
                    console.Error("Query failed. {0} unique records expected. {1} unique returned.", 21, uniques.Count);
                }
                else
                {
                    console.Info("query succeeded with {0} records {1} unique", count, uniques.Count);
                }
            }
            finally
            {
                rs.Close();
            }
        }
Exemplo n.º 4
0
        public Dictionary <string, InstanceInfo> GetInstanceList()
        {
            Dictionary <string, InstanceInfo> instancesInfo = new Dictionary <string, InstanceInfo>();

            var statement = new Statement()
            {
                Namespace = _settings[Keystore.AerospikeKeys.InstanceStore.Namespace],
                SetName   = _settings[Keystore.AerospikeKeys.InstanceStore.Set]
            };

            using (AerospikeClient client = GetClient())
            {
                var records = client.Query(null, statement);

                if (records == null)
                {
                    return(instancesInfo);
                }

                try
                {
                    while (records.Next())
                    {
                        Key    key          = records.Key;
                        Record record       = records.Record;
                        var    instanceInfo = new InstanceInfo()
                        {
                            RegionId        = record.GetUInt(Keystore.AerospikeKeys.InstanceStore.RegionIdBin),
                            ZoneId          = record.GetUInt(Keystore.AerospikeKeys.InstanceStore.ZoneIdBin),
                            InstanceId      = record.GetUInt(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin),
                            UpdateTimestamp = GetTimestamp(record.GetString(Keystore.AerospikeKeys.InstanceStore.TimestampBin))
                        };


                        instancesInfo[instanceInfo.GetUniqueId()] = instanceInfo;
                    }
                }
                finally
                {
                    records.Close();
                }

                return(instancesInfo);
            }
        }
Exemplo n.º 5
0
        private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName)
        {
            int begin = 14;
            int end   = 18;

            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.SetFilters(Filter.Range(binName, begin, end));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Key    key    = rs.Key;
                    Record record = rs.Record;
                    long   result = record.GetLong(binName);

                    console.Info("Record found: namespace={0} set={1} digest={2} bin={3} value={4}",
                                 key.ns, key.setName, ByteUtil.BytesToHexString(key.digest), binName, result);

                    count++;
                }

                if (count != 5)
                {
                    console.Error("Query count mismatch. Expected 5. Received " + count);
                }
            }
            finally
            {
                rs.Close();
            }
        }
        private void RunRadiusQuery(AerospikeClient client, Arguments args, string indexName, string binName)
        {
            double lon    = -122.0;
            double lat    = 37.5;
            double radius = 50000.0;

            console.Info("QueryRadius long=" + lon + " lat= " + lat + " radius=" + radius);

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilter(Filter.GeoWithinRadius(binName, lon, lat, radius));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Key    key    = rs.Key;
                    Record record = rs.Record;
                    string result = record.GetGeoJSON(binName);

                    console.Info("Record found: " + result);
                    count++;
                }

                if (count != 4)
                {
                    console.Error("Query count mismatch. Expected 4. Received " + count);
                }
            }
            finally
            {
                rs.Close();
            }
        }
Exemplo n.º 7
0
        private void RunQuery1(AerospikeClient client, Arguments args, string binName)
        {
            int begin = 10;
            int end   = 40;

            console.Info("Query Predicate: (bin2 > 126 && bin2 <= 140) || (bin2 = 360)");

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);

            // Filter applied on query itself.  Filter can only reference an indexed bin.
            stmt.SetFilter(Filter.Range(binName, begin, end));

            // Predicates are applied on query results on server side.
            // Predicates can reference any bin.
            QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);

            policy.filterExp = Exp.Build(
                Exp.Or(
                    Exp.And(
                        Exp.GT(Exp.IntBin("bin2"), Exp.Val(126)),
                        Exp.LE(Exp.IntBin("bin2"), Exp.Val(140))),
                    Exp.EQ(Exp.IntBin("bin2"), Exp.Val(360))));

            RecordSet rs = client.Query(policy, stmt);

            try
            {
                while (rs.Next())
                {
                    Record record = rs.Record;
                    console.Info("Record: " + record.ToString());
                }
            }
            finally
            {
                rs.Close();
            }
        }
        private void RunQuery2(AerospikeClient client, Arguments args, string binName)
        {
            int begin = 10;
            int end   = 40;

            console.Info("Query Predicate: Record updated on 2017-01-15");
            DateTime beginTime = new DateTime(2017, 1, 15);
            DateTime endTime   = new DateTime(2017, 1, 16);

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilter(Filter.Range(binName, begin, end));
            stmt.SetPredExp(
                PredExp.RecLastUpdate(),
                PredExp.IntegerValue(beginTime),
                PredExp.IntegerGreaterEq(),
                PredExp.RecLastUpdate(),
                PredExp.IntegerValue(endTime),
                PredExp.IntegerLess(),
                PredExp.And(2)
                );

            RecordSet rs = client.Query(null, stmt);

            try
            {
                while (rs.Next())
                {
                    Record record = rs.Record;
                    console.Info("Record: " + record.ToString());
                }
            }
            finally
            {
                rs.Close();
            }
        }
Exemplo n.º 9
0
        private void RunQuery2(AerospikeClient client, Arguments args, string binName)
        {
            int begin = 10;
            int end   = 40;

            console.Info("Query Predicate: Record updated in 2020");
            DateTime beginTime = new DateTime(2020, 1, 1);
            DateTime endTime   = new DateTime(2021, 1, 1);

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilter(Filter.Range(binName, begin, end));

            QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);

            policy.filterExp = Exp.Build(
                Exp.And(
                    Exp.GE(Exp.LastUpdate(), Exp.Val(beginTime)),
                    Exp.LT(Exp.LastUpdate(), Exp.Val(endTime))));

            RecordSet rs = client.Query(policy, stmt);

            try
            {
                while (rs.Next())
                {
                    Record record = rs.Record;
                    console.Info("Record: " + record.ToString());
                }
            }
            finally
            {
                rs.Close();
            }
        }
Exemplo n.º 10
0
        private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName)
        {
            console.Info("Query list bins");

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilter(Filter.Contains(binName, IndexCollectionType.LIST, "905"));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    count++;
                    Key    key    = rs.Key;
                    Record record = rs.Record;
                    IList  list   = record.GetList(binName);

                    console.Info("Record " + count);

                    foreach (string s in list)
                    {
                        console.Info(s);
                    }
                }
            }
            finally
            {
                rs.Close();
            }
        }
Exemplo n.º 11
0
        private void ValidateRecords(AerospikeClient client, Arguments args, string indexName, string binName1, string binName2, int size)
        {
            int begin = 1;
            int end   = size + 100;

            console.Info("Validate records");

            Statement stmt = new Statement();

            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Range(binName1, begin, end));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int[] expectedList = new int[] { 1, 2, 3, 104, 5, 106, 7, 108, -1, 10 };
                int   expectedSize = size - 1;
                int   count        = 0;

                while (rs.Next())
                {
                    Key    key    = rs.Key;
                    Record record = rs.Record;
                    object value1 = null;
                    object value2 = null;

                    record.bins.TryGetValue(binName1, out value1);
                    record.bins.TryGetValue(binName2, out value2);

                    console.Info("Record found: ns={0} set={1} bin1={2} value1={3} bin2={4} value2={5}",
                                 key.ns, key.setName, binName1, value1, binName2, value2);

                    if (value1 == null)
                    {
                        console.Error("Data mismatch. value1 is null");
                        break;
                    }
                    long val1 = (long)value1;

                    if (val1 == 9)
                    {
                        console.Error("Data mismatch. value1 " + val1 + " should not exist");
                        break;
                    }

                    if (val1 == 5)
                    {
                        if (value2 != null)
                        {
                            console.Error("Data mismatch. value2 " + value2 + " should be null");
                            break;
                        }
                    }
                    else
                    {
                        long val2 = (long)value2;

                        if (val1 != expectedList[val2 - 1])
                        {
                            console.Error("Data mismatch. Expected " + expectedList[val2 - 1] + ". Received " + value1);
                            break;
                        }
                    }
                    count++;
                }

                if (count != expectedSize)
                {
                    console.Error("Query count mismatch. Expected " + expectedSize + ". Received " + count);
                }
            }
            finally
            {
                rs.Close();
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Example functions not in use
        /// </summary>
        private void deleteTweets()
        {
            RecordSet rs = null;

            try
            {
                // Get username
                string username;
                Console.WriteLine("\nEnter username:"******"test", "users", username);
                    Record userRecord = client.Get(null, userKey);
                    if (userRecord != null)
                    {
                        WritePolicy wPolicy = new WritePolicy();
                        wPolicy.recordExistsAction = RecordExistsAction.UPDATE;

                        string[]  bins = { "tweet" };
                        Statement stmt = new Statement();
                        stmt.SetNamespace("test");
                        stmt.SetSetName("tweets");
                        stmt.SetIndexName("username_index");
                        stmt.SetBinNames(bins);
                        stmt.SetFilters(Filter.Equal("username", username));

                        Console.WriteLine("\nDeleting " + username + "'s tweet(s):\n");

                        rs = client.Query(null, stmt);
                        while (rs.Next())
                        {
                            Record r = rs.Record;
                            Console.WriteLine(r.GetValue("tweet"));
                            client.Delete(null, rs.Key);
                        }
                        //Update tweetcount and timestamp to reflect this
                        client.Operate(wPolicy, userKey, Operation.Put(new Bin("tweetcount", 0)), Operation.Put(new Bin("lasttweeted", 0)));
                        rs.Close();
                    }
                    else
                    {
                        Console.WriteLine("ERROR: User record not found!");
                    }
                }
                else
                {
                    Console.WriteLine("ERROR: User record not found!");
                }
            }
            finally
            {
                if (rs != null)
                {
                    // Close record set
                    rs.Close();
                }
            }
        }
Exemplo n.º 13
0
        } //updatePasswordUsingUDF

        public void aggregateUsersByTweetCountByRegion()
        {
            RecordSet rs = null;

            //ResultSet asd
            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");

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

                Assembly assembly = Assembly.GetExecutingAssembly();
                Policy   policy   = new Policy();
                policy.SetTimeout(100);
                RegisterTask rtask = client.Register(policy, "aggregationByRegion.lua", "aggregationByRegion.lua", Language.LUA);
                rtask.Wait();

                string[]  bins = { "tweetcount", "region" };
                Statement stmt = new Statement();
                stmt.SetNamespace("test");
                stmt.SetSetName("users");
                stmt.SetIndexName("tweetcount_index");
                stmt.SetBinNames(bins);
                stmt.SetFilter(Filter.Range("tweetcount", min, max));
                stmt.SetAggregateFunction("aggregationByRegion", "sum");

                //QueryAggregateExecutor queryAggregateExecutor = new QueryAggregateExecutor(null, null, stmt);
                //rs = queryAggregateExecutor.ResultSet;

                rs = client.Query(null, stmt);

                //rs = client.QueryAggregate(null, stmt, "aggregationByRegion", "sum");

                if (rs.Next())
                {
                    //Dictionary<object, object> result = (Dictionary<object, object>)rs.Record.;
                    //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
Exemplo n.º 14
0
        } //scanTweetsCallback

        public void queryTweetsByUsername()
        {
            Console.WriteLine("\n********** Query Tweets By Username **********\n");

            // TODO: Create STRING index on username in tweets set
            // Exercise Q1
            // 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", "tweets", "username_index", "username", IndexType.STRING);
            //task.Wait();

            RecordSet rs = null;

            try
            {
                // Get username
                string username;
                Console.WriteLine("\nEnter username:"******"tweet" };

                    // TODO: Create Statement instance
                    // Exercise Q3
                    Statement stmt = new Statement();

                    // TODO: Set namespace on the instance of the Statement
                    // Exercise Q3
                    stmt.SetNamespace("test");

                    // TODO: Set the name of the set on the instance of the Statement
                    // Exercise Q3
                    stmt.SetSetName("tweets");

                    // TODO: Set the name of index on the instance of the Statement
                    // Exercise Q3
                    stmt.SetIndexName("username_index");

                    // TODO: Set the list of bins to retrieve on the instance of the Statement
                    // Exercise Q3
                    stmt.SetBinNames(bins);

                    // TODO: Set the equality Filter on username on the instance of the Statement
                    // Exercise Q3
                    stmt.SetFilters(Filter.Equal("username", username));

                    Console.WriteLine("\nHere's " + username + "'s tweet(s):\n");

                    // TODO: Execute the Query passing null policy and Statement instance
                    // Exercise Q3
                    rs = client.Query(null, stmt);


                    while (rs.Next())
                    {
                        // TODO: Iterate through returned RecordSet and output tweets to the console.
                        // Exercise Q3
                        Record r = rs.Record;
                        Console.WriteLine(r.GetValue("tweet"));
                    }
                }
                else
                {
                    Console.WriteLine("ERROR: User record not found!");
                }
            }
            finally
            {
                // TODO: Close the RecordSet
                // Exercise Q3
                if (rs != null)
                {
                    // Close record set
                    rs.Close();
                }
            }
        } //queryTweetsByUsername