123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Linq;
- using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
- using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;
- using Microsoft.Azure.Documents.Client;
- using Microsoft.Azure.Documents;
- using Microsoft.Azure.Documents.Linq;
- using System.Reflection;
- using Microsoft.Azure.CosmosDB.BulkExecutor;
- using Microsoft.Azure.CosmosDB.BulkExecutor.BulkImport;
- using System.Threading;
- using Microsoft.Azure.CosmosDB.BulkExecutor.BulkUpdate;
- using CosmosDBTest.AzureCosmosDB;
- using System.Text.Json;
- namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
- { /// <summary>
- /// sdk 文档https://github.com/Azure/azure-cosmos-dotnet-v2/tree/master/samples
- /// https://github.com/Azure/azure-cosmos-dotnet-v2/blob/530c8d9cf7c99df7300246da05206c57ce654233/samples/code-samples/DatabaseManagement/Program.cs#L72-L121
- /// </summary>
- public class AzureCosmosDBRepository : IAzureCosmosDBRepository
- {
- /// <summary>
- /// sdk 文档https://github.com/Azure/azure-cosmos-dotnet-v2/tree/master/samples
- /// https://github.com/Azure/azure-cosmos-dotnet-v2/blob/530c8d9cf7c99df7300246da05206c57ce654233/samples/code-samples/DatabaseManagement/Program.cs#L72-L121
- /// </summary>
-
- private DocumentClient CosmosClient { get; set; }
- private DocumentCollection CosmosCollection { get; set; }
- private string Database { get; set; }
- private int CollectionThroughput { get; set; }
- public AzureCosmosDBRepository(AzureCosmosDBOptions options)
- {
- try
- {
- if (!string.IsNullOrEmpty(options.ConnectionString))
- {
- CosmosClient = CosmosDBClientSingleton.getInstance(options.ConnectionString, options.ConnectionKey).GetCosmosDBClient();
- }
-
- else
- {
- throw new Exception("请设置正确的AzureCosmosDB数据库配置信息!");
- }
- Database = options.Database;
- CollectionThroughput = options.CollectionThroughput;
- CosmosClient.CreateDatabaseIfNotExistsAsync(new Database { Id = Database });
- // _connectionString = options.ConnectionString;
- }
- catch (DocumentClientException de)
- {
- Exception baseException = de.GetBaseException();
- Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message);
- }
- catch (Exception e)
- {
- Exception baseException = e.GetBaseException();
- Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
- }
- finally
- {
- Console.WriteLine("End of demo, press any key to exit.");
- // Console.ReadKey();
- }
- }
- private async Task<DocumentCollection> InitializeCollection<T>()
- {
- Type t = typeof(T);
- if (CosmosCollection == null || !CosmosCollection.Id.Equals(t.Name))
- {
- DocumentCollection collectionDefinition = new DocumentCollection { Id = t.Name };
- collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
- string partitionKey = GetPartitionKey<T>();
- // collectionDefinition.PartitionKey = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection<string>() };
- if (!string.IsNullOrEmpty(partitionKey))
- {
- collectionDefinition.PartitionKey.Paths.Add("/" + partitionKey);
- }
- // CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(Database), collectionDefinition);
- CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(
- UriFactory.CreateDatabaseUri(Database), collectionDefinition, new RequestOptions { OfferThroughput = CollectionThroughput }
- );
- }
- return CosmosCollection;
- }
- private string GetPartitionKey<T>()
- {
- Type type = typeof(T);
- PropertyInfo[] properties = type.GetProperties();
- List<PropertyInfo> attrProperties = new List<PropertyInfo>();
- foreach (PropertyInfo property in properties)
- {
- if (property.Name.Equals("PartitionKey"))
- {
- attrProperties.Add(property);
- break;
- }
- object[] attributes = property.GetCustomAttributes(true);
- foreach (object attribute in attributes) //2.通过映射,找到成员属性上关联的特性类实例,
- {
- if (attribute is PartitionKeyAttribute)
- {
- attrProperties.Add(property);
- }
- }
- }
- if (attrProperties.Count <= 0)
- {
- return null;
- }
- else
- {
- if (attrProperties.Count == 1)
- {
- return attrProperties[0].Name;
- }
- else { throw new Exception("PartitionKey can only be single!"); }
- }
- }
- public async Task<T> Save<T>(T entity) //where T : object, new()
- {
- try {
- Type t = typeof(T);
- DocumentCollection documentCollection = await InitializeCollection<T>();
- ResourceResponse<Document> doc =
- await CosmosClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, t.Name), entity);
- //Console.WriteLine(doc.ActivityId);
- return entity;
- } catch (Exception e ) {
- throw new Exception(e.Message);
- }
- }
- public async Task<T> Update<T>(T entity)
- {
- Type t = typeof(T);
- await InitializeCollection<T>();
- ResourceResponse<Document> doc =
- await CosmosClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, t.Name), entity);
- return entity;
- }
- public async Task<string> ReplaceObject<T>(T entity, string key)
- {
- Type t = typeof(T);
- await InitializeCollection<T>();
- try
- {
- ResourceResponse<Document> doc =
- await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, key), entity);
- return key;
- }
- catch (Exception e)
- {
- Console.WriteLine("{0} Exception caught.", e);
- //return false;
- }
- return null;
- }
- public async Task<string> ReplaceObject<T>(T entity, string key, string partitionKey)
- {
- Type t = typeof(T);
- await InitializeCollection<T>();
- try
- {
- ResourceResponse<Document> doc =
- await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, key),
- entity,
- new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
- return key;
- }
- catch (Exception e)
- {
- throw new Exception(e.Message);
- //Console.WriteLine("{0} Exception caught.", e);
- //return false;
- }
- }
- public async Task<List<T>> FindAll<T>()
- {
- Type t = typeof(T);
- Boolean open = true;
- List<T> objs = new List<T>();
- //await InitializeCollection<T>();
- //查询条数 -1是全部
- FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions).AsDocumentQuery();
- while (query.HasMoreResults)
- {
- foreach (T obj in await query.ExecuteNextAsync())
- {
- objs.Add(obj);
- }
- }
- return objs;
- //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name),sql);
- }
- public async Task<List<T>> FindLinq<T>(Func<IQueryable<object>, object> singleOrDefault)
- {
- Type t = typeof(T);
- List<T> objs = new List<T>();
- await InitializeCollection<T>();
- //查询条数 -1是全部
- FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions);
- // query.Where();
- return objs;
- //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name),sql);
- }
- public async Task<List<T>> FindSQL<T>(string sql)
- {
- Type t = typeof(T);
- List<T> objs = new List<T>();
- await InitializeCollection<T>();
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql);
- foreach (var item in query)
- {
- objs.Add(item);
- }
- return objs;
- }
- public async Task<List<T>> FindSQL<T>(string sql, bool IsPk)
- {
- Type t = typeof(T);
- List<T> objs = new List<T>();
- Boolean open = IsPk;
- await InitializeCollection<T>();
- //查询条数 -1是全部
- FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql, queryOptions);
- foreach (var item in query)
- {
- objs.Add(item);
- }
- return objs;
- }
- public async Task<string> DeleteAsync<T>(string id)
- {
- Type t = typeof(T);
- await InitializeCollection<T>();
- ResourceResponse<Document> doc =
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, id));
- //Console.WriteLine(doc.ActivityId);
- return id;
- }
- public async Task<T> DeleteAsync<T>(T entity)
- {
- await InitializeCollection<T>();
- Type t = typeof(T);
- string PartitionKey = GetPartitionKey<T>();
- if (!string.IsNullOrEmpty(PartitionKey))
- {
- string pkValue = entity.GetType().GetProperty(PartitionKey).GetValue(entity).ToString();
- string idValue = entity.GetType().GetProperty("id").GetValue(entity).ToString();
- ResourceResponse<Document> doc =
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, idValue), new RequestOptions { PartitionKey = new PartitionKey(pkValue) });
- }
- else
- {
- string idValue = entity.GetType().GetProperty("id").GetValue(entity).ToString();
- ResourceResponse<Document> doc =
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, idValue));
- }
- //Console.WriteLine(doc.ActivityId);
- return entity;
- }
- public async Task<string> DeleteAsync<T>(string id, string partitionKey)
- {
- Type t = typeof(T);
- await InitializeCollection<T>();
- ResourceResponse<Document> doc =
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, id), new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
- //Console.WriteLine(doc.ActivityId);
- return id;
- }
- public async Task<List<T>> SaveAll<T>(List<T> enyites)
- {
- DocumentCollection dataCollection = await InitializeCollection<T>();
- // Set retry options high for initialization (default values).
- CosmosClient.ConnectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds = 30;
- CosmosClient.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 9;
- IBulkExecutor bulkExecutor = new BulkExecutor(CosmosClient, dataCollection);
- await bulkExecutor.InitializeAsync();
- // Set retries to 0 to pass control to bulk executor.
- CosmosClient.ConnectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds = 0;
- CosmosClient.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 0;
- BulkImportResponse bulkImportResponse = null;
- long totalNumberOfDocumentsInserted = 0;
- double totalRequestUnitsConsumed = 0;
- double totalTimeTakenSec = 0;
- var tokenSource = new CancellationTokenSource();
- var token = tokenSource.Token;
- int pageSize = 100;
- int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
- for (int i = 0; i < pages; i++)
- {
- List<string> documentsToImportInBatch = new List<string>();
- List<T> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
- for (int j = 0; j < lists.Count; j++)
- {
- documentsToImportInBatch.Add(JsonSerializer.Serialize(lists[j]));
- }
- var tasks = new List<Task>
- { Task.Run(async () =>
- {
- do
- {
- //try
- //{
- bulkImportResponse = await bulkExecutor.BulkImportAsync(
- documents: documentsToImportInBatch,
- enableUpsert: true,
- disableAutomaticIdGeneration: true,
- maxConcurrencyPerPartitionKeyRange: null,
- maxInMemorySortingBatchSize: null,
- cancellationToken: token);
- //}
- //catch (DocumentClientException de)
- //{
- // break;
- //}
- //catch (Exception e)
- //{
- // break;
- //}
- } while (bulkImportResponse.NumberOfDocumentsImported < documentsToImportInBatch.Count);
- totalNumberOfDocumentsInserted += bulkImportResponse.NumberOfDocumentsImported;
- totalRequestUnitsConsumed += bulkImportResponse.TotalRequestUnitsConsumed;
- totalTimeTakenSec += bulkImportResponse.TotalTimeTaken.TotalSeconds;
- },
- token)
- };
- await Task.WhenAll(tasks);
- }
- return enyites;
- }
-
- private static UpdateOperation SwitchType(string key, object obj)
- {
- Type s = obj.GetType();
- TypeCode typeCode = Type.GetTypeCode(s);
- switch (typeCode)
- {
- case TypeCode.String: return new SetUpdateOperation<string>(key, obj.ToString());
- case TypeCode.Int32: return new SetUpdateOperation<Int32>(key, (Int32)obj);
- case TypeCode.Double: return new SetUpdateOperation<Double>(key, (Double)obj);
- case TypeCode.Byte: return new SetUpdateOperation<Byte>(key, (Byte)obj);
- case TypeCode.Boolean: return new SetUpdateOperation<Boolean>(key, (Boolean)obj);
- case TypeCode.DateTime: return new SetUpdateOperation<DateTime>(key, (DateTime)obj);
- case TypeCode.Int64: return new SetUpdateOperation<Int64>(key, (Int64)obj);
- default: return null;
- }
- }
- }
- }
|