public void DeleteDocument(string collectionId, string documentId) { JObject document = GetDocument(collectionId, documentId); if (document != null) { Uri documentUri = UriFactory.CreateDocumentUri(_databaseId, collectionId, documentId); Task <ResourceResponse <Document> > deleteTask = _database.DeleteDocumentAsync(documentUri); deleteTask.Wait(); } }
static int Main(string[] args) { Options options = new Options(); if (!CommandLine.Parser.Default.ParseArguments(args, options)) { Console.WriteLine("Invalid arguments"); return 1; } using (DocumentClient client = new DocumentClient( new Uri(options.Endpoint), options.AuthKey, new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp })) { Database database = client.CreateDatabaseQuery().Where(d => d.Id == options.Database).AsEnumerable().FirstOrDefault(); if (database == null) { Console.WriteLine("Cannot find database " + options.Database); return 2; } List<DocumentCollection> collections = client.ReadDocumentCollectionFeedAsync(database.SelfLink).Result.ToList(); int minimumRequiredCollections = Math.Max(options.NewCollections, options.CurrentCollections); if (collections.Count < minimumRequiredCollections) { Console.WriteLine("At least {0} collections must be pre-created", minimumRequiredCollections); return 3; } Console.WriteLine("Current distribution of documents across collections:"); LogDocumentCountsPerCollection(client, database).Wait(); Console.WriteLine(); HashPartitionResolver currentPartitionResolver = new HashPartitionResolver(options.PartitionKeyName, collections.Take(options.CurrentCollections).Select(c => c.SelfLink)); HashPartitionResolver nextPartitionResolver = new HashPartitionResolver(options.PartitionKeyName, collections.Take(options.NewCollections).Select(c => c.SelfLink)); int numberOfMovedDocuments = 0; Parallel.ForEach(currentPartitionResolver.CollectionLinks, collectionLink => { ResourceFeedReader<Document> feedReader = client.CreateDocumentFeedReader(collectionLink, new FeedOptions { MaxItemCount = -1 }); while (feedReader.HasMoreResults) { foreach (Document document in DocumentClientHelper.ExecuteWithRetryAsync<FeedResponse<Document>>(() => feedReader.ExecuteNextAsync()).Result) { object partitionKey = nextPartitionResolver.GetPartitionKey(document); string newCollectionLink = nextPartitionResolver.ResolveForCreate(partitionKey); if (newCollectionLink != collectionLink) { int count = Interlocked.Increment(ref numberOfMovedDocuments); DocumentClientHelper.ExecuteWithRetryAsync(() => client.DeleteDocumentAsync(document.SelfLink)).Wait(); DocumentClientHelper.ExecuteWithRetryAsync(() => client.CreateDocumentAsync(newCollectionLink, document)).Wait(); if (count % 100 == 0) { Console.WriteLine("Moved {0} documents between partitions", numberOfMovedDocuments); } } } } }); Console.WriteLine(); Console.WriteLine("Moved {0} documents between partitions.", numberOfMovedDocuments); Console.WriteLine(); Console.WriteLine("Current distribution of documents across collections:"); LogDocumentCountsPerCollection(client, database).Wait(); Console.WriteLine(); } return 0; }
private static async Task TransferDc(DocumentCollection oldDc, DocumentCollection newDc, DocumentClient client, Database database, Hashtable newList) { foreach (DictionaryEntry dis in newList) { var tempDis = dis; var items = from d in client.CreateDocumentQuery<PostMessage>(oldDc.DocumentsLink) where d.Type == "Post" && d.Path.District == tempDis.Key.ToString() select d; foreach (var item in items) { try { await client.DeleteDocumentAsync(item._self); } catch (Exception) { Thread.Sleep(200); client.DeleteDocumentAsync(item._self).Wait(); } try { await client.CreateDocumentAsync(newDc.DocumentsLink, item); } catch (Exception) { Thread.Sleep(200); client.CreateDocumentAsync(newDc.DocumentsLink, item).Wait(); } } } }
private static async Task SaveDisList(Hashtable newList, Hashtable oldList, DocumentCollection origin, DocumentCollection oldDc, DocumentCollection newDc, DocumentClient client) { var t = (long) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds; var allow = new DcAllocate { Type = "DisList", DcName = newDc.Id, DcSelfLink = newDc.SelfLink, District = new List<string>() }; foreach (DictionaryEntry i in newList) { allow.District.Add(i.Key.ToString()); } await client.CreateDocumentAsync(origin.DocumentsLink, allow); var ds = from d in client.CreateDocumentQuery<DcAllocate>(origin.DocumentsLink) where d.Type == "DisList" && d.DcName == oldDc.Id select d; var l = ds.ToList().FirstOrDefault(); if (l != null) await client.DeleteDocumentAsync(l._self); var allow2 = new DcAllocate { Type = "DisList", DcName = oldDc.Id, DcSelfLink = oldDc.SelfLink, District = new List<string>(), }; foreach (DictionaryEntry i in oldList) { allow2.District.Add(i.Key.ToString()); } await client.CreateDocumentAsync(origin.DocumentsLink, allow2); }
private static async Task DeleteAll(DocumentClient client, Database database, DocumentCollection documentCollection) { IEnumerable<DocumentCollection> dz = client.CreateDocumentCollectionQuery(database.SelfLink) .AsEnumerable(); foreach (var z in dz) { var families = from f in client.CreateDocumentQuery(z.DocumentsLink) select f; foreach (var family in families) { dynamic d = JsonConvert.DeserializeObject(family.ToString()); //Console.WriteLine(d.Path.school); try { //var res = await client.DeleteDocumentAsync(family.SelfLink); var family1 = family; var response = await _iDbService.ExecuteWithRetries(5, () => client.DeleteDocumentAsync(family1.SelfLink)); } catch (DocumentClientException e) { /* if (e.RetryAfter.TotalMilliseconds>0) { Console.WriteLine(e.RetryAfter.TotalMilliseconds); Thread.Sleep((int) e.RetryAfter.TotalMilliseconds); client.DeleteDocumentAsync(family.SelfLink).Wait(); } else {*/ Console.WriteLine(e.Message); // } } Console.WriteLine(family.SelfLink); } } }
public async Task DeleteDocument(string id) { await _cosmosDBClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(_dbSetting.DatabaseId, _dbSetting.CollectionId, id), CreateRequestOptions("*")); }
private static async Task DeleteCourse(Guid guid, DocumentCollection documentCollection) { DocumentClient documentClient = new DocumentClient(new Uri("https://schooltest.documents.azure.com:443/"), "Of414DnEibhmqeSTB0pKShdQtR6dxxfXP8uT93pggxoumKthJGzuECCUJZ27Pqf846VUkFfOrraMTQ74PZaywA=="); Course course = documentClient.CreateDocumentQuery<Course>(documentCollection.DocumentsLink, new SqlQuerySpec(string.Format("SELECT * FROM c WHERE c.CourseId = '{0}'", guid))).AsEnumerable().FirstOrDefault(); if (course == null) return; await documentClient.DeleteDocumentAsync(course.SelfLink); }
public static Task DeleteFamily(DocumentClient client, DocumentCollection collection, string needle) { var family = GetFamily(client, collection, needle); dynamic doc = GetDocumentById(client, collection,family.id); var task = client.DeleteDocumentAsync(doc.SelfLink); return task; }