public static void Main(string[] args) { try { using (var client = new DocumentClient( new Uri("https://jdm-docdb.documents.azure.com:443/"), "YRnKNmv6qHHhyWFna6UK8sJG7eI7izeKenajyCQdqyGf0XNfjiwYtIWfL3lc1CyVywX0EbRasDlOaKZCBKAEhQ==", new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp })) { var query = client.CreateDocumentQuery(UriFactory.CreateCollectionUri("imports", "Items"), "SELECT * FROM c where c.importId = \"d7968646-aec6-4687-be4f-e612cf361f76\""); var docQuery = query.AsDocumentQuery(); GetResults(docQuery).Wait(); Console.WriteLine("done"); Debug.WriteLine("done"); Console.Read(); } } catch (Exception e) { Console.WriteLine(e); Debug.WriteLine(e); Console.Read(); } }
public async Task <List <Module> > GetAllModuleAsync() { List <Module> modules = new List <Module>(); var queryS = "SELECT * FROM c WHERE c.Doctype = @module"; var pars = new SqlParameterCollection(); pars.Add(new SqlParameter("@module", "Module")); SqlQuerySpec query = new SqlQuerySpec(queryS, pars); FeedOptions option = new FeedOptions() { MaxItemCount = 1000 }; try { var querySalesOrder = client.CreateDocumentQuery <Module> (UriFactory.CreateCollectionUri(DatabaseID, CollectionID), query, option).AsDocumentQuery(); var results = await querySalesOrder.ExecuteNextAsync <Module>(); foreach (var result in results) { modules.Add(result); } return(modules); } catch (Exception e) { } return(null); }
public async Task <Course> GetCourseAsync(string pid) { var queryS = "SELECT * FROM c WHERE c.CourseId = @courseId"; var pars = new SqlParameterCollection(); pars.Add(new SqlParameter("@courseId", pid)); SqlQuerySpec query = new SqlQuerySpec(queryS, pars); FeedOptions option = new FeedOptions() { MaxItemCount = 1000 }; try { var querySalesOrder = client.CreateDocumentQuery <Course> (UriFactory.CreateCollectionUri(DatabaseID, CollectionID), query, option).AsDocumentQuery(); var results = await querySalesOrder.ExecuteNextAsync <Course>(); foreach (var result in results) { return(result); } } catch (Exception e) { } return(null); }
protected async Task <Uri> GetCollectionLinkAsync <T>() { if (!_collectionUris.ContainsKey(typeof(T))) { await CreateCollectionIfNecessaryAsync <T>(); _collectionUris[typeof(T)] = UriFactory.CreateCollectionUri(_settings.DocumentDbRepositoryDatabaseId, typeof(T).Name); } return(_collectionUris[typeof(T)]); }
public static async Task <IEnumerable <T> > GetItemsAsync() { IDocumentQuery <T> query = client.CreateDocumentQuery <T>(UriFactory.CreateCollectionUri(dbId, collectionId)).AsDocumentQuery(); List <T> results = new List <T>(); while (query.HasMoreResults) { results.AddRange(await query.ExecuteNextAsync <T>()); } return(results); }
private static async Task QueryAsync() { var collectionUri = UriFactory.CreateCollectionUri(ConfigurationHelper.DatabaseId, ConfigurationHelper.CollectionId); var query = client.CreateDocumentQuery <TweeterStatus>(collectionUri, new FeedOptions { MaxItemCount = 10 }) .Where(x => x.RetweetCount > 900) .OrderBy(x => x.User.ScreenName) .AsDocumentQuery(); while (query.HasMoreResults) { var result = await query.ExecuteNextAsync <TweeterStatus>(); Console.WriteLine("Request consumed {0} RU", result.RequestCharge); foreach (TweeterStatus status in result) { Console.WriteLine("Id: {0}, User: {1}, Followers: {2}", status.StatusId, status.User.ScreenName, status.User.FollowersCount); } } }
private static async Task UpdatePolicy() { await client.OpenAsync(); var collectionUri = UriFactory.CreateCollectionUri(ConfigurationHelper.DatabaseId, ConfigurationHelper.CollectionId); var collection = await client.ReadDocumentCollectionAsync(collectionUri); collection.Resource.IndexingPolicy.IndexingMode = IndexingMode.Consistent; collection.Resource.IndexingPolicy.Automatic = true; collection.Resource.IndexingPolicy.IncludedPaths.Clear(); collection.Resource.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/user/name/?", Indexes = new Collection <Index> { new RangeIndex(DataType.String) { Precision = -1 } } }); collection.Resource.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/user/screen_name/?", Indexes = new Collection <Index> { new RangeIndex(DataType.String) { Precision = -1 } } }); collection.Resource.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new Collection <Index> { new RangeIndex(DataType.Number) { Precision = -1 }, new HashIndex(DataType.String) { Precision = 3 } } }); collection.Resource.IndexingPolicy.ExcludedPaths = new Collection <ExcludedPath> { new ExcludedPath { Path = "/entities/*" }, new ExcludedPath { Path = "/user/favourites_count/?" }, new ExcludedPath { Path = "/in_reply_to_status_id/?" } }; await UpdatePolicy(collection); }
public static void FlightDataTransfer( [BlobTrigger("flightdata/{name}", Connection = "StorageConneciton")] Stream myBlob, string name, [CosmosDB(databaseName: "DatabaseName", collectionName: "CollectionName", ConnectionStringSetting = "CosmosDBConnection")] DocumentClient client, ILogger log) { //********************** Read App Settings******************* var CosmosDBName = System.Environment.GetEnvironmentVariable("DatabaseName", EnvironmentVariableTarget.Process); var CosmosCollectionName = System.Environment.GetEnvironmentVariable("CollectionName", EnvironmentVariableTarget.Process); var ttlDocuemnt = int.Parse(System.Environment.GetEnvironmentVariable("TTLDocuemntMinute", EnvironmentVariableTarget.Process) ?? "-1"); //*********************************************************** //check time to live on collection level. var collLink = UriFactory.CreateCollectionUri(CosmosDBName, CosmosCollectionName); DocumentCollection collection = client.ReadDocumentCollectionAsync(collLink).Result; if (ttlDocuemnt == -1 || collection.DefaultTimeToLive != ttlDocuemnt * 60) //time-to-live in seconds { collection.DefaultTimeToLive = ttlDocuemnt * 60; client.ReplaceDocumentCollectionAsync(collection); } log.LogInformation($"Function was triggered by blob Name:{name} Size: {myBlob.Length} Bytes"); //******************Read File content updated in storage**************************** string json_content; var tiks = DateTime.Now.Ticks; FlightRecords msg; using (TextReader tr = new StreamReader(myBlob)) { json_content = tr.ReadToEnd(); tr.Close(); } //*******************Validate File Content***************************************** try { msg = JsonConvert.DeserializeObject <FlightRecords>(json_content); } catch (Exception ex) { log.LogInformation($"File Content can not be deserialized. It seems to be wrong formated JSON\n"); log.LogError(ex.ToString()); return; } //********************Create separate documents by airline code*************************** var gpByAirline = msg.Flight_Record.GroupBy(x => x.AIRLINE_CODE).ToDictionary(k => k.Key, k => k.ToList()); var collectionLink = UriFactory.CreateDocumentCollectionUri(CosmosDBName, CosmosCollectionName); var option = new FeedOptions { EnableCrossPartitionQuery = true }; foreach (var airline in gpByAirline.Keys) { try { var documentId = $"FlightRecords-{airline}"; //unique Document ID //select document by ID var docExists = client.CreateDocumentQuery(collectionLink, option) .Where(doc => doc.Id == documentId) .Select(doc => doc.Id) .AsEnumerable() .Any(); //create new document. id - filed must be exists and represent the key var docSpec = new { id = documentId, Flight_Record = gpByAirline[airline] }; if (docExists) { //if the document exists, document will be replaced Uri docUri = UriFactory.CreateDocumentUri("boomi-msg", "boomi", documentId); var result = client.ReplaceDocumentAsync(docUri, docSpec).Result; log.LogInformation($"Document with id:'{result.Resource.Id}' updated"); } else { //if there is no document found - create new one. var result = client.CreateDocumentAsync(collectionLink, docSpec).Result; log.LogInformation($"Document with id:'{result.Resource.Id}' created"); } } catch (Exception ex) { log.LogInformation($"File Content can not be deserialized. It Seems to be wrong formated JSON\n{ex}"); return; } } }
public async static Task Run() { Debugger.Break(); var endpoint = ConfigurationManager.AppSettings["DocDbEndpoint"]; var masterKey = ConfigurationManager.AppSettings["DocDbMasterKey"]; // *** Before ID-Based Routing (8/13/2015) *** // Create a database using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { await client.CreateDatabaseAsync(new Database { Id = "MyNewDb" }); } // Create a collection without knowing the database self-link using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { Database database = client .CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'MyNewDb'") .AsEnumerable() .First(); await client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = "MyNewColl" }); } // Create a document without knowing the database or collection self-link using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { Database database = client .CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'MyNewDb'") .AsEnumerable() .First(); DocumentCollection collection = client .CreateDocumentCollectionQuery(database.CollectionsLink, "SELECT * FROM c WHERE c.id = 'MyNewColl'") .AsEnumerable() .First(); await client.CreateDocumentAsync(collection.SelfLink, new Document { Id = "MyNewDoc" }); } // Query for a document by its ID without knowing the database or collection self-link using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { Database database = client .CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'MyNewDb'") .AsEnumerable() .First(); DocumentCollection collection = client .CreateDocumentCollectionQuery(database.CollectionsLink, "SELECT * FROM c WHERE c.id = 'MyNewColl'") .AsEnumerable() .First(); Document document = client .CreateDocumentQuery(collection.DocumentsLink, "SELECT * FROM c WHERE c.id = 'MyNewDoc'") .AsEnumerable() .First(); } // Delete a document, collection, and database, without knowing any of their self-links using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { Database database = client .CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'MyNewDb'") .AsEnumerable() .First(); DocumentCollection collection = client .CreateDocumentCollectionQuery(database.CollectionsLink, "SELECT * FROM c WHERE c.id = 'MyNewColl'") .AsEnumerable() .First(); Document document = client .CreateDocumentQuery(collection.DocumentsLink, "SELECT * FROM c WHERE c.id = 'MyNewDoc'") .AsEnumerable() .First(); await client.DeleteDocumentAsync(document.SelfLink); await client.DeleteDocumentCollectionAsync(collection.SelfLink); await client.DeleteDatabaseAsync(database.SelfLink); } // *** After ID-Based Routing (8/13/2015) *** // Create a database using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { await client.CreateDatabaseAsync(new Database { Id = "MyNewDb" }); } // Create a collection without knowing the database self-link using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { await client.CreateDocumentCollectionAsync("dbs/MyNewDb", new DocumentCollection { Id = "MyNewColl" }); } // Create a document without knowing the database or collection self-link using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { await client.CreateDocumentAsync("dbs/MyNewDb/colls/MyNewColl", new Document { Id = "MyNewDoc" }); } // Query for a document by its ID without knowing the database or collection self-link using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { Document document = client .CreateDocumentQuery("dbs/MyNewDb/colls/MyNewColl/docs", "SELECT * FROM c WHERE c.id = 'MyNewDoc'") .AsEnumerable() .First(); } // Delete a document, collection, and database, without knowing any of their self-links using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { await client.DeleteDocumentAsync("dbs/MyNewDb/colls/MyNewColl/docs/MyNewDoc"); await client.DeleteDocumentCollectionAsync("dbs/MyNewDb/colls/MyNewColl"); await client.DeleteDatabaseAsync("dbs/MyNewDb"); } // Use UriFactory to automatically construct a URL-encoded ID-based self-link using (var client = new DocumentClient(new Uri(endpoint), masterKey)) { await client.CreateDatabaseAsync(new Database { Id = "MyNewDb" }); await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri("MyNewDb"), new DocumentCollection { Id = "MyNewColl" }); await client.CreateDocumentAsync(UriFactory.CreateCollectionUri("MyNewDb", "MyNewColl"), new Document { Id = "MyNewDoc" }); Document document = client .CreateDocumentQuery(UriFactory.CreateCollectionUri("MyNewDb", "MyNewColl"), "SELECT * FROM c WHERE c.id = 'MyNewDoc'") .AsEnumerable() .First(); await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri("MyNewDb", "MyNewColl", "MyNewDoc")); await client.DeleteDocumentCollectionAsync(UriFactory.CreateCollectionUri("MyNewDb", "MyNewColl")); await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri("MyNewDb")); } }