1. 程式人生 > 其它 >【elasticsearch7.x】建立索引、獲取索引配置資訊

【elasticsearch7.x】建立索引、獲取索引配置資訊

技術標籤:elasticsearchelasticsearchjava大資料

package com.es.fixData.get;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.client.GetAliasesResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import com.alibaba.fastjson.JSONObject;
import com.es.util.ESClient;

public class CreateIndex {
	public static void main(String[] args) throws Exception {
		// 建立測試索引
		createTest();

		// 給指定索引新增欄位
		addField("test_wd");

		// 獲取索引配置資訊
		getIndexSettings();
	}

	private static void getIndexSettings() {

		RestHighLevelClient client = ESClient.getES7Client();

		try {
			GetAliasesRequest request = new GetAliasesRequest();
			GetAliasesResponse getAliasesResponse = client.indices().getAlias(request, RequestOptions.DEFAULT);
			Map<String, Set<AliasMetaData>> map = getAliasesResponse.getAliases();
			// 獲取es7全部索引集合
			Set<String> indices = map.keySet();
			// 遍歷索引
			for (String index : indices) {
//				System.out.println(index);
				// 獲取索引setting
				GetSettingsRequest getSettings = new GetSettingsRequest().indices(index);
				GetSettingsResponse getSettingsResponse = client.indices().getSettings(getSettings,
						RequestOptions.DEFAULT);
				String indexShards = getSettingsResponse.getSetting(index, "index.number_of_shards");// 索引分片
				String indexReplicas = getSettingsResponse.getSetting(index, "index.number_of_replicas");// 索引副本

				// 獲取索引mappings
				GetMappingsRequest getMappings = new GetMappingsRequest().indices(index);
				GetMappingsResponse getMappingResponse = client.indices().getMapping(getMappings,
						RequestOptions.DEFAULT);
				Map<String, MappingMetaData> allMappings = getMappingResponse.mappings();
				MappingMetaData indexMapping = allMappings.get(index);
				Map<String, Object> mapping = indexMapping.sourceAsMap();

				JSONObject json = new JSONObject(mapping);
				json.put("number_of_shards", indexShards);
				json.put("number_of_replicas", indexReplicas);

				JSONObject endJson = new JSONObject();
				endJson.put(index, json);
				System.out.println(endJson.toJSONString());
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void createTest() {
		String wd = "test_wd";// 索引名稱
		try {
			// 新增索引:帶routing
			createIndex(wd, 6, 1, true);// 建立索引

			List<Map<String, String>> zdList = new ArrayList<Map<String, String>>();
			zdList.add(createFieldMap("company", "text", null)); // 公司主體
			zdList.add(createFieldMap("companyType", "keyword", null)); // keyword 不分詞
			zdList.add(createFieldMap("updateTime", "long", null)); // 時間型別
			zdList.add(createFieldMap("description", "text", "false")); // 不索引該欄位(不能用於檢索)

			createType(wd, "_doc", zdList);// 新增mapping欄位
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 建立索引
	 * 
	 * @param index    索引
	 * @param shards   分片數量
	 * @param replicas 副本數量
	 * @param routing  是否新增路由
	 * @throws IOException
	 */

	private static void createIndex(String index, int shards, int replicas, Boolean routing) throws IOException {
		if (shards < 6) {
			shards = 6;
		}
		if (replicas < 1) {
			replicas = 1;
		}
		RestHighLevelClient client = ESClient.getES7Client();

		// number_of_shards:資料分片數,預設6
		// number_of_replicas:資料備份數,只有1臺機器設定為0
		Settings setting = Settings.builder().put("number_of_shards", shards).put("number_of_replicas", replicas)
				.build();
		CreateIndexRequest indexRequest = new CreateIndexRequest(index, setting);
		try {
			client.indices().create(indexRequest, RequestOptions.DEFAULT);
			if (routing) {
				setIndexRouting(index);
			}
		} catch (ResourceAlreadyExistsException e) {
			System.out.println("【Exception 此庫已存在!!! 】");
		}

	}

	/**
	 * 給索引設定路由
	 * 
	 * @param indexName 索引名稱
	 */
	public static void setIndexRouting(String indexName) {
		if (indexName == null)
			return;
		try {
			XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("_doc")
					.startObject("_routing").field("required", true).endObject().endObject().endObject();

			PutMappingRequest mapping = Requests.putMappingRequest(indexName).type("_doc").source(builder);
			ESClient.getES7Client().indices().putMapping(mapping, RequestOptions.DEFAULT);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 新增mapping欄位
	 * 
	 * @param indexName 索引名稱
	 * @param typeName  預設_doc
	 * @param zdList    組裝好的 欄位集合
	 * @throws IOException
	 */
	private static void createType(String indexName, String typeName, List<Map<String, String>> zdList)
			throws IOException {
		if (indexName == null || typeName == null)
			return;
		if (zdList == null || zdList.size() == 0)
			return;
		XContentBuilder builder = null;
		try {
			builder = XContentFactory.jsonBuilder().startObject().startObject(typeName).startObject("properties");
			for (Map<String, String> map : zdList) {
				String code = (String) map.get("fieldCode");
				String fieldType = (String) map.get("fieldType");
				String storeType = (String) map.get("storeType");
				if (fieldType.equals("date")) {
					fieldType = "long";
				}
				builder.startObject(code);
				builder.field("type", fieldType);

				if (storeType != null && storeType.length() > 0) {
					builder.field("index", storeType);
				}
				builder.endObject();
			}
			builder.endObject();// start幾個,end幾個
			builder.endObject();
			builder.endObject();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (builder == null)
			return;

		PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(typeName).source(builder);
		ESClient.getES7Client().indices().putMapping(mappingRequest, RequestOptions.DEFAULT);
//		ESClient.getES7Client().close();
	}

	/**
	 * 組裝mapping欄位
	 * 
	 * @param fieldCode 欄位名稱
	 * @param fieldType 欄位型別
	 * @param storeType 是否索引
	 * @return
	 */
	public static Map<String, String> createFieldMap(String fieldCode, String fieldType, String storeType) {
		Map<String, String> zdMap = new HashMap<String, String>();
		zdMap.put("fieldCode", fieldCode);// 欄位名稱
		zdMap.put("fieldType", fieldType);// 欄位型別
		if (storeType != null) {
			zdMap.put("storeType", storeType);
		}
		return zdMap;
	}

	/**
	 * 給指定索引新增單個欄位欄位
	 * 
	 * @param index 索引
	 * @throws Exception
	 */
	private static void addField(String index) throws Exception {
		List<Map<String, String>> zdList = new ArrayList<Map<String, String>>();
		zdList.add(createFieldMap("eventType", "text", null));

		createType(index, "_doc", zdList);
	}

}