溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎樣解決Elasticsearch type 不一致導(dǎo)致寫入數(shù)據(jù)失敗的問題

發(fā)布時(shí)間:2021-12-06 15:01:18 來源:億速云 閱讀:258 作者:柒染 欄目:云計(jì)算

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎樣解決Elasticsearch type 不一致導(dǎo)致寫入數(shù)據(jù)失敗的問題,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

在 Elasticsearch 7.x 以前的版本中,當(dāng)ES Client寫數(shù)據(jù)的時(shí)候報(bào)了如下錯(cuò)誤:

2020-03-13 10:00:41.076 ERROR 9 --- [Report ES Thread 0] .g.c.c.AbstractElasticsearchReportClient : item: 2634ef87-ec48-4d38-899f-508ba8b69b9c, errorReason: Rejecting mapping update to [journal_test] as the final mapping would have more than 1 type: [default, doc]

意思就是說你寫入的數(shù)據(jù)的中的type 與你創(chuàng)建索引時(shí)指定type不一致。比如我創(chuàng)建時(shí) "_type":"_doc",而寫入時(shí)為 "_type":"default"。因此,有兩種辦法,要么修改寫入數(shù)據(jù)時(shí)的 type ,要么修改當(dāng)前索引的 type 。

不過在最新的 7.x 沒有該問題了,因?yàn)?nbsp;官方已經(jīng)把 type 功能移除了。

考慮到更改寫入時(shí)候的 type 就得重啟應(yīng)用,會影響用戶使用。所以這里采用修改當(dāng)前索引的方法。

提供一種最簡單的解決思路:通過 Elasticsearch 的 reindex 功能將當(dāng)前索引 journal_test 備份至 journal_test_back 索引并刪除 journal_test,讓 ES 根據(jù)寫入數(shù)據(jù)時(shí)自動生成 type 為 default 的索引。隨后將備份數(shù)據(jù)導(dǎo)出到文件,修改文件中 type 為 default。再寫入原來新的 journal_test_2 索引。

 

將 journal_test_2 索引數(shù)據(jù)備份至 journal_test_2_bak索引:

curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
 "source": {
   "index": "journal_test"
 },
 "dest": {
   "index": "journal_test_back"
 }
}
'
   

刪除 journal_test 索引,讓你的應(yīng)用再次寫入數(shù)據(jù)時(shí) ES 自己創(chuàng)建索引:

curl -X DELETE "http://localhost:9200/journal_test"
   

journal_test_back 數(shù)據(jù)導(dǎo)出到 my_index_mapping.json 文件中

這里借助工具:https://github.com/taskrabbit/elasticsearch-dump,將 備份數(shù)據(jù)導(dǎo)出到我本地 Desktop 目錄下:

docker run --rm -ti -v ~/Desktop:/tmp taskrabbit/elasticsearch-dump \
--input=http://localhost:9200/journal_test_back \
--output=/tmp/my_index_mapping.json \
--type=data
 

編輯 my_index_mapping.json 文件,將 type 修改為 default。注意,為了不影響原來的備份數(shù)據(jù),我將修改后的數(shù)據(jù)寫入到新的文件(my_index_mapping_default.json)中:

cat my_index_mapping.json| sed s/\"_type\":\"doc\"/\"_type\":\"default\"/ > my_index_mapping_default.json
   

最后一步,將正確的數(shù)據(jù)導(dǎo)入正確的 journal_test 索引中:

docker run --rm -ti -v ~/Desktop:/tmp taskrabbit/elasticsearch-dump \
--output=http://localhost:9200/journal_test \ # 注意索引名
--input=/tmp/my_index_mapping_default.json \ #注意這里使用的文件
--type=data
   

上述就是小編為大家分享的怎樣解決Elasticsearch type 不一致導(dǎo)致寫入數(shù)據(jù)失敗的問題了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI