栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch7.x基于Java API 快照备份和恢复

ElasticSearch7.x基于Java API 快照备份和恢复

快照备份

1. 配置es环境
我这里用的是windowns版本的,在es官网下载压缩包
https://www.elastic.co/cn/downloads/elasticsearch
解压后在connfig找到elasticsearch.yml进行以下配置修改

    服务名称 cluster.name: my-application节点名称 node.name: node-1快照本地路径 path.repo: [E:/es/data]地址network.host: localhost端口 http.port: 9200

在 bin 目录下,找到elasticsearch.bat 双击 启动es

2.创建好springboot项目进行测试

依赖说一下,避免找不到对应jar包里的方法,当使用springboot + Elasticsearch时,需要明确指定依赖.

   
        7.9.3
    
     
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            ${elasticsearch.version}
        

        
            org.elasticsearch
            elasticsearch
            ${elasticsearch.version}
        

        
            org.apache.logging.log4j
            log4j-core
            2.17.0
        

        
            org.apache.logging.log4j
            log4j-api
            2.17.0
        

        
            junit
            junit
            test
        

        
            com.fasterxml.jackson.core
            jackson-databind
        
    创建快照仓库
 RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );
        System.out.println("es已成功链接");
        //创建快照仓库
        PutRepositoryRequest request = new PutRepositoryRequest();
        request.name("my_backup0001");
        request.type(FsRepository.TYPE);
        request.masterNodeTimeout(Timevalue.timevalueMinutes(1));
        request.verify(true);

        String locationKey = FsRepository.LOCATION_SETTING.getKey();
        String locationValue = "my_fs_data_location";
        String compressKey = FsRepository.COMPRESS_SETTING.getKey();
        boolean compressValue = true;

        Map map = new HashMap<>();
        map.put(locationKey, locationValue);
        map.put(compressKey, compressValue);
        request.settings(map);

        AcknowledgedResponse response = client.snapshot().createRepository(request, RequestOptions.DEFAULT);

        //确认是否响应
        boolean acknowledged = response.isAcknowledged();
        System.out.println("创建快照仓库是否成功:"+acknowledged);
        client.close();
        System.out.println("es已关闭");

测试结果

    创建快照
   RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );
        System.out.println("es已成功链接");
        //快照开始时间
        System.out.println("快照开始时间:" + new Date());
        //创建快照
        CreateSnapshotRequest request = new CreateSnapshotRequest();
        //快照仓库名称
        request.repository("my_backup0001");
        //快照名称
        String snapshotName = "backup"+System.currentTimeMillis();
        request.snapshot(snapshotName);
        //快照的索引 第一次全量备份,以后是增量备份
        request.indices("shopping_demo", "test_demo");
        request.indicesOptions(IndicesOptions.fromOptions(false, false, true, true));
        request.partial(false);
        request.includeGlobalState(true);
        request.masterNodeTimeout("1m");
        request.waitForCompletion(true);
        //同步请求客户端
        CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT);
        //快照信息
        SnapshotInfo snapshotInfo = response.getSnapshotInfo();
        //快照索引信息
        System.out.println(snapshotInfo.indices());
        //快照结束时间
        System.out.println("快照结束时间:" + snapshotInfo.endTime());
        //快照创建响应状态 200 ok
        RestStatus status = response.status();
        System.out.println("es快照响应状态:"+status);

        client.close();
        System.out.println("es已关闭");

测试结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/730242.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号