.net下將關係數據庫的數據批量導入Elasticsearch中。html
Elasticsearch使用版本的是7.xgit
下面是主要應用到的方法,代碼參考:Elasticsearch.Net、Nest批量插入BulkAll,感謝做者。github
public static bool BulkAll<T>(IElasticClient elasticClient, IndexName indexName, IEnumerable<T> list) where T : class { const int size = 1000; var tokenSource = new CancellationTokenSource(); var observableBulk = elasticClient.BulkAll(list, f => f .MaxDegreeOfParallelism(8) .BackOffTime(TimeSpan.FromSeconds(10)) .BackOffRetries(2) .Size(size) .RefreshOnCompleted() .Index(indexName) .BufferToBulk((r, buffer) => r.IndexMany(buffer)) , tokenSource.Token); var countdownEvent = new CountdownEvent(1); Exception exception = null; void OnCompleted() { WriteLine("BulkAll Finished"); countdownEvent.Signal(); } var bulkAllObserver = new BulkAllObserver( onNext: response => { WriteLine($"Indexed {response.Page * size} with {response.Retries} retries"); }, onError: ex => { WriteLine("BulkAll Error : {0}", ex); exception = ex; countdownEvent.Signal(); }, OnCompleted); observableBulk.Subscribe(bulkAllObserver); countdownEvent.Wait(tokenSource.Token); if (exception != null) { WriteLine("BulkHotelGeo Error : {0}", exception); return false; } else { return true; } }
參考代碼數據庫