ElasticSearch 數(shù)據(jù)遷移工具elasticdump
Elasticdump 是一個用于導入和導出 Elasticsearch 數(shù)據(jù)的命令行工具。它提供了一種方便的方式來在不同的 Elasticsearch 實例之間傳輸數(shù)據(jù),或者進行數(shù)據(jù)備份和恢復。
使用 Elasticdump,你可以將 Elasticsearch 索引中的數(shù)據(jù)導出為 JSON 文件,或者將 JSON 文件中的數(shù)據(jù)導入到 Elasticsearch 索引中。它支持各種選項和過濾器,用于指定源和目標,包括索引模式、文檔類型、查詢過濾器等等。
主要特征包括
- 支持在Elasticsearch實例或者集群之間傳輸和備份數(shù)據(jù)??梢詫?shù)據(jù)從一個集群復制到另一個集群。
- 支持不同格式的數(shù)據(jù)傳輸,包括JSON、NDJSON、CSV、備份文件等。
- 可以通過命令行或者程序化的方式使用。命令行方式提供了便捷的操作接口。
- 支持增量式同步,只復制目標集群中不存在的文檔。
- 支持各種認證方式連接Elasticsearch,如basic auth、Amazon IAM等。
- 支持多線程操作,可以加快數(shù)據(jù)遷移的速度。
- 開源免費,代碼托管在GitHub上。
一、安裝node
首先獲取安裝包
wget https://nodejs.org/dist/v16.14.0/node-v16.14.0-linux-x64.tar.xz
tar axf node-v16.14.0-linux-x64.tar.xz -C /usr/local/
mv /usr/local/node-v16.14.0-linux-x64 /usr/local/node
然后配置環(huán)境變量
vim /etc/profile
export NODE_HOME=/usr/local/node
export PATH=$NODE_HOME/bin:$PATH
接下來刷新環(huán)境變量,然后測試一下安裝是否完成
source /etc/profile
node -v
npm -v
如果是mac 的話可以使用brew
安裝
brew install node@16
二、在線安裝elasticdump
執(zhí)行下面的命令安裝
npm install elasticdump -g
使用下面的命令查看安裝目錄
npm root -g
我的位置在這里/opt/homebrew/lib/node_modules
三、離裝elasticdump
這里的原理是將node安裝包和elasticdump安裝報復制到需要離線安裝的服務器。
- 獲取node 的離線安裝包進行安裝即可,參考第一步
- 獲取elasticdump的安裝包安裝,所以我們首選需要一個打包工具
npm install -g npm-pack-all
然后我們切換到上面elasticdump的安裝路,打包elasticdump,會在當前目錄生成elasticdump-6.103.0.tgz
這樣一個壓縮包,這就是我們離線安裝需要的包
cd /opt/homebrew/lib/node_modules/elasticdump/
npm-pack-all
到這里我們看到離線包已經(jīng)生成好了,接下來我們復制到我們之前已經(jīng)安裝好node 的機器上,執(zhí)行下面的命令
npm install elasticdump-6.103.0.tgz
后面為了方便使用,我們可以配置一下elasticdump的環(huán)境變量
vim ~/.bashrc
# 追加以下內容
#node
export DUMP_HOME=/opt/homebrew/lib/node_modules/elasticdump/
export PATH=$DUMP_HOME/bin:$PATH
# 刷新
source ~/.bashrc
四、使用elasticdump
這里的使用主要分為兩種,一種是數(shù)據(jù)備份,一種是數(shù)據(jù)遷移
- 備份主要指的是生成備份的數(shù)據(jù)文件,在需要的時候進行還原
- 數(shù)據(jù)遷移是指將原來索引里的數(shù)據(jù)遷移到新的索引
其實數(shù)據(jù)備份也能達到數(shù)據(jù)遷移的目的,但是在兩個環(huán)境的網(wǎng)絡不通的時候我們只能使用數(shù)據(jù)備份
我們安裝成功后,在elasticdump的bin目錄下其實有兩個工具,一個是elasticdump
另外一個是multielasticdump
數(shù)據(jù)遷移
遷移索引
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=http://192.168.1.141:9200/target_index \
--type=mapping
遷移數(shù)據(jù)
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=http://192.168.1.141:9200/target_index \
--type=data \
--limit=2000 # 每次操作的objects數(shù)量,默認100,數(shù)據(jù)量大的話,可以調大加快遷移速度
這個命令會將源 Elasticsearch 實例中的 “my_index” 索引的所有數(shù)據(jù)導出,并保存到 “/path/to/output.json” 的 JSON 文件中。
-
--input
:指定輸入的 Elasticsearch 實例和索引。可以是一個 URL,也可以是本地 Elasticsearch 實例的路徑。 -
--output
:指定輸出的文件路徑,數(shù)據(jù)將保存為一個 JSON 文件。 -
--type
:指定要導出的數(shù)據(jù)類型,通常為 “data” 表示文檔數(shù)據(jù)。
你還可以使用其他選項來進一步控制導出過程,如 --query
, --size
, --limit
, --filter
等,具體取決于你的需求??梢酝ㄟ^運行 elasticdump --help
命令來
數(shù)據(jù)備份
導出索引和數(shù)據(jù)
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=/data/source_index_mapping.json \
--type=mapping
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=/data/source_index.json \
--type=data \
--limit=2000
導入索引和數(shù)據(jù)
elasticdump \
--input=/data/source_index_mapping.json \
--output=http://192.168.1.141:9200/source_index \
--type=mapping
elasticdump \
--input=/data/source_index.json \
--output=http://192.168.1.141:9200/source_index \
--type=data \
--limit=2000
其他用法
還有其他使用的細節(jié),例如壓縮,指定query 什么的,我們可以參考下面的例子
# Copy an index from production to staging with analyzer and mapping:
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=analyzer
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=mapping
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=data
# Backup index data to a file:
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index_mapping.json \
--type=mapping
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.json \
--type=data
# Backup and index to a gzip using stdout:
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=$ \
| gzip > /data/my_index.json.gz
# Backup the results of a query to a file
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=query.json \
--searchBody="{\"query\":{\"term\":{\"username\": \"admin\"}}}"
# Specify searchBody from a file
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=query.json \
--searchBody=@/data/searchbody.json
# Copy a single shard data:
elasticdump \
--input=http://es.com:9200/api \
--output=http://es.com:9200/api2 \
--input-params="{\"preference\":\"_shards:0\"}"
# Backup aliases to a file
elasticdump \
--input=http://es.com:9200/index-name/alias-filter \
--output=alias.json \
--type=alias
# Import aliases into ES
elasticdump \
--input=./alias.json \
--output=http://es.com:9200 \
--type=alias
# Backup templates to a file
elasticdump \
--input=http://es.com:9200/template-filter \
--output=templates.json \
--type=template
# Import templates into ES
elasticdump \
--input=./templates.json \
--output=http://es.com:9200 \
--type=template
# Split files into multiple parts
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.json \
--fileSize=10mb
# Import data from S3 into ES (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input "s3://${bucket_name}/${file_name}.json" \
--output=http://production.es.com:9200/my_index
# Export ES data to S3 (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input=http://production.es.com:9200/my_index \
--output "s3://${bucket_name}/${file_name}.json"
# Import data from MINIO (s3 compatible) into ES (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input "s3://${bucket_name}/${file_name}.json" \
--output=http://production.es.com:9200/my_index
--s3ForcePathStyle true
--s3Endpoint https://production.minio.co
# Export ES data to MINIO (s3 compatible) (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input=http://production.es.com:9200/my_index \
--output "s3://${bucket_name}/${file_name}.json"
--s3ForcePathStyle true
--s3Endpoint https://production.minio.co
# Import data from CSV file into ES (using csvurls)
elasticdump \
# csv:// prefix must be included to allow parsing of csv files
# --input "csv://${file_path}.csv" \
--input "csv:///data/cars.csv"
--output=http://production.es.com:9200/my_index \
--csvSkipRows 1 # used to skip parsed rows (this does not include the headers row)
--csvDelimiter ";" # default csvDelimiter is ','
常用參數(shù)
--direction dump/load 導出/導入
--ignoreType 被忽略的類型,data,mapping,analyzer,alias,settings,template
--includeType 包含的類型,data,mapping,analyzer,alias,settings,template
--suffix 加前綴,es6-${index}
--prefix 加后綴,${index}-backup-2018-03-13
總結
elasticdump是ElasticSearch提供的一個工具,我們主要可以用來完成
- 數(shù)據(jù)備份
- 數(shù)據(jù)遷移
這一節(jié)我們主要介紹了elasticdump的安裝和使用,還有就是,Elasticdump 是一個第三方工具,不是官方的 Elasticsearch 產(chǎn)品。雖然它對某些用例很有幫助,但在使用之前,確保與你的 Elasticsearch 版本兼容,并查閱工具的文檔以了解任何特定的注意事項或限制。文章來源:http://www.zghlxwxcb.cn/news/detail-575034.html
總體來說,elasticdump是一個非常實用的數(shù)據(jù)遷移和備份工具。它可以幫助我們輕松地在不同Elasticsearch集群之間進行數(shù)據(jù)遷移,實現(xiàn)集群之間的無縫數(shù)據(jù)同步。文章來源地址http://www.zghlxwxcb.cn/news/detail-575034.html
到了這里,關于ElasticSearch 數(shù)據(jù)遷移工具elasticdump的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!