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 sys
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():
# 파일 open 후 읽어오기?
file = open('me.json')
jsonString = json.load(file)
# key 값 추출
keys = [
key for key in jsonString
]
print(keys)
# i = 0 # 변수 초기화
x = range(len(keys))
# 키 값에 따른 객체 가져온 후 값 insert
for i in x:
jsonArray = jsonString.get(keys[i])
# print(jsonArray)
for list in jsonArray:
yield {
"_index": "jsoninsert3",
"_source": {
keys[i] : list
}
}
# print(list)
i = i + 1
bulk(es, gendata())
gendata()
c#
// using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elasticsearch.Net;
using Nest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace jsonParsing
{
class Program
{
public class IotDatabase
{
public JToken K { get; set; }
public JToken V { get; set; }
public virtual JToken First { get; }
public static explicit operator JToken(IotDatabase v)
{
throw new NotImplementedException();
}
}
static void Main(string args)
{
var defaultIndex = "jsoninsert";
var pool = new SingleNodeConnectionPool(new Uri("http://id:password@ipAddress:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
String path = @"C:\me.json";
JObject obj = JObject.Parse(File.ReadAllText(path));
IList keys = obj.Properties().Select(p => p.Name).ToList();
// 키 추출
List key_list = new List();
var z = keys.Count();
List list5 = new List();
for (var i = 0; i < z; i++)
{
Console.WriteLine(keys[i]);
JToken key_list2 = keys[i];
JToken key_value_list = obj[keys[i]];
JToken iotDatabase = (JToken) new IotDatabase { K = (JToken)key_list2, V = (JToken)key_value_list };
list5.Add(iotDatabase);
}
// bulk 삽입
var bulkAllObservable = client.BulkAll(list5, b => b
.Index("jsoninsert")
.BackOffTime("30s") // 재시도 간의 대기시간
.BackOffRetries(2)
.Size(100000) // 대량 요청 당 항목?
)
.Wait(TimeSpan.FromMinutes(15), next =>
{
});
Console.ReadLine();
}
}
}