-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ElasticIndexWriter.java
276 lines (242 loc) · 9.11 KB
/
ElasticIndexWriter.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nutch.indexwriter.elastic;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapred.JobConf;
import org.apache.nutch.indexer.IndexWriter;
import org.apache.nutch.indexer.NutchDocument;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*/
public class ElasticIndexWriter implements IndexWriter {
public static Logger LOG = LoggerFactory.getLogger(ElasticIndexWriter.class);
private static final int DEFAULT_MAX_BULK_DOCS = 250;
private static final int DEFAULT_MAX_BULK_LENGTH = 2500500;
private Client client;
private Node node;
private String defaultIndex;
private Configuration config;
private BulkRequestBuilder bulk;
private ListenableActionFuture<BulkResponse> execute;
private int port = -1;
private String host = null;
private String clusterName = null;
private int maxBulkDocs;
private int maxBulkLength;
private long indexedDocs = 0;
private int bulkDocs = 0;
private int bulkLength = 0;
private boolean createNewBulk = false;
@Override
public void open(JobConf job, String name) throws IOException {
clusterName = job.get(ElasticConstants.CLUSTER);
host = job.get(ElasticConstants.HOST);
port = job.getInt(ElasticConstants.PORT, 9300);
Builder settingsBuilder = ImmutableSettings.settingsBuilder().classLoader(
Settings.class.getClassLoader());
BufferedReader reader = new BufferedReader(
job.getConfResourceAsReader("elasticsearch.conf"));
String line;
String parts[];
while ((line = reader.readLine()) != null) {
if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
line.trim();
parts = line.split("=");
if (parts.length == 2) {
settingsBuilder.put(parts[0].trim(), parts[1].trim());
}
}
}
if (StringUtils.isNotBlank(clusterName))
settingsBuilder.put("cluster.name", clusterName);
// Set the cluster name and build the settings
Settings settings = settingsBuilder.build();
// Prefer TransportClient
if (host != null && port > 1) {
client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(host, port));
} else if (clusterName != null) {
node = nodeBuilder().settings(settings).client(true).node();
client = node.client();
}
bulk = client.prepareBulk();
defaultIndex = job.get(ElasticConstants.INDEX, "nutch");
maxBulkDocs = job.getInt(ElasticConstants.MAX_BULK_DOCS,
DEFAULT_MAX_BULK_DOCS);
maxBulkLength = job.getInt(ElasticConstants.MAX_BULK_LENGTH,
DEFAULT_MAX_BULK_LENGTH);
}
@Override
public void write(NutchDocument doc) throws IOException {
String id = (String) doc.getFieldValue("id");
String type = doc.getDocumentMeta().get("type");
if (type == null)
type = "doc";
IndexRequestBuilder request = client.prepareIndex(defaultIndex, type, id);
Map<String, Object> source = new HashMap<String, Object>();
// Loop through all fields of this doc
for (String fieldName : doc.getFieldNames()) {
if (doc.getField(fieldName).getValues().size() > 1) {
source.put(fieldName, doc.getFieldValue(fieldName));
// Loop through the values to keep track of the size of this
// document
for (Object value : doc.getField(fieldName).getValues()) {
bulkLength += value.toString().length();
}
} else {
source.put(fieldName, doc.getFieldValue(fieldName));
bulkLength += doc.getFieldValue(fieldName).toString().length();
}
}
request.setSource(source);
// Add this indexing request to a bulk request
bulk.add(request);
indexedDocs++;
bulkDocs++;
if (bulkDocs >= maxBulkDocs || bulkLength >= maxBulkLength) {
LOG.info("Processing bulk request [docs = " + bulkDocs + ", length = "
+ bulkLength + ", total docs = " + indexedDocs
+ ", last doc in bulk = '" + id + "']");
// Flush the bulk of indexing requests
createNewBulk = true;
commit();
}
}
@Override
public void delete(String key) throws IOException {
try {
DeleteRequestBuilder builder = client.prepareDelete();
builder.setIndex(defaultIndex);
builder.setType("doc");
builder.setId(key);
builder.execute().actionGet();
} catch (ElasticsearchException e) {
throw makeIOException(e);
}
}
public static IOException makeIOException(ElasticsearchException e) {
final IOException ioe = new IOException();
ioe.initCause(e);
return ioe;
}
@Override
public void update(NutchDocument doc) throws IOException {
write(doc);
}
@Override
public void commit() throws IOException {
if (execute != null) {
// wait for previous to finish
long beforeWait = System.currentTimeMillis();
BulkResponse actionGet = execute.actionGet();
if (actionGet.hasFailures()) {
for (BulkItemResponse item : actionGet) {
if (item.isFailed()) {
throw new RuntimeException("First failure in bulk: "
+ item.getFailureMessage());
}
}
}
long msWaited = System.currentTimeMillis() - beforeWait;
LOG.info("Previous took in ms " + actionGet.getTookInMillis()
+ ", including wait " + msWaited);
execute = null;
}
if (bulk != null) {
if (bulkDocs > 0) {
// start a flush, note that this is an asynchronous call
execute = bulk.execute();
}
bulk = null;
}
if (createNewBulk) {
// Prepare a new bulk request
bulk = client.prepareBulk();
bulkDocs = 0;
bulkLength = 0;
}
}
@Override
public void close() throws IOException {
// Flush pending requests
LOG.info("Processing remaining requests [docs = " + bulkDocs
+ ", length = " + bulkLength + ", total docs = " + indexedDocs + "]");
createNewBulk = false;
commit();
// flush one more time to finalize the last bulk
LOG.info("Processing to finalize last execute");
createNewBulk = false;
commit();
// Close
client.close();
if (node != null) {
node.close();
}
}
@Override
public String describe() {
StringBuffer sb = new StringBuffer("ElasticIndexWriter\n");
sb.append("\t").append(ElasticConstants.CLUSTER)
.append(" : elastic prefix cluster\n");
sb.append("\t").append(ElasticConstants.HOST).append(" : hostname\n");
sb.append("\t").append(ElasticConstants.PORT).append(" : port\n");
sb.append("\t").append(ElasticConstants.INDEX)
.append(" : elastic index command \n");
sb.append("\t").append(ElasticConstants.MAX_BULK_DOCS)
.append(" : elastic bulk index doc counts. (default 250) \n");
sb.append("\t").append(ElasticConstants.MAX_BULK_LENGTH)
.append(" : elastic bulk index length. (default 2500500 ~2.5MB)\n");
return sb.toString();
}
@Override
public void setConf(Configuration conf) {
config = conf;
String cluster = conf.get(ElasticConstants.CLUSTER);
String host = conf.get(ElasticConstants.HOST);
if (StringUtils.isBlank(cluster) && StringUtils.isBlank(host)) {
String message = "Missing elastic.cluster and elastic.host. At least one of them should be set in nutch-site.xml ";
message += "\n" + describe();
LOG.error(message);
throw new RuntimeException(message);
}
}
@Override
public Configuration getConf() {
return config;
}
}