本文將繼續介紹elasticsearch索引監控之Indices segments與Indices Shard stores api。node
提供Lucene索引(分片級別)使用的segments(段信息)。api
其對應的示例代碼以下:微信
1public static final void test_Indices_segments() { 2 TransportClient client = EsClient.getTransportClient(); 3 try { 4 IndicesSegmentsRequest request = new IndicesSegmentsRequest(); 5 request.indices("logs_write"); 6 ActionFuture<IndicesSegmentResponse> responseFuture = client.admin().indices().segments(request); 7 IndicesSegmentResponse response = responseFuture.get(); 8 System.out.println(response); 9 } catch (Throwable e) { 10 e.printStackTrace(); 11 } finally { 12 EsClient.close(client); 13 } 14}
返回結果相似:運維
1{ 2 "_shards": ... 3 "indices": { 4 "test": { 5 "shards": { 6 "0": [ 7 { 8 "routing": { 9 "state": "STARTED", 10 "primary": true, 11 "node": "zDC_RorJQCao9xf9pg3Fvw" 12 }, 13 "num_committed_segments": 0, 14 "num_search_segments": 1, 15 "segments": { 16 "_0": { 17 "generation": 0, 18 "num_docs": 1, 19 "deleted_docs": 0, 20 "size_in_bytes": 3800, 21 "memory_in_bytes": 1410, 22 "committed": false, 23 "search": true, 24 "version": "7.0.0", 25 "compound": true, 26 "attributes": { 27 } 28 } 29 } 30 } 31 ] 32 } 33 } 34 } 35}
返回結果字段說明以下:elasticsearch
另外Indices Segments支持verbose默認,將輸出一些調試信息,其返回結果以下:ide
1{ 2 "_0": { 3 4 "ram_tree": [ 5 { 6 "description": "postings [PerFieldPostings(format=1)]", 7 "size_in_bytes": 2696, 8 "children": [ 9 { 10 "description": "format 'Lucene50_0' ...", 11 "size_in_bytes": 2608, 12 "children" :[ ... ] 13 }, 14 ] 15 }, 16 ] 17 } 18}
主要展現索引分片副本的存儲信息。默認狀況下,列表只存儲至少有一個未分配副本的分片的信息。當集羣健康狀態爲黃色時,將列出至少有一個未分配副本的分片的存儲信息。當集羣健康狀態爲紅色時,這將列出具備未分配初選的碎片的存儲信息。post
對應的JAVA示例以下:調試
1public static final void test_Indices_Shard_Stores() { 2 TransportClient client = EsClient.getTransportClient(); 3 try { 4 IndicesShardStoresRequest request = new IndicesShardStoresRequest(); 5 request.indices("logs_write"); 6 ActionFuture<IndicesShardStoresResponse> responseFuture = client.admin().indices().shardStores(request); 7 IndicesShardStoresResponse response = responseFuture.get(); 8 ImmutableOpenMap<String, ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>>> data = response.getStoreStatuses(); 9 List indexList = new ArrayList(); 10 for (Iterator it = data.keysIt(); it.hasNext(); ) { 11 String key = (String)it.next(); 12 Map indexData = new HashMap(); 13 indexList.add(indexData); 14 List indexShardList = new ArrayList(); 15 indexData.put(key, indexShardList); 16 ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>> value = data.get(key); 17 for(Iterator it2 = value.keysIt(); it2.hasNext(); ) { 18 Integer key2 = (Integer)it2.next(); 19 Map shardData = new HashMap(); 20 indexShardList.add(shardData); 21 List shardStoreStatusList = new ArrayList(); 22 shardData.put(key2 + "", shardStoreStatusList); 23 List<IndicesShardStoresResponse.StoreStatus> storeStatusList = value.get(key2); 24 for(IndicesShardStoresResponse.StoreStatus storeStatus : storeStatusList) { 25 Map storeStatusMap = new HashMap(); 26 shardStoreStatusList.add(storeStatusMap); 27 storeStatusMap.put("allocationId", storeStatus.getAllocationId()); 28 storeStatusMap.put("allocationStatus", storeStatus.getAllocationStatus().value()); 29 Map discoveryNodeData = new HashMap(); 30 storeStatusMap.put("discoveryNode", discoveryNodeData); 31 DiscoveryNode node = storeStatus.getNode(); 32 discoveryNodeData.put("name", node.getName()); 33 discoveryNodeData.put("name", node.getAddress()); 34 discoveryNodeData.put("attributes", node.getAttributes()); 35 discoveryNodeData.put("ephemeralId", node.getEphemeralId()); 36 discoveryNodeData.put("hostAddress", node.getHostAddress()); 37 discoveryNodeData.put("hostName", node.getHostName()); 38 discoveryNodeData.put("id", node.getId()); 39 discoveryNodeData.put("roles", node.getRoles()); 40 } 41 } 42 } 43 System.out.println(FastJsonUtils.getBeanToJson(indexList)); 44 } catch (Throwable e) { 45 e.printStackTrace(); 46 } finally { 47 EsClient.close(client); 48 } 49}
返回的結果爲:code
1[ 2 { 3 "logs-000002":[ 4 { 5 0:[ // @1 6 { 7 "discoveryNode":{ // @2 8 "hostName":"127.0.0.1", 9 "roles":[ 10 "MASTER", 11 "DATA", 12 "INGEST" 13 ], 14 "name":{ 15 "address":"127.0.0.1", 16 "fragment":true, 17 "port":9300 18 }, 19 "attributes":{ 20 "ml.machine_memory":"16964890624", 21 "ml.max_open_jobs":"20", 22 "xpack.installed":"true", 23 "ml.enabled":"true" 24 }, 25 "hostAddress":"127.0.0.1", 26 "id":"ekEDWaVVRH-944BgEsfRLA", 27 "ephemeralId":"ox0CP9hhQOu1klZgNv7Ezw" 28 }, 29 "allocationId":"KRw3BYPFTrK39HOYXzwXBA", // @3 30 "allocationStatus":"primary" // @4 31 } 32 ] 33 } 34 35 //因爲當前試驗環境爲單機模式,故省略其餘分片信息 36 37 ] 38 } 39]
代碼@1:分片編號。orm
代碼@2:分片所在的節點的信息,包含名稱、角色、id、地址等信息。
代碼@3:副本的分配ID。
代碼@4:分配的狀態,其值爲primary、replica、unused。
索引監控相關API就介紹到這裏了。
更多文章請關注微信公衆號:
一波廣告來襲,做者新書《RocketMQ技術內幕》已出版上市:
《RocketMQ技術內幕》已出版上市,目前可在主流購物平臺(京東、天貓等)購買,本書從源碼角度深度分析了RocketMQ NameServer、消息發送、消息存儲、消息消費、消息過濾、主從同步HA、事務消息;在實戰篇重點介紹了RocketMQ運維管理界面與當前支持的39個運維命令;並在附錄部分羅列了RocketMQ幾乎全部的配置參數。本書獲得了RocketMQ創始人、阿里巴巴Messaging開源技術負責人、Linux OpenMessaging 主席的高度承認並做序推薦。目前是國內第一本成體系剖析RocketMQ的書籍。