本文整理汇总了C#中Microsoft.Azure.Documents.Client.DocumentClient.CreateDocumentCollectionAsync方法的典型用法代码示例。如果您正苦于以下问题:C# DocumentClient.CreateDocumentCollectionAsync方法的具体用法?C# DocumentClient.CreateDocumentCollectionAsync怎么用?C# DocumentClient.CreateDocumentCollectionAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Azure.Documents.Client.DocumentClient
的用法示例。
在下文中一共展示了DocumentClient.CreateDocumentCollectionAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDocumentDbClient
private async Task<DocumentClient> GetDocumentDbClient()
{
var client = new DocumentClient(new Uri(_endPointUrl), _authorizationKKry);
var database = client.CreateDatabaseQuery().
Where(db => db.Id == DocumentDbId).AsEnumerable().FirstOrDefault();
if (database == null)
{
database = await client.CreateDatabaseAsync(
new Database
{
Id = DocumentDbId
});
}
DocumentCollection = client.CreateDocumentCollectionQuery
("dbs/" + database.Id).Where(c => c.Id == _collectionId).AsEnumerable().FirstOrDefault();
if (DocumentCollection == null)
{
DocumentCollection = await client.CreateDocumentCollectionAsync("dbs/" + DocumentDbId,
new DocumentCollection
{
Id = _collectionId
});
}
return client;
}
示例2: CreateDocumentCollection
private static async Task<DocumentCollection> CreateDocumentCollection(DocumentClient client, Database database, string collectionId)
{
var collectionSpec = new DocumentCollection { Id = collectionId };
var requestOptions = new RequestOptions { OfferType = "S1" };
return await client.CreateDocumentCollectionAsync(database.SelfLink, collectionSpec, requestOptions);
}
示例3: CreateDocumentCollection
private static void CreateDocumentCollection(DocumentClient documentClient)
{
// Create the database if it doesn't exist.
try
{
documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName))
.GetAwaiter()
.GetResult();
}
catch (DocumentClientException de)
{
// If the document collection does not exist, create it
if (de.StatusCode == HttpStatusCode.NotFound)
{
var collectionInfo = new DocumentCollection
{
Id = CollectionName,
IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 })
};
documentClient.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(DatabaseName),
collectionInfo,
new RequestOptions { OfferThroughput = 400 }).GetAwaiter().GetResult();
}
else
{
throw;
}
}
}
示例4: CreateCollection
public static async Task<HttpStatusCode> CreateCollection(DocumentClient client, Database database, string collectionName)
{
var result = await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection
{
Id = collectionName
});
return result.StatusCode;
}
示例5: GetDocumentCollection
//SELECT C.id AS CourseId, C.Name AS CourseName
//from CourseCollection AS C
//Join Session IN C.Sessions
//WHERE Session.Name = "Introduction"
#region Helpers
private static async Task<DocumentCollection> GetDocumentCollection(DocumentClient client, Database database)
{
DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == "CourseCollection").AsEnumerable().FirstOrDefault();
if (documentCollection == null)
{
documentCollection = await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection() { Id = "CourseCollection" });
}
return documentCollection;
}
示例6: GetOrCreateCollection
private static DocumentCollection GetOrCreateCollection(DocumentClient client, string databaseSelfLink, string collectionName)
{
// Try to retrieve the collection (Microsoft.Azure.Documents.DocumentCollection) whose Id is equal to collectionName
var collection = client.CreateDocumentCollectionQuery(databaseSelfLink)
.Where(c => c.Id == collectionName)
.ToArray()
.FirstOrDefault()
??
client.CreateDocumentCollectionAsync(databaseSelfLink,
new DocumentCollection { Id = collectionName }).Result;
if (collection == null)
{
collection = new DocumentCollection { Id = collectionName };
collection.IndexingPolicy.IncludedPaths.Add(new IndexingPath { IndexType = IndexType.Range, NumericPrecision = 5, Path = "/" });
collection = client.CreateDocumentCollectionAsync(databaseSelfLink, collection).Result;
}
return collection;
}
示例7: ReadOrCreateCollection
internal static DocumentCollection ReadOrCreateCollection(DocumentClient client, string databaseLink, string collectionId)
{
var collections = client.CreateDocumentCollectionQuery(databaseLink)
.Where(col => col.Id == collectionId).ToArray();
if (collections.Any())
{
return collections.First();
}
var collection = new DocumentCollection { Id = collectionId };
return client.CreateDocumentCollectionAsync(databaseLink, collection).Result;
}
示例8: WriteDocument
public async Task<bool> WriteDocument(MessageType type, String jsonString)
{
var client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["DocumentServiceEndpoint"]), ConfigurationManager.AppSettings["DocumentKey"]);
var dbName = ConfigurationManager.AppSettings["DocumentDatabase"];
var database = client.CreateDatabaseQuery().Where(db => db.Id == dbName).AsEnumerable().FirstOrDefault() ??
await client.CreateDatabaseAsync(new Database{ Id = dbName});
// Check to verify a document collection with the does not exist
var docCollection = ConfigurationManager.AppSettings["DocumentCollection"];
var documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == docCollection).AsEnumerable().FirstOrDefault() ??
await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection { Id = docCollection });
var id = Guid.NewGuid().ToString();
var response = HttpStatusCode.Unused;
switch (type)
{
case MessageType.Energy:
var energyDoc = JsonConvert.DeserializeObject<EnergyDocument>(jsonString);
energyDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, energyDoc)).StatusCode;
break;
case MessageType.Humidity:
var humidityDoc = JsonConvert.DeserializeObject<HumidityDocument>(jsonString);
humidityDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, humidityDoc)).StatusCode;
break;
case MessageType.Light:
var lightDoc = JsonConvert.DeserializeObject<LightDocument>(jsonString);
lightDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, lightDoc)).StatusCode;
break;
case MessageType.Temperature:
var tempDoc = JsonConvert.DeserializeObject<TemperatureDocument>(jsonString);
tempDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, tempDoc)).StatusCode;
break;
}
return response == HttpStatusCode.Created;
}
示例9: SaveTestResults
public static async Task SaveTestResults(PerformanceTestResult testResults)
{
// Make sure to call client.Dispose() once you've finished all DocumentDB interactions
// Create a new instance of the DocumentClient
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
// Check to verify a database with the id=FamilyRegistry does not exist
Database database = client.CreateDatabaseQuery().Where(db => db.Id == "DistributedWebTest").AsEnumerable().FirstOrDefault();
if (database == null)
{
// Create a database
database = await client.CreateDatabaseAsync(
new Database
{
Id = "DistributedWebTest"
});
}
// Check to verify a document collection with the id=FamilyCollection does not exist
DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == "DistributedTestResults").AsEnumerable().FirstOrDefault();
if (documentCollection == null)
{
// Create a document collection using the lowest performance tier available (currently, S1)
documentCollection = await client.CreateDocumentCollectionAsync(database.CollectionsLink,
new DocumentCollection { Id = "DistributedTestResults" },
new RequestOptions { OfferType = "S1" });
}
await client.CreateDocumentAsync(documentCollection.DocumentsLink, testResults);
}
示例10: GetOrCreateDocumentCollection
public static DocumentCollection GetOrCreateDocumentCollection(DocumentClient client, string databaseLink,
string collectionId, IndexingPolicy indexingPolicy)
{
IQueryable<DocumentCollection> collectionQuery =
from coll in client.CreateDocumentCollectionQuery(databaseLink)
where coll.Id == collectionId
select coll;
IEnumerable<DocumentCollection> enumerable = collectionQuery.AsEnumerable();
if (!enumerable.Any())
{
DocumentCollection collection = new DocumentCollection() { Id = collectionId };
if (indexingPolicy != null)
{
collection.IndexingPolicy.Automatic = indexingPolicy.Automatic;
collection.IndexingPolicy.IndexingMode = indexingPolicy.IndexingMode;
foreach (var path in indexingPolicy.IncludedPaths)
{
collection.IndexingPolicy.IncludedPaths.Add(path);
}
foreach (var path in indexingPolicy.ExcludedPaths)
{
collection.IndexingPolicy.ExcludedPaths.Add(path);
}
}
return client.CreateDocumentCollectionAsync(databaseLink, collection).Result.Resource;
}
else
{
return enumerable.First();
}
}
示例11: UploadFeedsToDocumentDB
public async void UploadFeedsToDocumentDB()
{
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
Database database = client.CreateDatabaseQuery()
.Where(db => db.Id == databaseId)
.AsEnumerable()
.FirstOrDefault();
if (database==null)
{
database = await client.CreateDatabaseAsync(new Database { Id = databaseId });
Console.WriteLine("Created Database: id - {0} and selfLink - {1}", database.Id, database.SelfLink);
}
DocumentCollection collectionBlog = client.CreateDocumentCollectionQuery(database.CollectionsLink)
.Where(doccoll => doccoll.Id == collectionIdBlogs)
.AsEnumerable()
.FirstOrDefault();
if (collectionBlog == null)
{
collectionBlog = await client.CreateDocumentCollectionAsync(database.SelfLink,
new DocumentCollection { Id = collectionIdBlogs });
Console.WriteLine("Created Collection {0}.", collectionBlog);
}
DocumentCollection collectionPosts = client.CreateDocumentCollectionQuery(database.CollectionsLink)
.Where(doccoll => doccoll.Id == collectionIdPost)
.AsEnumerable()
.FirstOrDefault();
if (collectionPosts == null)
{
collectionPosts = await client.CreateDocumentCollectionAsync(database.SelfLink,
new DocumentCollection { Id = collectionIdPost });
Console.WriteLine("Created Collection {0}.", collectionPosts);
}
foreach(var f in this.Feeds)
{
Console.WriteLine(f.Title);
foreach (var post in f.Items)
{
Console.Write(".");
try
{
await client.CreateDocumentAsync(collectionPosts.SelfLink, post);
}
catch(Exception ex)
{ Console.WriteLine("X"); }
}
f.Items.Clear();
await client.CreateDocumentAsync(collectionBlog.SelfLink, f);
Console.WriteLine();
}
}
示例12: GetStartedDemo
private static async Task GetStartedDemo()
{
// Make sure to call client.Dispose() once you've finished all DocumentDB interactions
// Create a new instance of the DocumentClient
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
// Check to verify a database with the id=FamilyRegistry does not exist
Database database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault();
if (database == null)
{
// Create a database
database = await client.CreateDatabaseAsync(
new Database
{
Id = "FamilyRegistry"
});
}
else { Warn("database"); }
// Check to verify a document collection with the id=FamilyCollection does not exist
DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault();
if (documentCollection == null)
{
// Create a document collection using the lowest performance tier available (currently, S1)
documentCollection = await client.CreateDocumentCollectionAsync(database.CollectionsLink,
new DocumentCollection { Id = "FamilyCollection" },
new RequestOptions { OfferType = "S1" });
}
else { Warn("document collection"); }
// Check to verify a document with the id=AndersenFamily does not exist
Document document = client.CreateDocumentQuery(documentCollection.DocumentsLink).Where(d => d.Id == "AndersenFamily").AsEnumerable().FirstOrDefault();
if (document == null)
{
// Create the Andersen Family document
Family AndersonFamily = new Family
{
Id = "AndersenFamily",
LastName = "Andersen",
Parents = new Parent[] {
new Parent { FirstName = "Thomas" },
new Parent { FirstName = "Mary Kay"}
},
Children = new Child[] {
new Child
{
FirstName = "Henriette Thaulow",
Gender = "female",
Grade = 5,
Pets = new Pet[] {
new Pet { GivenName = "Fluffy" }
}
}
},
Address = new Address { State = "WA", County = "King", City = "Seattle" },
IsRegistered = true
};
await client.CreateDocumentAsync(documentCollection.DocumentsLink, AndersonFamily);
}
else { Warn("document"); }
// Check to verify a document with the id=AndersenFamily does not exist
document = client.CreateDocumentQuery(documentCollection.DocumentsLink).Where(d => d.Id == "WakefieldFamily").AsEnumerable().FirstOrDefault();
if (document == null)
{
// Create the WakeField document
Family WakefieldFamily = new Family
{
Id = "WakefieldFamily",
Parents = new Parent[] {
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
};
//.........这里部分代码省略.........
示例13: CreateDocumentDbConnection
private DocumentConnection CreateDocumentDbConnection()
{
var client = new DocumentClient(new Uri(EndpointUri), AccountKey);
// get the database and if it doesn't exist create it
Database database = client.CreateDatabaseQuery()
.Where(db => db.Id == Database)
.AsEnumerable()
.FirstOrDefault();
if (database == null)
{
Log.LogMessage(MessageImportance.Low, "The database {0} does not exist, will create it.", Database);
var task = client.CreateDatabaseAsync(new Database { Id = Database });
database = task.Result;
}
// get the document collection and if it doesn't exist create it
DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink)
.Where(c => c.Id == Collection)
.AsEnumerable()
.FirstOrDefault();
if (collection == null)
{
Log.LogMessage(MessageImportance.Low, "The collection {0} does not exist, will create it.", Collection);
var task = client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = Collection });
collection = task.Result;
}
Log.LogMessage(MessageImportance.Normal, "Connected to DocumentDB database {0}, collection {1}.", Database, Collection);
return new DocumentConnection(database, client, collection);
}
示例14: InitializeDocumentDb
/// <summary>
/// Initialize the DocumentDb settings and connections
/// </summary>
public void InitializeDocumentDb()
{
this.DocumentDbEndPointUrl = ConfigurationManager.AppSettings["DocumentDbEndPointUrl"];
if (String.IsNullOrWhiteSpace(this.DocumentDbEndPointUrl))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbEndPointUrl");
}
this.DocumentDbAuthorizationKey = ConfigurationManager.AppSettings["DocumentDbAuthorizationKey"];
if (String.IsNullOrWhiteSpace(this.DocumentDbAuthorizationKey))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbAuthorizationKey");
}
this.DocumentDbDatabase = ConfigurationManager.AppSettings["DocumentDbDatabase"];
if (String.IsNullOrWhiteSpace(this.DocumentDbDatabase))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbDatabase");
}
this.DocumentDbCollection = ConfigurationManager.AppSettings["DocumentDbCollection"];
if (String.IsNullOrWhiteSpace(this.DocumentDbCollection))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbCollection");
}
// Create a new instance of the DocumentClient
documentClient = new DocumentClient(new Uri(this.DocumentDbEndPointUrl), this.DocumentDbAuthorizationKey);
// Check to verify if database already exists
Database database = documentClient.CreateDatabaseQuery().
Where(db => db.Id == this.DocumentDbDatabase).AsEnumerable().FirstOrDefault();
if (database == null)
{
Context.Logger.Info("Creating a new DocumentDb database: {0}", this.DocumentDbDatabase);
// Create a database
var task = documentClient.CreateDatabaseAsync(
new Database
{
Id = this.DocumentDbDatabase
});
task.Wait();
database = task.Result;
}
else
{
Context.Logger.Info("Found an existing DocumentDb database: {0}", database.Id);
}
// Check to verify a document collection already exists
documentCollection = documentClient.CreateDocumentCollectionQuery(database.CollectionsLink).
Where(c => c.Id == this.DocumentDbCollection).AsEnumerable().FirstOrDefault();
if (documentCollection == null)
{
Context.Logger.Info("Creating a new DocumentDb collection: {0}", this.DocumentDbCollection);
// Create a document collection
var task = documentClient.CreateDocumentCollectionAsync(database.CollectionsLink,
new DocumentCollection
{
Id = this.DocumentDbCollection,
});
task.Wait();
documentCollection = task.Result;
}
else
{
Context.Logger.Info("Found an existing DocumentDb collection: {0}", documentCollection.Id);
}
}
示例15: CreateDatabase
private static async Task CreateDatabase()
{
var client = new DocumentClient(new Uri(url), auth);
// Create the database if it does not exist.
Database database = client.CreateDatabaseQuery().Where(db => db.Id == "fProj")
.AsEnumerable().FirstOrDefault();
if (database == null)
{
database = await client.CreateDatabaseAsync(new Database { Id = "fProj" });
}
else
{
Console.WriteLine("Your Database existed previously");
}
// Create the Document Collection if it does not exist.
DocumentCollection col = client.CreateDocumentCollectionQuery
(database.CollectionsLink)
.Where(c => c.Id == "fpDocs")
.AsEnumerable().FirstOrDefault();
if (col == null)
{
col = await client.CreateDocumentCollectionAsync
(database.CollectionsLink,
new DocumentCollection { Id = "fpDocs" },
new RequestOptions { OfferType = "S1" });
}
else
{
Console.WriteLine("Yo, you already had that collection");
}
// Create the first document
Document doc = client.CreateDocumentQuery(col.DocumentsLink)
.Where(d => d.Id == "1").AsEnumerable().FirstOrDefault();
if (doc == null)
{
Restaurant doc1 = new Restaurant
{
Id = "1",
RName = "Duck's Country Bunker",
UserId = "user1",
FName = "Phil",
LName = "McLovin",
MenuItems = new MenuItem[] {
new MenuItem { Id = "1", ItemName = "Small Fries", Calories = 333, Price = .99M },
new MenuItem { Id = "2", ItemName = "Medium Fries", Calories = 444, Price = 1.99M },
new MenuItem { Id = "3", ItemName = "Large Fries", Calories = 555, Price = 2.99M },
new MenuItem { Id = "4", ItemName = "Small Beer", Calories = 5, Price = .99M },
new MenuItem { Id = "5", ItemName = "Medium Beer", Calories = 10, Price = 2.00M },
new MenuItem { Id = "6", ItemName = "Normal Sized Beer", Calories = 300, Price = 3.00M },
new MenuItem { Id = "7", ItemName = "Coffee", Calories = 50, Price = 1.00M }
},
Address = new Address
{
Id = "1",
StreetNumber = "1235 SE Brooklyn St.",
City = "Corvallis",
State = "Oregon",
ZipCode = "99775"
}
};
await client.CreateDocumentAsync(col.DocumentsLink, doc1);
}
// Create the second document
doc = client.CreateDocumentQuery(col.DocumentsLink)
.Where(d => d.Id == "2").AsEnumerable().FirstOrDefault();
if (doc == null)
{
Restaurant doc2 = new Restaurant
{
Id = "2",
RName = "Obi One",
UserId = "user1",
FName = "Allen",
LName = "Jones",
MenuItems = new MenuItem[] {
new MenuItem { Id = "1", ItemName = "Cheeseburger", Calories = 75, Price = .99M },
new MenuItem { Id = "2", ItemName = "Hamburger", Calories = 50, Price = 1M },
new MenuItem { Id = "3", ItemName = "Regular Onion Rings", Calories = 100, Price = 1.50M },
new MenuItem { Id = "4", ItemName = "Large Onion Rings", Calories = 300, Price = 2M }
},
Address = new Address
{
Id = "2",
StreetNumber = "4949 N Broadway Ave",
City = "Eugene",
State = "Oregon",
ZipCode = "96709"
}
};
await client.CreateDocumentAsync(col.DocumentsLink, doc2);
//.........这里部分代码省略.........