/
EsBaseUtil.java
424 lines (374 loc) · 14.2 KB
/
EsBaseUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package com.github.bjlhx15.servicees;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Response;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.ClosedWatchServiceException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
/**
* @author lihongxu
* @since 2018/11/26 上午10:57
*/
@Deprecated
public class EsBaseUtil {
private Client client;
public Client getClient() {
return client;
}
//region 客户端
/**
* 客户端
*/
public Client createClient() throws UnknownHostException {
Settings settings = Settings.builder()
.put("cluster.name", "jiesi-5.4")
.build();
// client = new PreBuiltTransportClient(settings)//Settings.EMPTY
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.182.11"), 20101))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.182.12"), 20101))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.182.13"), 20101));
client = new PreBuiltTransportClient(settings)//Settings.EMPTY
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client;
}
//endregion
//region 索引映射操作
/**
* 1、创建索引【创建数据库】 单纯创建 没有指明映射以及分片信息
*
* @param indexName 索引名称
*/
public void esCreateIndex(String indexName) {
if (!existIndex(indexName)) {
client.admin().indices().prepareCreate(indexName).get();
}
}
/**
* 2、创建索引【创建数据库】 指明分片以及副本
*
* @param indexName 索引名
* @param shards 分分片数
* @param replicas 副本数
*/
public void esCreateIndex(String indexName, int shards, int replicas) {
if (!existIndex(indexName)) {
esCreateIndex(indexName, Settings.builder()
.put("index.number_of_shards", shards)
.put("index.number_of_replicas", replicas)
);
}
}
/**
* 3、创建索引【创建数据库】 指明分片以及副本
*
* @param indexName 索引名称
* @param builder 基本配置
*/
public void esCreateIndex(String indexName, Settings.Builder builder) {
if (!existIndex(indexName)) {
client.admin().indices().prepareCreate(indexName)
.setSettings(builder)
.get();
}
}
/**
* 4、创建索引【创建数据库,表以及字段】 同时 具有mapping
*
* @param indexName 索引名
* @param type type名 表名
* @param mapping 映射文件 {"typeName":{"properties":{"fieldName":{"type":"string"}}}}
*/
public void esCreateIndexWithMapping(String indexName, String type, String mapping) {
if (!existIndex(indexName)) {
client.admin().indices().prepareCreate(indexName)
.addMapping(type, mapping, XContentType.JSON)
.get();
}
}
/**
* 5、在 索引已经创建的 创建或更新 type 以及mapping【在已有数据库,创建表以及字段】
*
* @param indexName
* @param type
* @param mapping {"properties":{"name":{"type ":"string "}}} 或者 {"typeName":{"properties":{"name":{"type ":"string "}}}}
*/
public void esCreateOrUpdateTypeWithMapping(String indexName, String type, String mapping) {
if (!existIndex(indexName)) {
client.admin().indices().preparePutMapping(indexName)
.setType(type)
.setSource(mapping, XContentType.JSON)
.get();
}
}
/**
* 获取映射
*/
public String getMapping(String indexName, String typeName) {
MappingMetaData mappingMetaData = client.admin().cluster().prepareState().execute()
.actionGet().getState().getMetaData().getIndices()
.get(indexName).getMappings()
.get(typeName);
String toString = mappingMetaData.source().toString();
return toString;
}
/**
* 刷新索引
*
* @param indices 空 刷新所有;指定具体刷新
* es默认的refresh间隔时间是1s 近乎实时搜索
*/
public void esRefreshIndices(String[] indices) {
if (indices == null || indices.length == 0) {
client.admin().indices().prepareRefresh().get();
} else {
client.admin().indices()
.prepareRefresh(indices)
.get();
}
}
/**
* 删除索引
*/
public void deleteIndex(String indexName) {
client.admin().indices().prepareDelete(indexName).get();
}
/**
* 判断索引是否存在
*/
public boolean existIndex(String indexName) {
IndicesExistsRequest request = new IndicesExistsRequest(indexName);
IndicesExistsResponse response = client.admin().indices().exists(request).actionGet();
return response.isExists();
}
//endregion
// region 文档操作
/**
* 添加数据
*/
public void addDocument(String indexName, String type, String id, String json) {
esCreateIndex(indexName);
client.prepareIndex(indexName, type, id)
.setSource(json, XContentType.JSON)
.get();
}
/**
* 添加数据
*/
public void addDocument(String indexName, String type, String json) {
esCreateIndex(indexName);
client.prepareIndex(indexName, type, UUID.randomUUID().toString())
.setSource(json, XContentType.JSON)
.get();
}
/**
* 添加数据
*/
public void addDocument(String indexName, String type, List<String> jsonArray) {
esCreateIndex(indexName);
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (String s : jsonArray) {
bulkRequest.add(client.prepareIndex(indexName, type, UUID.randomUUID().toString())
.setSource(s, XContentType.JSON
)
);
}
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.isFragment()) {
for (BulkItemResponse bulkItemResponse : bulkResponse) {
System.out.println(bulkItemResponse.getId());
}
}
}
/**
* 添加数据
*/
public void addDocument(String indexName, String type, Map<String, String> jsonArrayMap) {
esCreateIndex(indexName);
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (Map.Entry<String, String> entry : jsonArrayMap.entrySet()) {
bulkRequest.add(client.prepareIndex(indexName, type, entry.getKey())
.setSource(entry.getValue(), XContentType.JSON
)
);
}
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.isFragment()) {
for (BulkItemResponse bulkItemResponse : bulkResponse) {
System.out.println(bulkItemResponse.getId());
}
}
}
/**
* 通过ID删除文档
*
* @param indexName
* @param type
* @param id
* @return
*/
public ActionResponse deleteDocumentById(String indexName, String type, String id) {
return client.prepareDelete(indexName, type, id).get();
}
/**
* 通过ID获取文档
*
* @param indexName
* @param type
* @param id
* @return
*/
public ActionResponse getDocumentById(String indexName, String type, String id) {
return client.prepareGet(indexName, type, id).get();
}
/**
* 通过ID更新文档
*
* @param indexName
* @param type
* @param id
* @return
*/
public ActionResponse updateDocumentById(String indexName, String type, String id, String updateJsonString) throws ExecutionException, InterruptedException {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(indexName);
updateRequest.type(type);
updateRequest.id(id);
updateRequest.doc(updateJsonString, XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest).get();
return updateResponse;
}
// endregion
//region 查询
/**
* 执行查询
*
* @param index
* @param type
* @param qb
*/
public void excuteQuery(String index, String type, QueryBuilder qb) {
SearchResponse sr = client.prepareSearch(index)
.setTypes(type)
.setQuery(qb).setSize(20)
.execute().actionGet();
SearchHits searchHits = sr.getHits();
for (SearchHit searchHit : searchHits) {
System.out.println("数据:" + searchHit.getSourceAsString());
}
}
/**
* 执行查询——_—
*
* @param qb
*/
public void excuteQuery(QueryBuilder qb) {
excuteQuery("bt_middle_data_test", "form", qb);
}
//endregion
//region 索引配置信息
//设置索引 配置信息 public void esCreateIndex(String indexName, Settings.Builder builder)
/**
* 获取索引 配置信息
*
* @param indices 索索引名
* @return
*/
public ActionResponse getSettingResponse(String[] indices) {
GetSettingsResponse response = client.admin().indices()
.prepareGetSettings(indices).get();
return response;
// for (ObjectObjectCursor<String, Settings> cursor : response.getIndexToSettings()) {
// String index = cursor.key;
// Settings settings = cursor.value;
// Integer shards = settings.getAsInt("index.number_of_shards", null);
// Integer replicas = settings.getAsInt("index.number_of_replicas", null);
// }
}
/**
* 更新索引 配置信息
*
* @param indexName 索引名
* @param builder 更新设置 Settings.builder().put("index.number_of_replicas", 0)
*
* @return
*/
public void updateIndicesSetting(String indexName, Settings.Builder builder) {
client.admin().indices().prepareUpdateSettings(indexName)
.setSettings(builder)
.get();
}
//endregion
//region 集群健康
public ActionResponse getClusterHealthResponse(){
ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get();
String clusterName = healths.getClusterName();
int numberOfDataNodes = healths.getNumberOfDataNodes();
int numberOfNodes = healths.getNumberOfNodes();
System.out.println("集群名称:"+clusterName);
System.out.println("集群节点数:"+numberOfNodes);
System.out.println("集群数据节点数:"+numberOfDataNodes);
for (ClusterIndexHealth health : healths.getIndices().values()) {
String index = health.getIndex();
int numberOfShards = health.getNumberOfShards();
int numberOfReplicas = health.getNumberOfReplicas();
ClusterHealthStatus status = health.getStatus();
System.out.println("集群索引名:"+index+";分片数:"+numberOfShards
+";副本数:"+numberOfReplicas+";状态:"+status);
}
return healths;
}
public ActionResponse getClusterHealthGreenStatus(String[] indices){
ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth(indices)
.setWaitForGreenStatus()
.get();
for (ClusterIndexHealth health : healthResponse.getIndices().values()) {
String index = health.getIndex();
int numberOfShards = health.getNumberOfShards();
int numberOfReplicas = health.getNumberOfReplicas();
ClusterHealthStatus status = health.getStatus();
System.out.println("集群索引名:"+index+";分片数:"+numberOfShards
+";副本数:"+numberOfReplicas+";状态:"+status);
}
return healthResponse;
}
public ActionResponse getClusterHealthGreenStatus(String index){
ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth(index)
.setWaitForGreenStatus()
.get();
ClusterHealthStatus status = healthResponse.getIndices().get(index).getStatus();
if (!status.equals(ClusterHealthStatus.GREEN)) {
throw new RuntimeException("Index is in " + status + " state");
}
return healthResponse;
}
//endregion
}