ElasticSearch Java API:Snapshot操作

2021-09-10 18:05:58 浏览数 (1)

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.8/_snapshot_apis.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/snapshot-restore-apis.html

获取快照仓库

代码语言:javascript复制
/**
 * 获取快照仓库
 * @param request
 * @return
 * @throws IOException
 */
public List<RepositoryMetaData> getRepositoriesResponse(GetRepositoriesRequest request) throws IOException {
    List<RepositoryMetaData> response = restHighLevelClient.snapshot().getRepository(request, RequestOptions.DEFAULT).repositories();
    return response;
}

新建快照仓库

代码语言:javascript复制
/**
 * 新建快照仓库
 * @param request
 * @return
 * @throws IOException
 */
public AcknowledgedResponse putRepository(PutRepositoryRequest request) throws IOException{
    AcknowledgedResponse response = restHighLevelClient.snapshot().createRepository(request, RequestOptions.DEFAULT);
    return response;
}

获取快照

代码语言:javascript复制
/**
 * 获取快照信息
 */
public List<SnapshotInfo> getSnapshots(String repositoryName, String[] snapshotName) throws IOException {

    GetSnapshotsRequest request = new GetSnapshotsRequest();
    request.repository(repositoryName);
    request.snapshots(snapshotName);
    request.ignoreUnavailable(true); // 在快照不可用是,不会抛出异常

    return snapshotDao.getSnapshots(request);
}

/**
 * 获取快照
 * @param request
 * @return
 * @throws IOException
 */
public List<SnapshotInfo> getSnapshots(GetSnapshotsRequest request) throws IOException{

    GetSnapshotsResponse response = restHighLevelClient.snapshot().get(request, RequestOptions.DEFAULT);

    List<SnapshotInfo> snapshotsInfos = response.getSnapshots();

    return snapshotsInfos;
}

删除快照

代码语言:javascript复制
/**
 * 删除快照
 * @param repositoryName
 * @param snapshotName
 * @return
 * @throws IOException
 */
public boolean acknowledged(String repositoryName, String snapshotName) throws IOException{
    DeleteSnapshotRequest request = new DeleteSnapshotRequest(repositoryName);
    request.snapshot(snapshotName);

    return snapshotDao.acknowledged(request);
}

/**
 * 删除快照
 * @return
 * @throws IOException
 */
public boolean acknowledged(DeleteSnapshotRequest request) throws IOException{
    AcknowledgedResponse response = restHighLevelClient.snapshot().delete(request, RequestOptions.DEFAULT);

    boolean acknowledged = response.isAcknowledged();

    return acknowledged;
}

新建快照

代码语言:javascript复制
/**
 * 删除快照
 * @param repositoryName
 * @param snapshotName
 * @return
 * @throws IOException
 */
public RestStatus createSnapshot(String repositoryName, String snapshotName, List<String> indices) throws IOException{
    CreateSnapshotRequest request = new CreateSnapshotRequest();
    // 仓库名
    request.repository(repositoryName);
    // 快照名
    request.snapshot(snapshotName);
    // 索引名
    request.indices(indices);
    // 将还原快照中的所有数据流和索引,但不还原群集状态。
    request.includeGlobalState(false);
    // ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices,
    // forbidClosedIndices, ignoreAliases, ignoreThrottled
    request.indicesOptions(IndicesOptions.fromOptions(false, false, true, true));
    // Waits for the snapshot to be completed before a response is returned
    request.waitForCompletion(true);

    return snapshotDao.createSnapshot(request);
}

/**
 * 新建快照
 * @param request
 * @return
 */
public RestStatus createSnapshot(CreateSnapshotRequest request) throws IOException{
    CreateSnapshotResponse response = restHighLevelClient.snapshot().create(request, RequestOptions.DEFAULT);

    RestStatus status = response.status();
    return status;
}

获取快照状态

代码语言:javascript复制
/**
 * 获取快照状态
 * @param repositoryName
 * @param snapshotName
 * @return
 */
public List<SnapshotStatus> snapshotsStatus(String repositoryName, String snapshotName) throws IOException{
    SnapshotsStatusRequest request = new SnapshotsStatusRequest();
    request.repository(repositoryName);
    String [] snapshots = new String[] {snapshotName};
    request.snapshots(snapshots);
    request.ignoreUnavailable(true);

    return snapshotDao.snapshotsStatus(request);
}

0 人点赞