use of org.elasticsearch.client.transport.TransportClient in project elasticsearch-suggest-plugin by spinscale.
the class TransportClientTest method getTransportClient.
private TransportClient getTransportClient() {
if (transportClient == null) {
transportClient = new TransportClient(settingsBuilder().put("cluster.name", internalCluster().getClusterName()).put("name", "programmatic_transport_client").put("client.transport.nodes_sampler_interval", "1s").build());
Transport transport = internalCluster().getDataNodeInstance(Transport.class);
transportClient.addTransportAddress(transport.boundAddress().publishAddress());
}
return transportClient;
}
use of org.elasticsearch.client.transport.TransportClient in project titan by thinkaurelius.
the class ElasticSearchIndex method legacyConfiguration.
/**
* Configure ElasticSearchIndex's ES client according to 0.4.x - 0.5.0 semantics.
* This checks local-mode first. If local-mode is true, then it creates a Node that
* uses JVM local transport and can't talk over the network. If local-mode is
* false, then it creates a TransportClient that can talk over the network and
* uses {@link com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration#INDEX_HOSTS}
* as the server addresses. Note that this configuration method
* does not allow creating a Node that talks over the network.
* <p>
* This is activated by <b>not</b> setting an explicit value for {@link #INTERFACE} in the
* Titan configuration.
*
* @see #interfaceConfiguration(com.thinkaurelius.titan.diskstorage.configuration.Configuration)
* @param config a config passed to ElasticSearchIndex's constructor
* @return a node and client object open and ready for use
*/
private ElasticSearchSetup.Connection legacyConfiguration(Configuration config) {
Node node;
Client client;
if (config.get(LOCAL_MODE)) {
log.debug("Configuring ES for JVM local transport");
boolean clientOnly = config.get(CLIENT_ONLY);
boolean local = config.get(LOCAL_MODE);
NodeBuilder builder = NodeBuilder.nodeBuilder();
Preconditions.checkArgument(config.has(INDEX_CONF_FILE) || config.has(INDEX_DIRECTORY), "Must either configure configuration file or base directory");
if (config.has(INDEX_CONF_FILE)) {
String configFile = config.get(INDEX_CONF_FILE);
ImmutableSettings.Builder sb = ImmutableSettings.settingsBuilder();
log.debug("Configuring ES from YML file [{}]", configFile);
FileInputStream fis = null;
try {
fis = new FileInputStream(configFile);
sb.loadFromStream(configFile, fis);
builder.settings(sb.build());
} catch (FileNotFoundException e) {
throw new TitanException(e);
} finally {
IOUtils.closeQuietly(fis);
}
} else {
String dataDirectory = config.get(INDEX_DIRECTORY);
log.debug("Configuring ES with data directory [{}]", dataDirectory);
File f = new File(dataDirectory);
if (!f.exists())
f.mkdirs();
ImmutableSettings.Builder b = ImmutableSettings.settingsBuilder();
for (String sub : DATA_SUBDIRS) {
String subdir = dataDirectory + File.separator + sub;
f = new File(subdir);
if (!f.exists())
f.mkdirs();
b.put("path." + sub, subdir);
}
b.put("script.disable_dynamic", false);
b.put("indices.ttl.interval", "5s");
builder.settings(b.build());
String clustername = config.get(CLUSTER_NAME);
Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
builder.clusterName(clustername);
}
node = builder.client(clientOnly).data(!clientOnly).local(local).node();
client = node.client();
} else {
log.debug("Configuring ES for network transport");
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
if (config.has(CLUSTER_NAME)) {
String clustername = config.get(CLUSTER_NAME);
Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
settings.put("cluster.name", clustername);
} else {
settings.put("client.transport.ignore_cluster_name", true);
}
log.debug("Transport sniffing enabled: {}", config.get(CLIENT_SNIFF));
settings.put("client.transport.sniff", config.get(CLIENT_SNIFF));
settings.put("script.disable_dynamic", false);
TransportClient tc = new TransportClient(settings.build());
int defaultPort = config.has(INDEX_PORT) ? config.get(INDEX_PORT) : HOST_PORT_DEFAULT;
for (String host : config.get(INDEX_HOSTS)) {
String[] hostparts = host.split(":");
String hostname = hostparts[0];
int hostport = defaultPort;
if (hostparts.length == 2)
hostport = Integer.parseInt(hostparts[1]);
log.info("Configured remote host: {} : {}", hostname, hostport);
tc.addTransportAddress(new InetSocketTransportAddress(hostname, hostport));
}
client = tc;
node = null;
}
return new ElasticSearchSetup.Connection(node, client);
}
use of org.elasticsearch.client.transport.TransportClient in project metacat by Netflix.
the class ElasticSearchConfig method elasticSearchClient.
/**
* The ElasticSearch client.
*
* @param config System config
* @return Configured client or error
*/
@Bean
public Client elasticSearchClient(final Config config) {
final String clusterName = config.getElasticSearchClusterName();
if (StringUtils.isBlank(clusterName)) {
throw new IllegalStateException("No cluster name set. Unable to continue");
}
final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).put("transport.tcp.connect_timeout", "60s").build();
final Client client = new TransportClient(settings);
// Add the transport address if exists
final String clusterNodesStr = config.getElasticSearchClusterNodes();
if (StringUtils.isNotBlank(clusterNodesStr)) {
final int port = config.getElasticSearchClusterPort();
final Iterable<String> clusterNodes = Splitter.on(',').split(clusterNodesStr);
clusterNodes.forEach(clusterNode -> ((TransportClient) client).addTransportAddress(new InetSocketTransportAddress(clusterNode, port)));
}
return client;
}
use of org.elasticsearch.client.transport.TransportClient 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.client.transport.TransportClient 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);
}
});
}
Aggregations