Search in sources :

Example 1 with PreBuiltTransportClient

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;
}
Also used : MockTcpTransportPlugin(org.elasticsearch.transport.MockTcpTransportPlugin) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) Settings(org.elasticsearch.common.settings.Settings) IOException(java.io.IOException) MockTcpTransportPlugin(org.elasticsearch.transport.MockTcpTransportPlugin) Plugin(org.elasticsearch.plugins.Plugin)

Example 2 with PreBuiltTransportClient

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;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 3 with PreBuiltTransportClient

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))));
    }
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) InetSocketAddress(java.net.InetSocketAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 4 with PreBuiltTransportClient

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();
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) HashMap(java.util.HashMap) IndexResponse(org.elasticsearch.action.index.IndexResponse) InetSocketAddress(java.net.InetSocketAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 5 with PreBuiltTransportClient

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;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) Method(java.lang.reflect.Method) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProcessException(org.apache.nifi.processor.exception.ProcessException) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Client(org.elasticsearch.client.Client) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient)

Aggregations

PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)45 Settings (org.elasticsearch.common.settings.Settings)37 TransportClient (org.elasticsearch.client.transport.TransportClient)27 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)24 TransportAddress (org.elasticsearch.common.transport.TransportAddress)20 UnknownHostException (java.net.UnknownHostException)10 InetSocketAddress (java.net.InetSocketAddress)5 InetAddress (java.net.InetAddress)4 HashMap (java.util.HashMap)4 PostConstruct (javax.annotation.PostConstruct)4 Bean (org.springframework.context.annotation.Bean)4 SearchItem (cn.exrick.manager.dto.front.SearchItem)3 IOException (java.io.IOException)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 Builder (org.elasticsearch.common.settings.Settings.Builder)3 XmallException (cn.exrick.common.exception.XmallException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Map (java.util.Map)2 Logger (org.apache.log4j.Logger)2