如何使用elasticsearch.net批量API按字节数组导入json文件到elasticsearch?
How to use elasticsearch.net bulk API to import json file by btye array to elasticsearch?
我有一些 json 文件需要导入到 elasticsearch。
我使用 curl api。下面是示例,对我来说效果很好。
curl -XPOST http://localhost:9200/index_local/_doc/_bulk -H "Content-Type: application/json" --data-binary @sample.json
我使用 HttpWebRequest 进行模拟,它对我来说也很好用。
public void Post(string fileName)
{
try
{
// get the byte array
var data = File.ReadAllBytes(fileName);
// create HttpRequest
var httpRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:9200/index_local/_doc/_bulk");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
httpRequest.ContentLength = data.Length;
// set the file byte array to the stream
using (var requestStream = httpRequest.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
// get the response
using (var response = httpRequest.GetResponse() as HttpWebResponse)
{
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
// read the result
Console.WriteLine(responseStream.ReadToEnd());
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
但是我找不到批量 api 导入 json 文件 elasticsearch.net.
是否有一些功能等于 HttpWebRequest 可以 post json 文件到 elasticsearch?
elasticsearch.net 库 ElasticLowLevelClient 或 ElasticClient 是否支持使用 btye 数组导入 json 文件?
假设 sample.json
是一个 JSON 文件,其结构对批量 API 有效,您可以使用
发送请求
var client = new ElasticClient();
var bytes = File.ReadAllBytes("sample.json");
var bulkResponse = client.LowLevel.Bulk<BulkResponse>(
bytes,
new BulkRequestParameters
{
RequestConfiguration = new RequestConfiguration
{
RequestTimeout = TimeSpan.FromMinutes(3)
}
});
if (!bulkResponse.IsValid)
{
// handle failure
}
这会为此请求设置一个特定的请求超时,如果批量大于正常请求(通常是这样),您可以将其设置为大于正常的值。如果 sample.json
大于 5MB 左右,您可以考虑按行对(批量操作和文档)批量读取文件,并作为多个批量请求发送。
我有一些 json 文件需要导入到 elasticsearch。
我使用 curl api。下面是示例,对我来说效果很好。
curl -XPOST http://localhost:9200/index_local/_doc/_bulk -H "Content-Type: application/json" --data-binary @sample.json
我使用 HttpWebRequest 进行模拟,它对我来说也很好用。
public void Post(string fileName)
{
try
{
// get the byte array
var data = File.ReadAllBytes(fileName);
// create HttpRequest
var httpRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:9200/index_local/_doc/_bulk");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
httpRequest.ContentLength = data.Length;
// set the file byte array to the stream
using (var requestStream = httpRequest.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
// get the response
using (var response = httpRequest.GetResponse() as HttpWebResponse)
{
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
// read the result
Console.WriteLine(responseStream.ReadToEnd());
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
但是我找不到批量 api 导入 json 文件 elasticsearch.net.
是否有一些功能等于 HttpWebRequest 可以 post json 文件到 elasticsearch?
elasticsearch.net 库 ElasticLowLevelClient 或 ElasticClient 是否支持使用 btye 数组导入 json 文件?
假设 sample.json
是一个 JSON 文件,其结构对批量 API 有效,您可以使用
var client = new ElasticClient();
var bytes = File.ReadAllBytes("sample.json");
var bulkResponse = client.LowLevel.Bulk<BulkResponse>(
bytes,
new BulkRequestParameters
{
RequestConfiguration = new RequestConfiguration
{
RequestTimeout = TimeSpan.FromMinutes(3)
}
});
if (!bulkResponse.IsValid)
{
// handle failure
}
这会为此请求设置一个特定的请求超时,如果批量大于正常请求(通常是这样),您可以将其设置为大于正常的值。如果 sample.json
大于 5MB 左右,您可以考虑按行对(批量操作和文档)批量读取文件,并作为多个批量请求发送。