Beispiel #1
0
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamples = await client.GetCollectionAsync(database, "temperatureSamples");

            var udf = new UserDefinedFunction
            {
                Id = "region"
                ,
                Body = File.ReadAllText(@"Javascript\udf.Region.js")
            };

            await client.TryDeleteUDF(temperatureSamples.SelfLink, udf.Id);

            var result = await client.CreateUserDefinedFunctionAsync(temperatureSamples.SelfLink, udf);

            var sqlQuerySpec = new SqlQuerySpec {
                QueryText = "SELECT s.Sensor.Id, udf.region(s) as regione FROM s"
            };

            var query = client.CreateDocumentQuery <dynamic>(temperatureSamples.SelfLink, sqlQuerySpec);
            var items = query.ToList();


            Console.ReadLine();
        }
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamples = await client.GetCollectionAsync(database, "temperatureSamples");

            string continuation = null;
            List <DocumentCollection> collections = new List <DocumentCollection>();

            do
            {
                FeedOptions options = new FeedOptions
                {
                    RequestContinuation = continuation,
                    MaxItemCount        = 300
                };

                // var response = await client.ReadDocumentCollectionFeedAsync(database.CollectionsLink, options);
                var response = await client.ReadDocumentFeedAsync(temperatureSamples.DocumentsLink, options);

                foreach (DocumentCollection col in response)
                {
                    collections.Add(col);
                }

                continuation = response.ResponseContinuation;
                Console.WriteLine(response.RequestCharge);
            } while (!String.IsNullOrEmpty(continuation));


            Console.ReadLine();
        }
Beispiel #3
0
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var families = await client.GetCollectionAsync(database, "families1");

            await client.CreateDocumentAsync(families.DocumentsLink, new Family
            {
                Id       = "WakefieldFamily",
                LastName = "Wakefield",
                Parents  = new [] {
                    new Parent {
                        FamilyName = "Wakefield", FirstName = "Robin"
                    },
                    new Parent {
                        FamilyName = "Miller", FirstName = "Ben"
                    }
                },
                Children = new Child[] {
                    new Child
                    {
                        FamilyName = "Merriam",
                        FirstName  = "Jesse",
                        Gender     = "female",
                        Grade      = 8,
                        Pets       = new Pet[] {
                            new Pet {
                                GivenName = "Goofy"
                            },
                            new Pet {
                                GivenName = "Shadow"
                            }
                        }
                    },
                    new Child
                    {
                        FamilyName = "Miller",
                        FirstName  = "Lisa",
                        Gender     = "female",
                        Grade      = 1
                    }
                },
                Address = new Address {
                    State = "NY", County = "Manhattan", City = "NY"
                },
                IsRegistered = false
            });


            var sqlQuerySpec = new SqlQuerySpec {
                // QueryText = "SELECT ts.Location.Name, sc.Thresold FROM sensorClass ts JOIN  sc WHERE ts.Type = 'TemperatureSample' AND sc.Type = 'SensorClass' AND ts.Sensor.SensorClass = sc.SensorClass"
                QueryText = "SELECT f.LastName, c.FirstName FROM f JOIN c IN f.Children"
            };

            var query = client.CreateDocumentQuery <dynamic>(families.DocumentsLink, sqlQuerySpec);
            var items = query.ToList();

            Console.ReadLine();
        }
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamplesNorth = await client.GetCollectionAsync(database, "temperatureSamplesNorth");

            var temperatureSamplesMiddle = await client.GetCollectionAsync(database, "temperatureSamplesMiddle");

            var temperatureSamplesSouth = await client.GetCollectionAsync(database, "temperatureSamplesSouth");

            RangePartitionResolver <string> regionResolver = new RangePartitionResolver <string>(
                "RegionName",
                new Dictionary <Range <string>, string>()
            {
                { new Range <string>("North"), temperatureSamplesNorth.SelfLink },
                { new Range <string>("Middle"), temperatureSamplesMiddle.SelfLink },
                { new Range <string>("South"), temperatureSamplesSouth.SelfLink }
            }
                );

            client.PartitionResolvers[database.SelfLink] = regionResolver;

            //var response = await client.NewTemperatureSampleDocumentAsync(database.SelfLink, "Pordenone", Region.North, 45, 12, Guid.NewGuid(), SensorClass.A, 24);
            //response = await client.NewTemperatureSampleDocumentAsync(database.SelfLink, "Roma", Region.Middle, 45, 12, Guid.NewGuid(), SensorClass.B, 25);
            //response = await client.NewTemperatureSampleDocumentAsync(database.SelfLink, "Napoli", Region.South, 45, 12, Guid.NewGuid(), SensorClass.C, 26);

            var query  = client.CreateDocumentQuery <TemperatureSample>(database.SelfLink, null, "Middle");
            var sample = query.AsEnumerable().FirstOrDefault();


            Console.ReadLine();
        }
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamplesInsert = await client.GetCollectionAsync(database, "temperatureSamplesInsert");

            Document document = await client.NewTemperatureSampleDocumentAsync(temperatureSamplesInsert.SelfLink, "Pordenone", Region.North, 45, 12, Guid.NewGuid(), SensorClass.A, 24);

            //This attachment could be any binary you want to attach. Like images, videos, word documents, pdfs etc. it doesn't matter
            using (FileStream fileStream = new FileStream(@".\Attachments\LoremIpsum.txt", FileMode.Open))
            {
                //Create the attachment
                await client.CreateAttachmentAsync(document.AttachmentsLink, fileStream, new MediaOptions { ContentType = "text/plain", Slug = "LoremIpsum.txt" });
            }

            //Query for document for attachment for attachments
            Attachment attachment = client.CreateAttachmentQuery(document.SelfLink).AsEnumerable().FirstOrDefault();

            //Use DocumentClient to read the Media content
            MediaResponse content = await client.ReadMediaAsync(attachment.MediaLink);

            byte[] bytes = new byte[content.ContentLength];
            await content.Media.ReadAsync(bytes, 0, (int)content.ContentLength);

            string result = Encoding.UTF8.GetString(bytes);

            Console.ReadLine();
        }
Beispiel #6
0
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamplesInsert = await client.GetCollectionAsync(database, "temperatureSamplesInsert");

            var response = await client.NewTemperatureSampleDocumentAsync(temperatureSamplesInsert.SelfLink, "Pordenone", Region.North, 45, 12, Guid.NewGuid(), SensorClass.A, 24);

            Console.ReadLine();
        }
Beispiel #7
0
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamples = await client.GetCollectionAsync(database, "temperatureSamples");

            var trigger = new Trigger
            {
                Id               = "normalize",
                Body             = File.ReadAllText(@"Javascript\trigger.Normalize.js"),
                TriggerOperation = TriggerOperation.Create,
                TriggerType      = TriggerType.Pre
            };

            await client.TryDeleteTrigger(temperatureSamples.SelfLink, trigger.Id);

            trigger = await client.CreateTriggerAsync(temperatureSamples.SelfLink, trigger);


            var requestOptions = new RequestOptions {
                PreTriggerInclude = new List <string> {
                    trigger.Id
                }
            };
            await client.CreateDocumentAsync(temperatureSamples.SelfLink, new
            {
                sensorId           = "53fae456-9afc-4af8-9018-981237430147"
                , degreeValue      = 99
                , locationName     = "Udine2"
                , locationRegion   = 0
                , locationLat      = 45
                , locationLong     = 12
                , calendarDateTime = "08/05/2015"
                , id = Guid.NewGuid().ToString()
            }, requestOptions);

            var results = client.CreateDocumentQuery <Document>(temperatureSamples.SelfLink, "SELECT * FROM temperatureSamples s WHERE s.Location.Name='Udine2'");

            foreach (var result in results)
            {
                Console.WriteLine("{0}", result);
            }

            Console.ReadLine();
        }
Beispiel #8
0
        private static async Task RunDaemonAsync(string locationName, string locationRegion, string locationLongitude, string locationLatitude, string sensorId, string sensorClass)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamples = await client.GetCollectionAsync(database, "temperatureSamples");

            var location = new Location {
                Name = locationName, Region = (Region)Enum.Parse(typeof(Region), locationRegion, true), Longitude = double.Parse(locationLongitude), Latitude = double.Parse(locationLatitude)
            };
            var sensor = new Sensor {
                Id = Guid.Parse(sensorId), Class = (SensorClass)Enum.Parse(typeof(SensorClass), sensorClass, true)
            };
            var random = new Random();

            while (true)
            {
                var response = await client.NewTemperatureSampleDocumentAsync(temperatureSamples.SelfLink, location, sensor, random.NextDouble() * 10 + 15);
            }
        }
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamples = await client.GetCollectionAsync(database, "temperatureSamples");

            temperatureSamples.IndexingPolicy.IncludedPaths.Add(new IndexingPath {
                IndexType = IndexType.Hash
                , Path    = ""
            });

            var sqlQuerySpec = new SqlQuerySpec {
                QueryText = "SELECT * FROM templeratureSamples s WHERE s.Location.Name = 'Pordenone'"
            };

            var query = client.CreateDocumentQuery <TemperatureSample>(temperatureSamples.DocumentsLink, sqlQuerySpec);
            var items = query.ToList();

            Console.ReadLine();
        }
        public static async Task RunAsync(string[] args)
        {
            var client   = DocumentDbExtension.CreateClient();
            var database = await client.GetDatabaseAsync("IoTEvents");

            var temperatureSamples = await client.GetCollectionAsync(database, "temperatureSamples");

            var sp = new StoredProcedure
            {
                Id = "count"
                ,
                Body = File.ReadAllText(@"Javascript\sp.Count.js")
            };

            await client.TryDeleteStoredProcedure(temperatureSamples.SelfLink, sp.Id);

            sp = await client.CreateStoredProcedureAsync(temperatureSamples.SelfLink, sp);

            var sql = "SELECT * FROM temperatureSamples";
            var continuationToken = string.Empty;
            var count             = 0;

            while (true)
            {
                var response = (await client.ExecuteStoredProcedureAsync <dynamic>(sp.SelfLink, sql, continuationToken)).Response;
                count            += (int)response.count;
                continuationToken = (string)response.continuationToken;
                if (string.IsNullOrEmpty(continuationToken))
                {
                    break;
                }
                Console.WriteLine("Partial count: {0}, continuing...", count);
            }

            Console.ReadLine();
        }