我希望对Elasticsearch索引的一些记录(例如最近的100万条记录)进行备份,并在另一台计算机上恢复此备份。如果可以使用可用的/内置的Elasticsearch特性来实现这一点,那就更好了。
我尝试过Elasticsearch快照和还原(如下代码),但看起来它需要备份整个索引,而不是选择性记录。
curl -H 'Content-Type: application/json' -X PUT "localhost:9200/_snapshot/es_data_dump?pretty=true" -d '
{
"type": "fs",
"settings": {
"compress" : true,
"location": "es_data_dump"
}
}'
curl -H 'Content-Type: application/json' -X PUT "localhost:9200/_snapshot/es_data_dump/snapshot1?wait_for_completion=true&pretty=true" -d '
{
"indices" : "index_name",
"type": "fs",
"settings": {
"compress" : true,
"location": "es_data_dump"
}
}'备份的格式可以是任何格式,只要可以在不同的计算机上成功还原即可。
发布于 2019-08-05 14:59:54
最后,我使用python驱动程序获取了所需的数据,因为对于给定的用例,这是我发现最简单的。
为此,我运行了一个Elasticsearch查询,并将其响应以换行符分隔的格式存储在一个文件中,然后使用另一个python脚本从其中恢复数据。这种方式最多返回10000个条目,以及用于获取下一个10000个条目的滚动ID,依此类推。
es = Elasticsearch(timeout=30, max_retries=10, retry_on_timeout=True)
page = es.search(index=['ct_analytics'], body={'size': 10000, 'query': _query, 'stored_fields': '*'}, scroll='5m')
while len(page['hits']['hits']) > 0:
es_data = page['hits']['hits'] #Store this as you like
scrollId = page['_scroll_id']
page = es.scroll(scroll_id=scrollId, scroll='5m')发布于 2019-05-29 18:28:17
您可以使用_reinex接口。它可以接受任何查询。在重新编制索引之后,您有一个新的索引作为备份,其中包含请求的记录。轻松地将其复制到您想要的任何位置。
完整的信息在这里:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html
https://stackoverflow.com/questions/56357634
复制相似问题