快照備份
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時,需要明確指定依賴.
<properties>
<elasticsearch.version>7.9.3</elasticsearch.version>
</properties>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
- 創建快照倉庫
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<String, Object> 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已關閉");
測驗結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/423573.html
標籤:其他
上一篇:FEG創始人ROX:SmartDefi將會是整個去中心化金融市場的標桿
下一篇:今年跳槽可以再等等
