Elasticsearch 索引Index API(7.5.0)

1. Java代碼層配置模板,執行成功將打印自定義的放置模板成功幾個字,可前往官網地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-put-template.htmlhtml

package com.ruhuanxingyun.index;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import java.io.IOException;
import java.util.Arrays;

/**
* @description: 配置索引模板
* @author: rup
* @date: Create in 2019/8/29 10:09
* @company: ruhuanxingyun
*/
public class NginxIndexTemplate {

public static void main(String[] args) {
// 模板名
PutIndexTemplateRequest request = new PutIndexTemplateRequest("t-nginx");
// 模板匹配模式,即匹配採用當前模板的索引名格式
request.patterns(Arrays.asList("nginx*"));

// 索引配置,number_of_shards索引分片、number_of_replicas副本,默認爲1,若是是本地使用單節點,則改成0
request.settings(Settings.builder().put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0));

RestHighLevelClient client = null;
try {
// 映射配置
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
xContentBuilder.startObject()
.startObject("properties")
  // @timestamp時間索引,專門提供給kibana用的字段,日期類型,格式
.startObject("@timestamp").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.startObject("mac").field("type", "keyword")
.endObject()
.startObject("sn").field("type", "keyword")
.endObject()
.startObject("productType").field("type", "keyword")
.endObject()
.startObject("status").field("type", "keyword")
.endObject()
.startObject("updateTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.endObject()
.endObject();
request.mapping(xContentBuilder);
// 別名
request.alias(new Alias("nginx"));
// 模板的排序
//request.order(1);
// 版本號
//request.version(2);

       // 客戶端初始化
client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
AcknowledgedResponse acknowledgedResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);

if (acknowledgedResponse.isAcknowledged()) {
System.out.println(String.format("放置模板名爲%s成功!", request.name()));
} else {
System.out.println(String.format("放置模板名爲%s失敗!", request.name()));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 關閉資源
if (client != null) {
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

 2. 可經過elasticsearch-head/postman等插件發送put請求 http://localhost:9200/_template/t-nginx,放置成功以下圖,可前往官網地址:https://www.elastic.co/guide/en/elasticsearch/reference/7.3/indices-templates.htmljava

{ "index_patterns": ["nginx*"], "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "properties" : { "@timestamp": { "format": "yyyy-MM-dd HH:mm:ss", "type": "date" }, "mac": { "type": "keyword" }, "sn": { "type": "keyword" }, "productType": { "type": "keyword" }, "updatetime": { "format": "yyyy-MM-dd HH:mm:ss", "type": "date" }, "status": { "type": "keyword" } } }, "aliases": { "nginx": {} } }

3. 索引模板相關的兩種方式nginx

  A. 獲取模板:GET請求 http://localhost:9200/_template/t-nginx ,其中t-nginx是模板名   或者   Java代碼層獲取https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-get-templates.html
apache

  B. 刪除模板:DELETE請求 http://localhost:9200/_template/t-nginx   或者  Java代碼層獲取https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-delete-template.html
json

   C. 放置模板:見二、3介紹服務器

4. 獲取索引數據結構

  A. elasticsearch-head/postman/kibanaapp

    GET /<index> elasticsearch

    路徑參數:index是索引名稱,多個以逗號分隔,或者通配符表達式,可以使用_all來獲取集羣全部的索引;ide

      查詢參數:expand_wildcards是控制通配符表達式能夠擴展到的索引類型,默認值爲open,all-展現開放和關閉的索引,open-僅展現開放的索引,closed-僅展現關閉的索引,none-不接受通配符表達式;

  B. kibana操做

  C. java編寫

  /** * 刪除超過預存天數的日誌 * * @param preserveDay 預留天數 */
    private void deleteLogByTime(Integer preserveDay) { String[] indexPrefixs = this.indexPrefix.split(StrUtil.COMMA); GetIndexRequest getIndexRequest; DeleteIndexRequest deleteIndexRequest; List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); try { for (String indexPrefix : indexPrefixs) { getIndexRequest = new GetIndexRequest(String.format("%s*", indexPrefix)); GetIndexResponse response = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT); // 索引數據第一條就是本索引最早的存儲的數據
                String[] indexs = response.getIndices(); int diff = indexs.length - preserveDay; if (diff > 0) { list.clear(); for (int i = 0; i < diff; i++) { list.add(indexs[i]); set.add(indexs[i].replace(indexPrefix, "")); } deleteIndexRequest = new DeleteIndexRequest(list.toArray(new String[0])); restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); log.info(String.format("刪除索引名稱爲%s日誌", list.toString())); } } if (CollUtil.isNotEmpty(set)) { this.deleteDiskSpaceByLogTime(set); } } catch (IOException e) { e.printStackTrace(); log.info("刪除超過預存天數的日誌失敗"); } }

5. 建立索引

  A. elasticsearch-head/postman/kibana

    PUT /<index>

    路徑參數:index索引名稱僅小寫;

    請求正文:aliases是索引別名,mappings是索引中字段,映射對象,settings是索引的配置項

  B. kibana操做

  C.  java編寫

6. 刪除索引

  A. elasticsearch-head/postman/kibana

    DELETE /<index>

    路徑參數:不能使用別名刪除索引,要禁用_all或通配符表達式刪除索引,請將elasticsearch.yml文件中action.destructive_requires_name羣集設置true

  B. kibana操做

  C. java編寫:見獲取索引裏的代碼

7. 關閉索引

  A. elasticsearch-head/postman

    POST /<index>/_close 關閉索引禁止進行讀寫操做,而且不容許開放索引容許的全部操做,沒法對文檔創建索引或者在關閉索引中進行搜索文檔。這使關閉索引沒必要維護用於文檔創建索引或搜索文檔的內部數據結構,從而減小了集羣上的開銷,但關閉索引會佔用大量的磁盤空間。

  B. java代碼層  

CloseIndexRequest request = new CloseIndexRequest("index"); AcknowledgedResponse closeIndexResponse = client.indices().close(request, RequestOptions.DEFAULT); boolean acknowledged = closeIndexResponse.isAcknowledged();

8. 開放索引

  A. elasticsearch-head/postman

    POST /<index>/_open 從新打開封閉索引,當打開或關閉索引時,主服務器負責從新啓動索引分片以反映索引的新狀態,而後索引將經歷正常的恢復過程

  B. java代碼層

OpenIndexRequest request = new OpenIndexRequest("index"); OpenIndexResponse openIndexResponse = client.indices().open(request, RequestOptions.DEFAULT); boolean acknowledged = openIndexResponse.isAcknowledged(); boolean shardsAcked = openIndexResponse.isShardsAcknowledged();
相關文章
相關標籤/搜索