use of org.elasticsearch.common.transport.InetSocketTransportAddress in project camel by apache.
the class ElasticsearchClusterBaseTest method cleanUpOnce.
@BeforeClass
public static void cleanUpOnce() throws Exception {
deleteDirectory("target/testcluster/");
clusterName = "es-cl-run-" + System.currentTimeMillis();
// create runner instance
runner = new ElasticsearchClusterRunner();
// create ES nodes
runner.onBuild(new ElasticsearchClusterRunner.Builder() {
@Override
public void build(final int number, final Builder settingsBuilder) {
settingsBuilder.put("http.cors.enabled", true);
settingsBuilder.put("http.cors.allow-origin", "*");
}
}).build(newConfigs().clusterName(clusterName).numOfNode(3).basePath("target/testcluster/").useLogger());
// wait for green status
runner.ensureGreen();
client = new PreBuiltTransportClient(getSettings()).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301));
}
use of org.elasticsearch.common.transport.InetSocketTransportAddress in project jena by apache.
the class BaseESTest method setupTransportClient.
/**
* Make sure that we have connectivity to the locally running ES node.
* The ES is started during the pre-integration-test phase
*/
@BeforeClass
public static void setupTransportClient() {
Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
transportClient = new PreBuiltTransportClient(settings);
try {
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ADDRESS), PORT));
} catch (UnknownHostException ex) {
Assert.fail("Failed to create transport client" + ex.getMessage());
}
classToTest = new TextIndexES(config(), transportClient, INDEX_NAME);
Assert.assertNotNull("Transport client was not created successfully", transportClient);
}
use of org.elasticsearch.common.transport.InetSocketTransportAddress in project API by ca-cwds.
the class DataAccessModule method elasticsearchClient.
// @Singleton
@Provides
public synchronized Client elasticsearchClient(ApiConfiguration apiConfiguration) {
if (client == null) {
ElasticsearchConfiguration config = apiConfiguration.getElasticsearchConfiguration();
try {
// Settings settings = Settings.settingsBuilder()
// .put("cluster.name", config.getElasticsearchCluster()).build();
// client = TransportClient.builder().settings(settings).build().addTransportAddress(
// new InetSocketTransportAddress(InetAddress.getByName(config.getElasticsearchHost()),
// Integer.parseInt(config.getElasticsearchPort())));
TransportClient ret = new PreBuiltTransportClient(Settings.builder().put("cluster.name", config.getElasticsearchCluster()).build());
ret.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(config.getElasticsearchHost()), Integer.parseInt(config.getElasticsearchPort())));
client = ret;
} catch (Exception e) {
LOGGER.error("Error initializing Elasticsearch client: {}", e.getMessage(), e);
throw new ApiException("Error initializing Elasticsearch client: " + e.getMessage(), e);
}
}
return client;
}
use of org.elasticsearch.common.transport.InetSocketTransportAddress in project fess by codelibs.
the class FessEsClient method open.
@PostConstruct
public void open() {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final String transportAddressesValue = System.getProperty(Constants.FESS_ES_TRANSPORT_ADDRESSES);
if (StringUtil.isNotBlank(transportAddressesValue)) {
for (final String transportAddressValue : transportAddressesValue.split(",")) {
final String[] addressPair = transportAddressValue.trim().split(":");
if (addressPair.length < 3) {
final String host = addressPair[0];
int port = 9300;
if (addressPair.length == 2) {
port = Integer.parseInt(addressPair[1]);
}
addTransportAddress(host, port);
} else {
logger.warn("Invalid address format: " + transportAddressValue);
}
}
}
if (transportAddressList.isEmpty()) {
if (runner == null) {
runner = new ElasticsearchClusterRunner();
final Configs config = newConfigs().clusterName(fessConfig.getElasticsearchClusterName()).numOfNode(1).useLogger();
final String esDir = System.getProperty("fess.es.dir");
if (esDir != null) {
config.basePath(esDir);
}
config.disableESLogger();
runner.onBuild((number, settingsBuilder) -> {
final File pluginDir = new File(esDir, "plugins");
if (pluginDir.isDirectory()) {
settingsBuilder.put("path.plugins", pluginDir.getAbsolutePath());
} else {
settingsBuilder.put("path.plugins", new File(System.getProperty("user.dir"), "plugins").getAbsolutePath());
}
if (settings != null) {
settingsBuilder.put(settings);
}
});
runner.build(config);
}
client = runner.client();
addTransportAddress("localhost", runner.node().settings().getAsInt("transport.tcp.port", 9300));
} else {
final Builder settingsBuilder = Settings.builder();
settingsBuilder.put("cluster.name", fessConfig.getElasticsearchClusterName());
settingsBuilder.put("client.transport.sniff", fessConfig.isElasticsearchTransportSniff());
settingsBuilder.put("client.transport.ping_timeout", fessConfig.getElasticsearchTransportPingTimeout());
settingsBuilder.put("client.transport.nodes_sampler_interval", fessConfig.getElasticsearchTransportNodesSamplerInterval());
final Settings settings = settingsBuilder.build();
final TransportClient transportClient = new PreBuiltTransportClient(settings);
for (final TransportAddress address : transportAddressList) {
transportClient.addTransportAddress(address);
}
client = transportClient;
}
if (StringUtil.isBlank(transportAddressesValue)) {
final StringBuilder buf = new StringBuilder();
for (final TransportAddress transportAddress : transportAddressList) {
if (transportAddress instanceof InetSocketTransportAddress) {
if (buf.length() > 0) {
buf.append(',');
}
final InetSocketTransportAddress inetTransportAddress = (InetSocketTransportAddress) transportAddress;
buf.append(inetTransportAddress.address().getHostName());
buf.append(':');
buf.append(inetTransportAddress.address().getPort());
}
}
if (buf.length() > 0) {
System.setProperty(Constants.FESS_ES_TRANSPORT_ADDRESSES, buf.toString());
}
}
waitForYellowStatus();
indexConfigList.forEach(configName -> {
final String[] values = configName.split("/");
if (values.length == 2) {
final String configIndex = values[0];
final String configType = values[1];
final String indexName;
final boolean isFessIndex = configIndex.equals("fess");
if (isFessIndex) {
indexName = fessConfig.getIndexDocumentUpdateIndex();
} else {
indexName = configIndex;
}
boolean exists = existsIndex(indexName);
if (!exists) {
final String createdIndexName;
if (isFessIndex) {
createdIndexName = generateNewIndexName(configIndex);
} else {
createdIndexName = configIndex;
}
createIndex(configIndex, configType, createdIndexName);
createAlias(configIndex, createdIndexName);
}
final String updatedIndexName;
if (isFessIndex) {
client.admin().cluster().prepareHealth(fessConfig.getIndexDocumentUpdateIndex()).setWaitForYellowStatus().execute().actionGet(fessConfig.getIndexIndicesTimeout());
final GetIndexResponse response = client.admin().indices().prepareGetIndex().addIndices(fessConfig.getIndexDocumentUpdateIndex()).execute().actionGet(fessConfig.getIndexIndicesTimeout());
final String[] indices = response.indices();
if (indices.length == 1) {
updatedIndexName = indices[0];
} else {
updatedIndexName = configIndex;
}
} else {
updatedIndexName = configIndex;
}
addMapping(configIndex, configType, updatedIndexName);
} else {
logger.warn("Invalid index config name: " + configName);
}
});
}
use of org.elasticsearch.common.transport.InetSocketTransportAddress in project nutch by apache.
the class ElasticIndexWriter method makeClient.
/**
* Generates a TransportClient or NodeClient
*/
protected Client makeClient(Configuration conf) throws IOException {
String clusterName = conf.get(ElasticConstants.CLUSTER);
String[] hosts = conf.getStrings(ElasticConstants.HOSTS);
int port = conf.getInt(ElasticConstants.PORT, DEFAULT_PORT);
Settings.Builder settingsBuilder = Settings.builder();
BufferedReader reader = new BufferedReader(conf.getConfResourceAsReader("elasticsearch.conf"));
String line;
String[] parts;
while ((line = reader.readLine()) != null) {
if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
line = line.trim();
parts = line.split("=");
if (parts.length == 2) {
settingsBuilder.put(parts[0].trim(), parts[1].trim());
}
}
}
// Set the cluster name and build the settings
if (StringUtils.isNotBlank(clusterName))
settingsBuilder.put("cluster.name", clusterName);
Settings settings = settingsBuilder.build();
Client client = null;
// Prefer TransportClient
if (hosts != null && port > 1) {
TransportClient transportClient = new PreBuiltTransportClient(settings);
for (String host : hosts) transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
client = transportClient;
} else if (clusterName != null) {
node = new Node(settings);
client = node.client();
}
return client;
}
Aggregations