ES 用 reindex 做数据迁移-从集群A 的数据,导入到 集群B

方案一: reindex

目标: 从集群A 的数据,导入到 集群B

reindex是Elasticsearch提供的一个api接口,可以把数据从源ES集群导入到当前的ES集群,同样实现了数据的迁移,限于腾讯云ES的实现方式,当前版本不支持reindex操作。简单介绍一下reindex接口的使用方式。

第一步: 修改本地(需要导入的集群B)

elasticsearch.yaml 修改后要重启(修改完之后重新启动服务)

添加:

reindex.remote.whitelist: ["up01:9200"]

第2步:执行

##2 . 调用reindex api  以下操作表示从源ES集群中查询名为test1的索引,查询条件为title字段为elasticsearch,将结果写入当前集群的test2索引

		POST _reindex
		{
  			"source": {
    			"remote": {
      				"host": "http://up01:9200/"
    			},
    			"index": "tfec_tbl_goods",
    			"size": 100  
  			},
  			"dest": {
   			 	"index": "tfec_tbl_goods"
  			}
		}

第3步: 数据校验:

看看数据量跟之前的是不是一样:

	GET _cat/indices/tfec_tbl_goods?v

补充:

es 官网和其他大佬处reindex 使用方法

POST _reindex
{
  "source": {
    "index": "old_index",
	"size": 5000
  },
  "dest": {
    "index": "new_index",
    "version_type": "internal"
	"routing": "=cat"
  }
}

POST _reindex{
	"conflicts": "proceed",	//有冲突继续,默认是有冲突终止
	"size":1000,	//设定条数  
	"source": {    
	    "index": "twitter", 	//也可以为 ["twitter", "blog"]
		"type": "tweet", 	// 或["type1","type2"] 	//红字限制范围 ,非必须  限制文档
		"query": { "term": { "user": "kimchy" } },	//添加查询来限制文档
		"sort": { "date": "desc" }, 	//排序
		"_source": ["user", "tweet"],	//指定字段
		"size": 100,	//滚动批次1000更改批处理大小:
	},  
  
    "dest": {    
		"index": "new_twitter",
		"op_type": "create",	//设置将导致_reindex只在目标索引中创建丢失的文档,create 只插入没有的数据
		"version_type": "external",	//没有设置 version_type或设置为internal 将覆盖掉相同id的数据,设置为external 将更新相同ID文档当version比较后的时候
		"routing": "=cat",	//将路由设置为cat
		"pipeline": "some_ingest_pipeline",	//指定管道来使用Ingest节点特性
    },
  
	"script": { // 执行脚本 
	   "source": "if (ctx._source.foo == 'bar') {ctx._version++; ctx._source.remove('foo')} ", 	                            					                      							 	
		"lang": "painless" 
	}
}




POST _reindex
{
  "conflicts": "proceed",
  "source": {
    "remote": {
      "host": "http://xxx.xxx.com:80",
      "username": "xxx",
      "password": "xxxxxxxxx",
      "socket_timeout": "1m",
      "connect_timeout": "30s"
    },
    "index": "case_cause",
    "size": 5000  //滚动批次1000更改批处理大小:
  },
  "dest": {
    "index": "case_cause",
    "op_type": "create",
    "routing": "=cat"
  }
}

我个人编写的批量执行脚本

本脚本为在本地es集群拉取远端es集群数据

#/bin/bash
#author wxd
#
PARAMETER=$1

ES_URL="http://172.16.248.21:9200"
ES_USER="xxx"
ES_PASSWORD="xxxxxxxxx"

usage() {
    echo -e "\033[46;31mUsage\033[0m: Please use \e[0;35m$0 foo \e[0m"    
    exit 1;
}

do_reindex(){
	for new_index in {parse_task,case_record,parse_check_record,adjustment,code_category,law_question,red_book,law_cloud_category,department,predict_department,predict_judge,cause_predict,law_macroforecast,case_num_data,court_data,indicator_data,standard_value,sub_indicator,cause_weight,cause_weight_backup,events_weight,weight_rate,case_edit,qid_cache,law_case_info_business,law_search_v3,spider_case,code,inner_case,law_doc_template,law,law_firm,institutions,predict_judge_data_v2,predict_judge_data}
	do
		echo -e "\n$(date '+%Y-%m-%d %H:%M:%S'), 索引\e[0;35m$new_index\e[0m  开始reindex"
		curl -POST -H "Content-Type: application/json" -s -u $ES_USER:$ES_PASSWORD "$ES_URL/_reindex" -d '
		{
		  "conflicts": "proceed",
		  "source": {
		    "remote": {
		      "host": "http://xxx.xxx.com:80",
		      "username": "xxxx",
		      "password": "xxxxxxxxxxxxx",
		      "socket_timeout": "1m",
		      "connect_timeout": "30s"
		    },
		    "index": "'$new_index'",
		    "size": 5000
		  },
		  "dest": {
		    "index": "'$new_index'",
		    "op_type": "create",
		    "routing": "=cat"
		  }
		}'
	echo -e "\n$(date '+%Y-%m-%d %H:%M:%S'), 索引\e[0;35m$new_index\e[0m  reindex完成"
	done	
	  
}


if [ -n "$PARAMETER" ]; then
   case "$PARAMETER" in
    foo)
        echo -e "\033[46;31m Do reindex!!!\033[0m"
        do_reindex
		echo -e "-----------------------------------------------\033[46;31mAll reindex done!!!\033[0m-----------------------------------------------"
        ;;  
    *)  
        usage
        exit
        ;;  
    esac
else
    echo -e "\033[46;31merror\033[0m: please input parameter"
fi

从源索引中随机取10条数据到新索引中。

POST _reindex
{
  "size": 10,
  "source": {
    "index": "twitter",
    "query": {
      "function_score" : {
        "query" : { "match_all": {} },
        "random_score" : {}
      }
    },
    "sort": "_score"    
  },
  "dest": {
    "index": "random_twitter"
  }
}

方案二:直接复制数据

需求:

有时候需要将Es里面的部分数据(以index为单位)转移到另一台主机下的es中,为了避免大量重复执行之前读取数据的耗时操作,只需要将对应的ES目录下的nodes目录中的相关数据文件夹拷贝到其他主机下的es中对应的nodes目录中即可

步骤:

第一步: 找到索引对应的文件夹名字:

第2步: 找到本地 存放数据的目录:

以对应拷贝index数据所存在的文件目录,如:books对应文件夹为---》uFdRtQv7Rf22pPC4--EWdg

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值