个人学习elasticSearch 5 的过程分享,至于实现代码后期有空再整理,很多东西可以从文档中找到的,拿过来用即可。

学习elasticSearch官方文档

elasticSearch权威指南 中文版

elasticSearch java客户端

elasticSearch英文文档

说明:
我用的是ES 5.0.0版本,以上文档除了权威指南,其它是ES 5.0.0版本,但是进去后可以选择切换成其它版本。elasticSearch权威指南(中文版)是1.4.X版本的,读着了解基本流程、操作很有用,有什么疑惑就查看对应的英文版。还有1.4.X、2.X和5.X的连接方式都不同,查看对应的版本文档,并最好使用对应的jar包。

特别声明

如果ELK加了search guard,那连接ES时步骤将不一样,需要验证ssl。

es java client简单实现

用search guard 连接 es 的简单的es java client实现代码分享。node-1-keystore.jks,truststore.jks这些文件从部署elk的管理员那儿索要。

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

import java.net.InetAddress;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import com.floragunn.searchguard.ssl.SearchGuardSSLPlugin;
import com.floragunn.searchguard.ssl.util.SSLConfigConstants;


public class ESClientHelper {

static Logger LOG = Logger.getLogger(ESClientHelper.class);

static final String INDEX = "index-*";
static final int SIZE = 1000;

private Settings.Builder settingsBuilder;
private Settings settings;
private TransportClient client;

@SuppressWarnings({ "resource", "unchecked" })
public ESClientHelper() {

try {
settingsBuilder = Settings.builder().put("path.home", ".").put("cluster.name", "elasticSearch")
.put("client.transport.sniff", true)
.put(SSLConfigConstants.SEARCHGUARD_SSL_HTTP_ENABLE_OPENSSL_IF_AVAILABLE, true)
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_ENABLE_OPENSSL_IF_AVAILABLE, true)
// .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_ENABLE_OPENSSL_IF_AVAILABLE,true)
// .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_ALIAS, "node-1")
.put("searchguard.ssl.transport.enabled", true)
// .put("searchguard.ssl.transport.keystore_filepath",
// "/data/hexl/logstash/deleteIndex/conf/node-1-keystore.jks")
// .put("searchguard.ssl.transport.truststore_filepath",
// "/data/hexl/logstash/deleteIndex/conf/truststore.jks")
.put("searchguard.ssl.transport.keystore_filepath", "D:/code/node-1-keystore.jks")
//.put("searchguard.ssl.transport.keystore_filepath", "D:/code/node-2-keystore.jks")
.put("searchguard.ssl.transport.truststore_filepath", "D:/code/truststore.jks")
.put("searchguard.ssl.transport.keystore_password", "password")
.put("searchguard.ssl.transport.truststore_password", "password")
.put("searchguard.ssl.transport.enforce_hostname_verification", false);

settings = settingsBuilder.build();

client = new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
//这里默认客户端端口是9300,网页默认端口是9200
} catch (Exception e) {
LOG.error("create ESClientHelper failed!");
}
}

/**
* 获取数据,分页操作
*
* @param indexStr
* @param fromN
* @param sizeN
*/
public ArrayList<String> getResponse(int fromN, int sizeN) {
try {

FieldSortBuilder sortBuilders = SortBuilders.fieldSort("@timestamp").order(SortOrder.ASC);

SearchResponse response = client.prepareSearch(INDEX).addSort(sortBuilders).setFrom(fromN).setSize(sizeN)
.execute().actionGet();
System.out.println(response.getHits().getHits().length);

return getContentList(response);

} catch (Exception e) {
LOG.error("getResponse failed, the parameter: " + "," + fromN + "," + sizeN);
}
return null;
}

/**
*获取某个字段,我的是content字段
*
* @param response
* @return
*/
private ArrayList<String> getContentList(SearchResponse response) {

ArrayList<String> list = new ArrayList<String>();

try {
if (response == null)
return null;
for (int idx = 0, len = response.getHits().getHits().length; idx < len; idx++) {

list.add(response.getHits().getAt(idx).getSource().get("content").toString());
}
} catch (Exception e) {
LOG.error("response transport to list failed!");
}

return list;
}

public void closeClient() {
try {
client.close();
} catch (Exception e) {
LOG.error("close ES java client failed!");
}
}

//test main
public static void main(String[] args) {
System.out.println(new ESClientHelper().getResponse(0, 1000).toString());
}
}

完结。