use of org.elasticsearch.transport.client.PreBuiltTransportClient in project elasticsearch by elastic.
the class ESSmokeClientTestCase method startClient.
private static Client startClient(Path tempDir, TransportAddress... transportAddresses) {
Settings.Builder builder = Settings.builder().put("node.name", "qa_smoke_client_" + counter.getAndIncrement()).put("client.transport.ignore_cluster_name", true).put(Environment.PATH_HOME_SETTING.getKey(), tempDir);
final Collection<Class<? extends Plugin>> plugins;
if (random().nextBoolean()) {
builder.put(NetworkModule.TRANSPORT_TYPE_KEY, MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME);
plugins = Collections.singleton(MockTcpTransportPlugin.class);
} else {
plugins = Collections.emptyList();
}
TransportClient client = new PreBuiltTransportClient(builder.build(), plugins).addTransportAddresses(transportAddresses);
logger.info("--> Elasticsearch Java TransportClient started");
Exception clientException = null;
try {
ClusterHealthResponse health = client.admin().cluster().prepareHealth().get();
logger.info("--> connected to [{}] cluster which is running [{}] node(s).", health.getClusterName(), health.getNumberOfNodes());
} catch (Exception e) {
clientException = e;
}
assumeNoException("Sounds like your cluster is not running at " + clusterAddresses, clientException);
return client;
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project flink by apache.
the class Elasticsearch5ApiCallBridge method createClient.
@Override
public Client createClient(Map<String, String> clientConfig) {
Settings settings = Settings.builder().put(clientConfig).put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME).put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME).build();
TransportClient transportClient = new PreBuiltTransportClient(settings);
for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) {
transportClient.addTransportAddress(transport);
}
// verify that we actually are connected to a cluster
if (transportClient.connectedNodes().isEmpty()) {
throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!");
}
if (LOG.isInfoEnabled()) {
LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes());
}
return transportClient;
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project uavstack by uavorg.
the class ESClient method init.
/**
* init
*
* @param esAddrs
* @param clusterName
*/
private void init(String[] esAddrs, String clusterName) {
Settings settings = Settings.EMPTY;
if (!StringHelper.isEmpty(clusterName)) {
settings = Settings.builder().put("cluster.name", clusterName).build();
}
client = new PreBuiltTransportClient(settings);
for (String esAddr : esAddrs) {
String[] ipport = esAddr.split(":");
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(ipport[0], DataConvertHelper.toInt(ipport[1], 9300))));
}
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project uavstack by uavorg.
the class DoTestESClient method main.
public static void main(String[] args) {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 9300)));
IndexRequestBuilder irb = client.prepareIndex("uav_test_db", "uav_test_table");
Map<String, Object> item = new HashMap<String, Object>();
item.put("name", "zz");
item.put("age", 1);
irb.setSource(item);
IndexResponse ir = irb.get();
System.out.println(ir.status());
client.close();
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project nifi by apache.
the class AbstractElasticsearch5TransportClientProcessor method getTransportClient.
protected Client getTransportClient(Settings.Builder settingsBuilder, String xPackPath, String username, String password, List<InetSocketAddress> esHosts, ComponentLog log) throws MalformedURLException {
// Map of headers
Map<String, String> headers = new HashMap<>();
TransportClient transportClient = null;
// authorization token if username and password are supplied.
if (!StringUtils.isBlank(xPackPath)) {
ClassLoader xPackClassloader = Thread.currentThread().getContextClassLoader();
try {
// Get the plugin class
Class xPackTransportClientClass = Class.forName("org.elasticsearch.xpack.client.PreBuiltXPackTransportClient", true, xPackClassloader);
Constructor<?> ctor = xPackTransportClientClass.getConstructor(Settings.class, Class[].class);
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
// Need a couple of classes from the X-Path Transport JAR to build the token
Class usernamePasswordTokenClass = Class.forName("org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken", true, xPackClassloader);
Class securedStringClass = Class.forName("org.elasticsearch.xpack.security.authc.support.SecuredString", true, xPackClassloader);
Constructor<?> securedStringCtor = securedStringClass.getConstructor(char[].class);
Object securePasswordString = securedStringCtor.newInstance(password.toCharArray());
Method basicAuthHeaderValue = usernamePasswordTokenClass.getMethod("basicAuthHeaderValue", String.class, securedStringClass);
String authToken = (String) basicAuthHeaderValue.invoke(null, username, securePasswordString);
if (authToken != null) {
headers.put("Authorization", authToken);
}
transportClient = (TransportClient) ctor.newInstance(settingsBuilder.build(), new Class[0]);
}
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException xPackLoadException) {
throw new ProcessException("X-Pack plugin could not be loaded and/or configured", xPackLoadException);
}
} else {
getLogger().debug("No X-Pack Transport location specified, secure connections and/or authorization will not be available");
}
// (which is logged), so continue with a non-secure client
if (transportClient == null) {
transportClient = new PreBuiltTransportClient(settingsBuilder.build());
}
if (esHosts != null) {
for (final InetSocketAddress host : esHosts) {
try {
transportClient.addTransportAddress(new InetSocketTransportAddress(host));
} catch (IllegalArgumentException iae) {
log.error("Could not add transport address {}", new Object[] { host });
}
}
}
Client client = transportClient.filterWithHeader(headers);
return client;
}
Aggregations