programing

Elastic Search 벌크 인덱스 JSON 데이터

oldcodes 2023. 3. 25. 11:52
반응형

Elastic Search 벌크 인덱스 JSON 데이터

JSON 파일을 새 Elastic Search 인덱스로 일괄 인덱싱하려고 하는데 인덱싱할 수 없습니다.JSON 내부에 다음과 같은 샘플 데이터가 있습니다.

[{"Amount": "480", "Quantity": "2", "Id": "975463711", "Client_Store_sk": "1109"},
{"Amount": "2105", "Quantity": "2", "Id": "975463943", "Client_Store_sk": "1109"},
{"Amount": "2107", "Quantity": "3", "Id": "974920111", "Client_Store_sk": "1109"},
{"Amount": "2115", "Quantity": "2", "Id": "975463798", "Client_Store_sk": "1109"},
{"Amount": "2116", "Quantity": "1", "Id": "975463827", "Client_Store_sk": "1109"},
{"Amount": "648", "Quantity": "3", "Id": "975464139", "Client_Store_sk": "1109"},
{"Amount": "2126", "Quantity": "2", "Id": "975464805", "Client_Store_sk": "1109"},
{"Amount": "2133", "Quantity": "1", "Id": "975464061", "Client_Store_sk": "1109"},
{"Amount": "1339", "Quantity": "4", "Id": "974919458", "Client_Store_sk": "1109"},
{"Amount": "1196", "Quantity": "5", "Id": "974920538", "Client_Store_sk": "1109"},
{"Amount": "1198", "Quantity": "4", "Id": "975463638", "Client_Store_sk": "1109"},
{"Amount": "1345", "Quantity": "4", "Id": "974919522", "Client_Store_sk": "1109"},
{"Amount": "1347", "Quantity": "2", "Id": "974919563", "Client_Store_sk": "1109"},
{"Amount": "673", "Quantity": "2", "Id": "975464359", "Client_Store_sk": "1109"},
{"Amount": "2153", "Quantity": "1", "Id": "975464511", "Client_Store_sk": "1109"},
{"Amount": "3896", "Quantity": "4", "Id": "977289342", "Client_Store_sk": "1109"},
{"Amount": "3897", "Quantity": "4", "Id": "974920602", "Client_Store_sk": "1109"}]

사용하고 있다

 curl -XPOST localhost:9200/index_local/my_doc_type/_bulk --data-binary --data @/home/data1.json 

Elastic Search에서 표준 벌크 인덱스 API를 사용하려고 하면 이 오류가 나타납니다.

 error: {"message":"ActionRequestValidationException[Validation Failed: 1: no requests added;]"}

이런 유형의 JSON 인덱스에 대해 도움을 줄 수 있는 사람이 있습니까?

이 JSON 파일을 읽은 다음 엔드포인트에서 예상하는 형식으로 대량 요청을 작성합니다. 즉, 명령어 행과 문서 행이 각각 새 행 문자로 구분됩니다.각 문서에 대해 헹군 후 반복합니다.

curl -XPOST localhost:9200/your_index/_bulk -d '
{"index": {"_index": "your_index", "_type": "your_type", "_id": "975463711"}}
{"Amount": "480", "Quantity": "2", "Id": "975463711", "Client_Store_sk": "1109"}
{"index": {"_index": "your_index", "_type": "your_type", "_id": "975463943"}}
{"Amount": "2105", "Quantity": "2", "Id": "975463943", "Client_Store_sk": "1109"}
... etc for all your documents
'

꼭 교환해 주세요.your_index그리고.your_type실제 색인 및 유형 이름을 입력합니다.

갱신하다

명령줄을 단축할 수 있습니다._index그리고._typeURL 에 지정되어 있는 경우는, 을 참조해 주세요.제거할 수도 있습니다._id매핑에서 id 필드의 경로를 지정한 경우(단, ES 2.0에서는 이 기능이 사용되지 않습니다).적어도 커맨드 라인은{"index":{}}모든 문서에 대해 수행하려는 작업 유형을 지정하려면 항상 필수입니다(이 경우).index문서)

업데이트 2

curl -XPOST localhost:9200/index_local/my_doc_type/_bulk --data-binary  @/home/data1.json

/home/data1.json는 다음과 같습니다.

{"index":{}}
{"Amount": "480", "Quantity": "2", "Id": "975463711", "Client_Store_sk": "1109"}
{"index":{}}
{"Amount": "2105", "Quantity": "2", "Id": "975463943", "Client_Store_sk": "1109"}
{"index":{}}
{"Amount": "2107", "Quantity": "3", "Id": "974920111", "Client_Store_sk": "1109"}

업데이트 3

UPDATE 2에서 언급된 새로운 json 스타일 파일을 생성하는 방법은 이 답변을 참조하십시오.

업데이트 4

ES 7.x에서는doc_type더 이상 필요없고 간단하게 해야 한다._doc대신my_doc_typeES 8.x 에서는, 문서 타입은 완전하게 삭제됩니다.자세한 내용은 여기를 참조하십시오.

현재 6.1.2는 Elastic Search의 최신 버전이며 Windows(x64)에서 작동하는 curl 명령은 다음과 같습니다.

curl -s -XPOST localhost:9200/my_index/my_index_type/_bulk -H "Content-Type: 
application/x-ndjson" --data-binary @D:\data\mydata.json

mydata.json에 있어야 하는 데이터의 형식은 @val의 답변과 동일합니다.

유효한 Elasticsearch 벌크 API 요청은 다음과 같습니다(새 행으로 끝남).

POST http://localhost:9200/products_slo_development_temp_2/productModel/_bulk

{ "index":{ } } 
{"RequestedCountry":"slo","Id":1860,"Title":"Stol"} 
{ "index":{ } } 
{"RequestedCountry":"slo","Id":1860,"Title":"Miza"} 

Elasticsearch 벌크 API 매뉴얼:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

이렇게 하는 거야

POST http 요구를 송신합니다.urihttp 요청의 URI/URL로 valable이 됩니다.elasticsearchJsonvariable Elasticsearch "api" "http" "JSON"으로 요청 입니다.

var uri = @"/" + indexName + "/productModel/_bulk";
var json = JsonConvert.SerializeObject(sqlResult);
var elasticsearchJson = GetElasticsearchBulkJsonFromJson(json, "RequestedCountry");

Elasticsearch 벌크 API에 필요한 json 형식을 생성하는 도우미 메서드:

public string GetElasticsearchBulkJsonFromJson(string jsonStringWithArrayOfObjects, string firstParameterNameOfObjectInJsonStringArrayOfObjects)
{
  return @"{ ""index"":{ } } 
" + jsonStringWithArrayOfObjects.Substring(1, jsonStringWithArrayOfObjects.Length - 2).Replace(@",{""" + firstParameterNameOfObjectInJsonStringArrayOfObjects + @"""", @" 
{ ""index"":{ } } 
{""" + firstParameterNameOfObjectInJsonStringArrayOfObjects + @"""") + @"
";
}

의 첫 속성는 입니다.RequestedCountry이 예에서 사용하는 이유는 속성입니다.

productModel탄력검색입니다. sqlResultC#으로 하다

엘라스틱 서치 7.x로 하다. _type을 사용하다다른 사용자가 언급한 것처럼 파일을 프로그램적으로 읽고 다음과 같이 요청 본문을 구성할 수 있습니다.에는, 「」, 「json」, 「json」이 붙어 있는 것을 알 수 .Id그래서문서 ID할 수 ._id아트리뷰트된 ★★★_bulkAPI를 사용합니다.

방식: HTTP 방법:POST

URI:/<index_name>/_bulk

요청 본문(새 행으로 끝나야 함):

{"index":{"_id": "975463711"}}
{"Amount": "480", "Quantity": "2", "Id": "975463711", "Client_Store_sk": "1109"}
{"index":{"_id": "975463943"}}
{"Amount": "2105", "Quantity": "2", "Id": "975463943", "Client_Store_sk": "1109"}

언급URL : https://stackoverflow.com/questions/33340153/elasticsearch-bulk-index-json-data

반응형