ElasticSearch7.4批量匯入_bulk

chuixue24發表於2020-01-19
請求的每一行包含描述操作說明
JSON 物件,第二行為 JSON 物件本身。可以把第一行視為資訊行,第二類為資料行。唯一的例
外是 delete 操作,它只包含資訊行。來看看下面的例子:
{ "index": { "_index": "addr", "_type": "contact", "_id": 1 }}
{ "name": "Fyodor Dostoevsky", "country": "RU" }
{ "create": { "_index": "addr", "_type": "contact", "_id": 2 }}
{ "name": "Erich Maria Remarque", "country": "DE" }
{ "create": { "_index": "addr", "_type": "contact", "_id": 2 }}
{ "name": "Joseph Heller", "country": "US" }
{ "delete": { "_index": "addr", "_type": "contact", "_id": 4 }}
{ "delete": { "_index": "addr", "_type": "contact", "_id": 1 }}
重要的是,每一個文件或操作說明放置在一行中(以換行符結束)。這意味著無法美化文件格
式。批量索引檔案的大小存在限制,它被設定為 100 MB ,在 Elasticsearch 配置檔案中可以通過
http.max_content_length 屬性來改變。這避免了請求過大時可能存在的請求超時及記憶體問題。

 

curl -X POST http://localhost:9200/_bulk -H Content-Type:application/json --data-binary @D:\DevEnv\curl-7.68.0-win64-mingw\bin\xx.json

需要匯入的檔案xx.json如下:

 { "index": {"_index": "library", "_type": "book", "_id": "1"}}
{ "title": "All Quiet on the Western Front","otitle": "Im Westen nichts Neues","author": "Erich Maria Remarque","year":  1929,"characters": ["Paul Bäumer", "Albert Kropp", "Haie  Westhus", "Fredrich Müller", "Stanislaus Katczinsky", "Tjaden"],"tags": ["novel"],"copies": 1, "available": true,  "section" : 3}
{ "index": {"_index": "library", "_type": "book", "_id": "2"}}
{ "title": "Catch-22","author": "Joseph Heller","year":  1961,"characters": ["John Yossarian", "Captain Aardvark",  "Chaplain Tappman", "Colonel Cathcart", "Doctor  Daneeka"],"tags": ["novel"],"copies": 6, "available" : false,  "section" : 1}
{ "index": {"_index": "library", "_type": "book", "_id": "3"}}
{ "title": "The Complete Sherlock Holmes",  "author": "Arthur Conan Doyle","year": 1936,"characters":  ["Sherlock Holmes","Dr. Watson", "G. Lestrade"],"tags":  [],"copies": 0, "available" : false, "section" : 12}
{ "index": {"_index": "library", "_type": "book", "_id": "4"}}
{ "title": "Crime and Punishment","otitle": "Преступлéние и  наказáние","author": "Fyodor Dostoevsky","year":  1886,"characters": ["Raskolnikov", "Sofia Semyonovna  Marmeladova"],"tags": [],"copies": 0, "available" : true}

注意需要匯入的檔案行與行之間要以換行符相隔,如果匯入時提示出錯,請將檔案內容都歸為一行,然後每一行之間以enterEnter鍵相隔開,然後再匯入。

也可以用postman進行匯入:

 

相關文章