I want to write the same c# code as the Python code below.
But there was an error. I don’t' know the reason.
python
'''
import json
import time
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
ES_ENDPOINT = "http://id:password@ip주소:9200"
es = Elasticsearch(ES_ENDPOINT, timeout=300, max_retries=10, retry_on_timeout=True)
def gendata():
file = open('me.json')
jsonString = json.load(file)
keys = [
key for key in jsonString
]
print(keys)
x = range(len(keys))
start = time.time()
i = 0
for i in x:
jsonArray = jsonString.get(keys[i])
for list in jsonArray:
yield {
"_index": "jsoninsert_python2",
"_source": {
keys[i] : list
}
}
i = i + 1
print("시간: {}초".format(time.time() - start))
bulk(es, gendata())
gendata()
'''
python results
c#
'''
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Elasticsearch.Net;
using Nest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace jsonParsing
{
class Program
{
public class IotDatabase
{
public string siteID { get; set; }
public string bimID { get; set; }
public string K { get; set; }
public string V { get; set; }
}
static void Main(string[] args)
{
// 엘라스틱서치 연동
var defaultIndex = "jsoninsert_c4";
var pool = new SingleNodeConnectionPool(new Uri("http://id:password@ip주소:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
// json 파싱
String path = @"C:\Users\me.json";
JObject obj = JObject.Parse(File.ReadAllText(path));
// list 선언
var list = new List<IotDatabase>();
// 키 추출
IList<string> keys = obj.Properties().Select(p => p.Name).ToList();
var c = keys.Count();
for (var i = 0; i < c; i++)
{
string k = keys[i];
JToken v = (JToken)obj[keys[i]];
JArray v1 = (JArray)v;
foreach (var x in v1)
{
var iotDatabase = new IotDatabase {
//siteID = "DTAAS",
//bimID = "TestBIM",
K = k,
V = x.ToString()
};
list.Add(iotDatabase);
}
}
// bulk 삽입
var bulkAllObservable = client.BulkAll(list, b => b
.Index("jsoninsert_c4")
.BackOffTime("30s") // 재시도 간의 대기시간
.BackOffRetries(2)
.Size(100000) // 대량 요청 당 항목?
)
.Wait(TimeSpan.FromMinutes(15), next =>
{
});
Console.ReadLine();
}
}
}
'''
c# results
I want to insert it so that it doesn't include line-switched characters like the Python result picture.