由于有时候需要将部分数据在不同的环境之间同步,需要在ES中迁移指定条件的数据,本例演示从测试环境A迁移某索引部分数据到测试环境B。
my_index_copy
,我的源数据索引为测试环境A的testsort_20221019
PUT my_index_copy
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"analysis": {
"analyzer": {
"common": {
"type": "pattern",
"pattern": ";"
}
}
}
}
}
<font color='red'> 注意:通过GET可以获取一个索引的mapping,然后将mappings里面的内容全部复制下来即可</font>
PUT /my_index_copy/_mapping
{
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
},
"keyword2": {
"type": "keyword"
}
},
"analyzer": "ik_smart"
}
}
}
<font color='red'>注意"my_index_copy/_mapping"前面的"/"不用填</font>
创建完成字段后在“概览”中可以看到新建的索引
在"基本查询"中设置好条件,获取查询语句,替换"query"下的条件
执执行下面的语句可以将testsort_20221019
索引中 name = "张飞" 的所有内容都复制到目标索引my_index_copy
POST _reindex
{
"source": {
"index": "testsort_20221019",
"size": 5000,
"query": {
"bool": {
"must": [
{
"match": {
"name": "张飞"
}
}
],
"must_not": [],
"should": []
}
}
},
"dest": {
"index": "my_index_copy"
}
}
整理好语句后,开始复制数据到新建的索引中
复制完数据后,查看my_index_copy
的UUID,复制这个uuid
通过UUID找到my_index_copy
的数据文件【数据文件目录使用UUID命名】,打包后再复制到开发环境path.data
more /usr/local/elasticsearch-7.8.0/config/elasticsearch.yml |grep data
# Path to directory where to store the data (separate multiple locations by comma):
path.data: /mydata/elasticsearch-7.8.0/logs
path.logs: /mydata/elasticsearch-7.8.0/data
indices.fielddata.cache.size: 40%
indices.breaker.fielddata.limit: 60%
其中path.data
指定了数据存放位置/mydata/elasticsearch-7.8.0/logs/nodes/0/indices/
如果配置文件中没有指定path.data,那么文件就存储在elasticsearch家目录下的data目录下面
在测试环境A中压缩文件,然后将压缩包传输到测试环境B继续后续操作。
[root@luoqiu indices]# ll -d NS5Wq50_STWqc7K_QZnmjA/
drwxrwxr-x 6 es es 47 Dec 13 17:22 NS5Wq50_STWqc7K_QZnmjA/
[root@luoqiu indices]# tar zcf NS5Wq50_STWqc7K_QZnmjA.tgz NS5Wq50_STWqc7K_QZnmjA/
在测试环境B解压后授权es
用户所有者权限
[root@luoqiu-ceshi indices]# tar zxf NS5Wq50_STWqc7K_QZnmjA.tgz
[root@luoqiu-ceshi indices]# chown -R es.es NS5Wq50_STWqc7K_QZnmjA
如果执行es的语句需要密码,可以拷贝到工具中执行,或者加上--user elastic:password
执行
详情可以参考《深入了解Elasticsearch》的第一篇文章。
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/_cluster/settings -d'
{
"persistent": {
"cluster.routing.allocation.enable": "none"
}
}'
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/_cluster/settings -d'
{
"persistent": {
"cluster.routing.allocation.enable": "all"
}
}'
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/_cluster/settings -d'
{
"transient" : {
"cluster.routing.allocation.enable" : "none"
}
}'
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/_cluster/settings -d'
{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}'
最后重启elasticsearch服务即可看到迁移后的数据了。
迁移指定数据的教程就结束了,如果像把迁移后的数据导入到原来的旧索引中,同样使用_reindex
即可,不指定条件(将query和里面的内容删除)即可将所有刚刚同步的数据导入原来的旧索引中。
{
"source": {
"index": "my_index_copy",
"size": 5000
},
"dest": {
"index": "my_index_old"
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。