use of org.elasticsearch.client.Client in project fess by codelibs.
the class EsDataStoreImpl method storeData.
@Override
protected void storeData(final DataConfig dataConfig, final IndexUpdateCallback callback, final Map<String, String> paramMap, final Map<String, String> scriptMap, final Map<String, Object> defaultDataMap) {
final String hostsStr = paramMap.get(HOSTS);
if (StringUtil.isBlank(hostsStr)) {
logger.info("hosts is empty.");
return;
}
final long readInterval = getReadInterval(paramMap);
final Settings settings = Settings.builder().put(paramMap.entrySet().stream().filter(e -> e.getKey().startsWith(SETTINGS_PREFIX)).collect(Collectors.toMap(e -> e.getKey().replaceFirst("^settings\\.", StringUtil.EMPTY), e -> e.getValue()))).build();
logger.info("Connecting to " + hostsStr + " with [" + settings.toDelimitedString(',') + "]");
final InetSocketTransportAddress[] addresses = split(hostsStr, ",").get(stream -> stream.map(h -> {
final String[] values = h.trim().split(":");
try {
if (values.length == 1) {
return new InetSocketTransportAddress(InetAddress.getByName(values[0]), 9300);
} else if (values.length == 2) {
return new InetSocketTransportAddress(InetAddress.getByName(values[0]), Integer.parseInt(values[1]));
}
} catch (final Exception e) {
logger.warn("Failed to parse address: " + h, e);
}
return null;
}).filter(v -> v != null).toArray(n -> new InetSocketTransportAddress[n]));
try (PreBuiltTransportClient client = new PreBuiltTransportClient(settings)) {
client.addTransportAddresses(addresses);
processData(dataConfig, callback, paramMap, scriptMap, defaultDataMap, readInterval, client);
}
}
use of org.elasticsearch.client.Client in project nutch by apache.
the class TestElasticIndexWriter method setup.
@Before
public void setup() {
conf = NutchConfiguration.create();
conf.addResource("nutch-site-test.xml");
bulkRequestSuccessful = false;
clusterSaturated = false;
curNumFailures = 0;
maxNumFailures = 0;
Settings settings = Settings.builder().build();
ThreadPool threadPool = new ThreadPool(settings);
// customize the ES client to simulate responses from an ES cluster
client = new AbstractClient(settings, threadPool) {
@Override
public void close() {
}
@Override
protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
BulkResponse response = null;
if (clusterSaturated) {
// pretend the cluster is saturated
curNumFailures++;
if (curNumFailures >= maxNumFailures) {
// pretend the cluster is suddenly no longer saturated
clusterSaturated = false;
}
// respond with a failure
BulkItemResponse failed = new BulkItemResponse(0, OpType.INDEX, new BulkItemResponse.Failure("nutch", "index", "failure0", new EsRejectedExecutionException("saturated")));
response = new BulkResponse(new BulkItemResponse[] { failed }, 0);
} else {
// respond successfully
BulkItemResponse success = new BulkItemResponse(0, OpType.INDEX, new IndexResponse(new ShardId("nutch", UUID.randomUUID().toString(), 0), "index", "index0", 0, true));
response = new BulkResponse(new BulkItemResponse[] { success }, 0);
}
listener.onResponse((Response) response);
}
};
// customize the plugin to signal successful bulk operations
testIndexWriter = new ElasticIndexWriter() {
@Override
protected Client makeClient(Configuration conf) {
return client;
}
@Override
protected BulkProcessor.Listener bulkProcessorListener() {
return new BulkProcessor.Listener() {
@Override
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
if (!response.hasFailures()) {
bulkRequestSuccessful = true;
}
}
@Override
public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
}
@Override
public void beforeBulk(long executionId, BulkRequest request) {
}
};
}
};
}
use of org.elasticsearch.client.Client in project elasticsearch-skywalker by jprante.
the class AbstractNodeTest method closeAllNodes.
public void closeAllNodes() {
for (Client client : clients.values()) {
client.close();
}
clients.clear();
for (Node node : nodes.values()) {
node.close();
}
nodes.clear();
}
use of org.elasticsearch.client.Client in project elasticsearch-skywalker by jprante.
the class AbstractNodeTest method closeNode.
public void closeNode(String id) {
Client client = clients.remove(id);
if (client != null) {
client.close();
}
Node node = nodes.remove(id);
if (node != null) {
node.close();
}
}
use of org.elasticsearch.client.Client in project elasticsearch-skywalker by jprante.
the class AbstractNodeTest method buildNode.
public Node buildNode(String id, Settings settings) {
String settingsSource = getClass().getName().replace('.', '/') + ".yml";
Settings finalSettings = settingsBuilder().loadFromClasspath(settingsSource).put(defaultSettings).put(settings).put("name", id).build();
if (finalSettings.get("gateway.type") == null) {
// default to non gateway
finalSettings = settingsBuilder().put(finalSettings).put("gateway.type", "none").build();
}
if (finalSettings.get("cluster.routing.schedule") != null) {
// decrease the routing schedule so new nodes will be added quickly
finalSettings = settingsBuilder().put(finalSettings).put("cluster.routing.schedule", "50ms").build();
}
Node node = nodeBuilder().settings(finalSettings).build();
Client client = node.client();
nodes.put(id, node);
clients.put(id, client);
return node;
}
Aggregations