json insert

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();
    }
}

}

Welcome to our community! :smiley:

What is the error?

python으로 했을때

c#으로 했을때_줄바꿈 문자 포함안되게 저장하고싶음.

The first picture is the result of running on Python. The second picture is the result of running with c#. I hope value is saved so that it doesn't include line-switched characters.

Please don't post pictures of text, they are difficult to read, impossible to search and replicate (if it's code), and some people may not be even able to see them :slight_smile:

I want to solve this problem. If you don't mind, I will send my code to your email address, so can you check it?

Sorry, I don't answer technical questions via email.
Feel free to put things on gist/pastebin/etc and link here.

I uploaded a new post, could you check it?
I am not good at English, so writing can be awkward.

Feel free to use our language specific categories - Elastic In Your Native Tongue - Discuss the Elastic Stack

thank you very much

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.