想法:
从kafka拉数据推送,因此这里不适用OOM,后续接入kafka。
- 安装依赖包
bash
> Elastic.Clients.Elasticsearch 9.2.2 9.2.2
> Newtonsoft.Json 13.0.4 13.0.4
> Swashbuckle.AspNetCore 10.0.1 10.0.1
- 写helper
csharp
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.Core.Bulk;
using Elastic.Clients.Elasticsearch.Nodes;
using Elastic.Clients.Elasticsearch.QueryDsl;
using Elastic.Transport;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Text.Json;
namespace ElasticsearchBulkIndex
{
public class EsHelper
{
private readonly ElasticsearchClient _esClient;
public EsHelper()
{
var node = new SingleNodePool(new Uri("http://192.168.220.130:31200"));
var settings = new ElasticsearchClientSettings(node).Authentication(new BasicAuthentication("elastic", "password"))
.EnableDebugMode().DefaultIndex("123");
_esClient = new ElasticsearchClient(settings);
}
public async Task<int> BulkIndex(List<JsonElement> jsonList)
{
var bulkRequest = new BulkRequest("index名称")
{
Operations = new List<IBulkOperation>()
};
foreach (var json in jsonList)
{
bulkRequest.Operations.Add(
new BulkIndexOperation<JsonElement>(json)
{
Id = json.TryGetProperty("id", out var id)
? id.GetInt32().ToString()
: null
}
);
}
BulkResponse response = await _esClient.BulkAsync(bulkRequest);
Console.WriteLine(response.ElasticsearchServerError);
Console.WriteLine(response.DebugInformation);
return response.Items.Count;
}
public async Task<bool> BulkDelete(List<string> ids)
{
var delete = new DeleteByQueryRequest("index名称")
{
Query = new IdsQuery()
{
Values=new Ids(ids)
}
};
DeleteByQueryResponse deleteResponse = await _esClient.DeleteByQueryAsync(delete);
Console.WriteLine(deleteResponse.DebugInformation);
Console.WriteLine(deleteResponse.IsSuccess());
return deleteResponse.IsSuccess();
}
}
}
- DI以及配置swagger
csharp
....
builder.Services.AddControllers();
builder.Services.AddSingleton<EsHelper>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
....
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger";
});
app.MapControllers();
app.Run();
- 修改launchsetting.json
bash
"launchUrl": "swagger"