Beispiel #1
0
        static async Task queryAirportsByTimezoneOffset(string tzOffset)
        {
            // SELECT * FROM c where c.timezone_num = "-5"
            // SELECT c.pk FROM c where c.timezone_num = "-5" offset 1 limit 2
            // SELECT COUNT(1) FROM c     <-- 1459
            // SELECT COUNT(1) FROM c where c.timezone_num = "-5" <-- 522

            // https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-offset-limit

            log("queryAirportsByTimezoneOffset");
            CosmosUtil cu     = new CosmosUtil();
            string     dbname = dbNameEnvVar();
            string     cname  = "airports";
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            string sql = $"select c.name, c.city, c.longitude, c.timezone_num from c where c.timezone_num = '{tzOffset}'";

            List <dynamic> items = await cu.queryDocuments(sql);

            for (int i = 0; i < items.Count; i++)
            {
                Console.WriteLine(items[i]);
            }
            return;
        }
Beispiel #2
0
        static async Task upsertAirports(int maxCount)
        {
            log($"upsertAirports: {maxCount}");

            FileUtil       fsu      = new FileUtil();
            List <Airport> airports = new FileUtil().ReadAirportsCsv();

            log($"airports read from csv file: {airports.Count}");

            CosmosUtil cu     = new CosmosUtil();
            string     dbname = dbNameEnvVar();
            string     cname  = "airports";
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            for (int i = 0; i < airports.Count; i++)
            {
                if (i < maxCount)
                {
                    Airport a = airports[i];
                    Console.WriteLine(a.ToJson());
                    ItemResponse <Airport> response = await cu.upsertAirportDocument(a);

                    log($"status code:    {response.StatusCode}");
                    log($"request charge: {response.RequestCharge}");
                    log($"diagnostics:    {response.Diagnostics}");
                    log($"resource:       {response.Resource}");
                }
            }
            return;
        }
Beispiel #3
0
        static async Task queryAirportsPaginated()
        {
            // SELECT COUNT(1) FROM c  <-- 1459
            // SELECT COUNT(1) FROM c where c.timezone_num = "-5"  <-- 522
            // SELECT c.pk FROM c where c.timezone_num = '-5' order by c.pk offset 140 limit 20

            // https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-offset-limit
            // https://www.postgresql.org/docs/current/queries-limit.html
            // https://github.com/azure/azure-documentdb-datamigrationtool
            //
            // "The rows skipped by an OFFSET clause still have to be computed inside the server;
            //  therefore a large OFFSET can be inefficient."

            log("queryAirportsPaginated");
            CosmosUtil cu     = new CosmosUtil();
            string     dbname = dbNameEnvVar();
            string     cname  = "airports";
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            string predicate = "where c.timezone_num = '-5'";
            int    itemCount = await cu.count(predicate);

            int offset       = 0;
            int itemsPerPage = 20;

            log("itemCount:    " + itemCount);
            log("itemsPerPage: " + itemsPerPage);
            double pagesDouble = ((double)itemCount / (double)itemsPerPage);
            double pagesCount  = Math.Ceiling(pagesDouble);

            log("pagesCount:   " + pagesCount);

            for (int p = 0; p < pagesCount; p++)
            {
                offset = offset + itemsPerPage;
                string sql = $"SELECT c.pk FROM c {predicate} order by c.pk offset {offset} limit {itemsPerPage}";
                log("===");
                log($"page: {p} sql: {sql}");

                List <dynamic> items = await cu.queryDocuments(sql, itemsPerPage, true);

                for (int i = 0; i < items.Count; i++)
                {
                    Console.WriteLine(items[i]);
                }
            }
            return;
        }
Beispiel #4
0
        static async Task queryAirportsByPk(string pk)
        {
            log("queryAirportsByPk");
            CosmosUtil cu     = new CosmosUtil();
            string     dbname = dbNameEnvVar();
            string     cname  = "airports";
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            string sql = $"select * from c where c.pk = '{pk}'";

            List <dynamic> items = await cu.queryDocuments(sql);

            for (int i = 0; i < items.Count; i++)
            {
                Console.WriteLine(items[i]);
            }
            return;
        }
Beispiel #5
0
        static async Task queryAirportsByTimezoneCode(string tzCode)
        {
            log("queryAirportsByTimezoneCode");
            CosmosUtil cu     = new CosmosUtil();
            string     dbname = dbNameEnvVar();
            string     cname  = "airports";
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            string sql = $"select c.name, c.city, c.longitude, c.timezone_num from c where c.TimezoneCode = '{tzCode}'";

            List <dynamic> items = await cu.queryDocuments(sql);

            for (int i = 0; i < items.Count; i++)
            {
                Console.WriteLine(items[i]);
            }
            return;
        }
Beispiel #6
0
        static async Task queryLatestEvents(int limit)
        {
            log("queryLatestEvents");

            CosmosUtil cu     = new CosmosUtil();
            string     dbname = Environment.GetEnvironmentVariable("AZURE_IOT_COSMOSDB_SQLDB_DBNAME");
            string     cname  = "events";
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            string sql = $"select * from c order by c.epoch desc offset 0 limit {limit}";

            List <dynamic> items = await cu.queryDocuments(sql);

            for (int i = 0; i < items.Count; i++)
            {
                Console.WriteLine(items[i]);
            }
            return;
        }
Beispiel #7
0
        static async Task queryEvents(double startEpoch, double endEpoch)
        {
            log($"queryEvents: {startEpoch} to {endEpoch}");

            CosmosUtil cu     = new CosmosUtil();
            string     dbname = Environment.GetEnvironmentVariable("AZURE_IOT_COSMOSDB_SQLDB_DBNAME");
            string     cname  = "events";
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            string sql = $"select * from c where c.epoch >= {startEpoch} and c.epoch < {endEpoch}";

            List <dynamic> items = await cu.queryDocuments(sql);

            for (int i = 0; i < items.Count; i++)
            {
                Console.WriteLine(items[i]);
            }
            return;
        }
Beispiel #8
0
        static async Task upsertAirports(int maxCount, int pauseMs)
        {
            log($"upsertAirports: {maxCount}  pauseMs: {pauseMs}");

            FileUtil       fsu      = new FileUtil();
            List <Airport> airports = new FileUtil().ReadAirportsCsv();

            log($"airports read from csv file: {airports.Count}");

            CosmosUtil cu     = new CosmosUtil();
            string     dbname = Environment.GetEnvironmentVariable("AZURE_IOT_COSMOSDB_SQLDB_DBNAME");
            string     cname  = Environment.GetEnvironmentVariable("AZURE_IOT_COSMOSDB_SQLDB_COLLNAME");
            await cu.setCurrentDatabase(dbname);

            await cu.setCurrentContainer(cname);

            for (int i = 0; i < airports.Count; i++)
            {
                if (i < maxCount)
                {
                    Airport a = airports[i];
                    a.UpdateEpoch();
                    log("===");
                    Console.WriteLine(a.ToJson());
                    ItemResponse <Airport> response = await cu.upsertAirportDocument(a);

                    log("---");
                    log($"status code:    {response.StatusCode}");
                    log($"request charge: {response.RequestCharge}");
                    //log($"diagnostics:    {response.Diagnostics}");
                    //log($"resource:       {response.Resource}");
                    Thread.Sleep(pauseMs);
                }
            }
            log($"airports read from csv file: {airports.Count}");
            return;
        }